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