Xixuan Wu | 4099889 | 2017-08-29 14:32:26 -0700 | [diff] [blame] | 1 | # Copyright 2017 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Module for cron job to trigger events for suite scheduler.""" |
| 6 | |
| 7 | import logging |
| 8 | |
Xixuan Wu | 4099889 | 2017-08-29 14:32:26 -0700 | [diff] [blame] | 9 | import config_reader |
| 10 | import constants |
| 11 | import datastore_client |
Xixuan Wu | 4099889 | 2017-08-29 14:32:26 -0700 | [diff] [blame] | 12 | import file_getter |
| 13 | import rest_client |
Xixuan Wu | 51bb710 | 2019-03-18 14:51:44 -0700 | [diff] [blame] | 14 | import task_config_reader |
Xixuan Wu | 4099889 | 2017-08-29 14:32:26 -0700 | [diff] [blame] | 15 | |
| 16 | |
| 17 | class TriggerReceiver(object): |
| 18 | """The class for receiving event triggers.""" |
| 19 | |
| 20 | def __init__(self): |
| 21 | """Initialize a trigger receiver. |
| 22 | |
| 23 | Its job is to fetch triggers from config files and trigger them. |
| 24 | """ |
Xixuan Wu | c681901 | 2019-05-23 11:34:59 -0700 | [diff] [blame] | 25 | self._build_client = rest_client.BuildBucketBigqueryClient( |
| 26 | rest_client.BaseRestClient( |
| 27 | constants.RestClient.BIGQUERY_CLIENT.scopes, |
| 28 | constants.RestClient.BIGQUERY_CLIENT.service_name, |
| 29 | constants.RestClient.BIGQUERY_CLIENT.service_version)) |
Xixuan Wu | 4099889 | 2017-08-29 14:32:26 -0700 | [diff] [blame] | 30 | self._last_exec_client = datastore_client.LastExecutionRecordStore() |
| 31 | self._android_client = rest_client.AndroidBuildRestClient( |
| 32 | rest_client.BaseRestClient( |
| 33 | constants.RestClient.ANDROID_BUILD_CLIENT.scopes, |
| 34 | constants.RestClient.ANDROID_BUILD_CLIENT.service_name, |
| 35 | constants.RestClient.ANDROID_BUILD_CLIENT.service_version)) |
| 36 | |
Xixuan Wu | 51bb710 | 2019-03-18 14:51:44 -0700 | [diff] [blame] | 37 | self._task_config = task_config_reader.TaskConfig( |
| 38 | config_reader.ConfigReader(file_getter.SUITE_SCHEDULER_CONFIG_FILE)) |
| 39 | |
Xixuan Wu | 4099889 | 2017-08-29 14:32:26 -0700 | [diff] [blame] | 40 | lab_config_reader = config_reader.ConfigReader( |
| 41 | file_getter.LAB_CONFIG_FILE) |
| 42 | self._lab_config = config_reader.LabConfig(lab_config_reader) |
Xixuan Wu | f4a4c88 | 2019-03-15 14:48:26 -0700 | [diff] [blame] | 43 | migration_config_reader = config_reader.ConfigReader( |
| 44 | file_getter.MIGRATION_CONFIG_FILE) |
| 45 | self._migration_config = config_reader.MigrationConfig( |
| 46 | migration_config_reader) |
Xixuan Wu | 4099889 | 2017-08-29 14:32:26 -0700 | [diff] [blame] | 47 | |
Xixuan Wu | 4099889 | 2017-08-29 14:32:26 -0700 | [diff] [blame] | 48 | # Initialize events |
| 49 | self.events = {} |
Xixuan Wu | 51bb710 | 2019-03-18 14:51:44 -0700 | [diff] [blame] | 50 | for keyword, klass in task_config_reader.EVENT_CLASSES.iteritems(): |
Xixuan Wu | 4099889 | 2017-08-29 14:32:26 -0700 | [diff] [blame] | 51 | logging.info('Initializing %s event', keyword) |
| 52 | new_event = klass( |
| 53 | self._task_config.get_event_setting(klass.section_name()), |
| 54 | self._last_exec_client.get_last_execute_time(klass.KEYWORD)) |
| 55 | |
| 56 | if new_event.should_handle: |
| 57 | new_event.set_task_list( |
Xixuan Wu | 4ac56dd | 2017-10-12 11:59:30 -0700 | [diff] [blame] | 58 | self._task_config.get_tasks_by_keyword(klass.KEYWORD)['tasks']) |
Xixuan Wu | 4099889 | 2017-08-29 14:32:26 -0700 | [diff] [blame] | 59 | logging.info('Got %d tasks for %s event', |
| 60 | len(new_event.task_list), klass.KEYWORD) |
| 61 | |
| 62 | self.events[keyword] = new_event |
| 63 | |
| 64 | self.event_results = {} |
| 65 | |
| 66 | def cron(self): |
| 67 | """The cron job to scheduler suite jobs by config. |
| 68 | |
| 69 | This cron job executes: |
| 70 | 1. Filter out the tasks that shoud be run at this round. |
| 71 | 2. Fetch launch_control_build for Android boards from API. |
Xixuan Wu | 6ec23e3 | 2019-05-23 11:56:02 -0700 | [diff] [blame] | 72 | 3. Fetch cros_builds for ChromeOS boards from Buildbucket bigquery. |
Xixuan Wu | 4099889 | 2017-08-29 14:32:26 -0700 | [diff] [blame] | 73 | 4. Schedule corresponding jobs with fetched builds. |
| 74 | 5. Reset event when it's finished. |
| 75 | """ |
| 76 | for keyword, event in self.events.iteritems(): |
| 77 | logging.info('Handling %s event in cron job', keyword) |
| 78 | |
| 79 | event.filter_tasks() |
| 80 | if event.task_list: |
| 81 | logging.info('Processing %d tasks.', len(event.task_list)) |
| 82 | self._schedule_tasks(event) |
| 83 | else: |
| 84 | logging.info('No task list found') |
| 85 | event.finish() |
| 86 | |
| 87 | def _schedule_tasks(self, event): |
| 88 | """Schedule tasks based on given event. |
| 89 | |
| 90 | Args: |
| 91 | event: a kind of event, which contains its tasks that should be |
| 92 | scheduled. |
| 93 | """ |
Craig Bergstrom | 58263d3 | 2018-04-26 14:11:35 -0600 | [diff] [blame] | 94 | cros_builds_tuple = event.get_cros_builds( |
Xixuan Wu | c681901 | 2019-05-23 11:34:59 -0700 | [diff] [blame] | 95 | self._lab_config, self._build_client) |
Craig Bergstrom | 58263d3 | 2018-04-26 14:11:35 -0600 | [diff] [blame] | 96 | logging.debug('Found CrOS builds: %r', cros_builds_tuple) |
Xixuan Wu | 4099889 | 2017-08-29 14:32:26 -0700 | [diff] [blame] | 97 | launch_control_builds = event.get_launch_control_builds( |
| 98 | self._lab_config, self._android_client) |
| 99 | logging.debug('Found launch_control_builds: %r', launch_control_builds) |
| 100 | |
| 101 | self.event_results[event.KEYWORD] = event.process_tasks( |
| 102 | launch_control_builds=launch_control_builds, |
Craig Bergstrom | 58263d3 | 2018-04-26 14:11:35 -0600 | [diff] [blame] | 103 | cros_builds_tuple=cros_builds_tuple, |
Xixuan Wu | f4a4c88 | 2019-03-15 14:48:26 -0700 | [diff] [blame] | 104 | configs=config_reader.Configs( |
| 105 | lab_config=self._lab_config, |
| 106 | migration_config=self._migration_config), |
Xixuan Wu | c681901 | 2019-05-23 11:34:59 -0700 | [diff] [blame] | 107 | build_client=self._build_client) |
Xixuan Wu | 4099889 | 2017-08-29 14:32:26 -0700 | [diff] [blame] | 108 | logging.info('Finished processing all tasks') |