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 |
| 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 | """ |
| 25 | self._cidb_client = cloud_sql_client.CIDBClient('CIDB', 'cidb') |
| 26 | self._last_exec_client = datastore_client.LastExecutionRecordStore() |
| 27 | self._android_client = rest_client.AndroidBuildRestClient( |
| 28 | rest_client.BaseRestClient( |
| 29 | constants.RestClient.ANDROID_BUILD_CLIENT.scopes, |
| 30 | constants.RestClient.ANDROID_BUILD_CLIENT.service_name, |
| 31 | constants.RestClient.ANDROID_BUILD_CLIENT.service_version)) |
| 32 | |
| 33 | task_config_reader = config_reader.ConfigReader( |
| 34 | file_getter.SUITE_SCHEDULER_CONFIG_FILE) |
| 35 | self._task_config = config_reader.TaskConfig(task_config_reader) |
| 36 | lab_config_reader = config_reader.ConfigReader( |
| 37 | file_getter.LAB_CONFIG_FILE) |
| 38 | self._lab_config = config_reader.LabConfig(lab_config_reader) |
| 39 | |
Xixuan Wu | 4099889 | 2017-08-29 14:32:26 -0700 | [diff] [blame] | 40 | # Initialize events |
| 41 | self.events = {} |
| 42 | for keyword, klass in config_reader.EVENT_CLASSES.iteritems(): |
| 43 | logging.info('Initializing %s event', keyword) |
| 44 | new_event = klass( |
| 45 | self._task_config.get_event_setting(klass.section_name()), |
| 46 | self._last_exec_client.get_last_execute_time(klass.KEYWORD)) |
| 47 | |
| 48 | if new_event.should_handle: |
| 49 | new_event.set_task_list( |
Xixuan Wu | 4ac56dd | 2017-10-12 11:59:30 -0700 | [diff] [blame] | 50 | self._task_config.get_tasks_by_keyword(klass.KEYWORD)['tasks']) |
Xixuan Wu | 4099889 | 2017-08-29 14:32:26 -0700 | [diff] [blame] | 51 | logging.info('Got %d tasks for %s event', |
| 52 | len(new_event.task_list), klass.KEYWORD) |
| 53 | |
| 54 | self.events[keyword] = new_event |
| 55 | |
| 56 | self.event_results = {} |
| 57 | |
| 58 | def cron(self): |
| 59 | """The cron job to scheduler suite jobs by config. |
| 60 | |
| 61 | This cron job executes: |
| 62 | 1. Filter out the tasks that shoud be run at this round. |
| 63 | 2. Fetch launch_control_build for Android boards from API. |
| 64 | 3. Fetch cros_builds for ChromeOS boards from CIDB. |
| 65 | 4. Schedule corresponding jobs with fetched builds. |
| 66 | 5. Reset event when it's finished. |
| 67 | """ |
| 68 | for keyword, event in self.events.iteritems(): |
| 69 | logging.info('Handling %s event in cron job', keyword) |
| 70 | |
| 71 | event.filter_tasks() |
| 72 | if event.task_list: |
| 73 | logging.info('Processing %d tasks.', len(event.task_list)) |
| 74 | self._schedule_tasks(event) |
| 75 | else: |
| 76 | logging.info('No task list found') |
| 77 | event.finish() |
| 78 | |
| 79 | def _schedule_tasks(self, event): |
| 80 | """Schedule tasks based on given event. |
| 81 | |
| 82 | Args: |
| 83 | event: a kind of event, which contains its tasks that should be |
| 84 | scheduled. |
| 85 | """ |
Craig Bergstrom | 58263d3 | 2018-04-26 14:11:35 -0600 | [diff] [blame] | 86 | cros_builds_tuple = event.get_cros_builds( |
| 87 | self._lab_config, self._cidb_client) |
| 88 | logging.debug('Found CrOS builds: %r', cros_builds_tuple) |
Xixuan Wu | 4099889 | 2017-08-29 14:32:26 -0700 | [diff] [blame] | 89 | launch_control_builds = event.get_launch_control_builds( |
| 90 | self._lab_config, self._android_client) |
| 91 | logging.debug('Found launch_control_builds: %r', launch_control_builds) |
| 92 | |
| 93 | self.event_results[event.KEYWORD] = event.process_tasks( |
| 94 | launch_control_builds=launch_control_builds, |
Craig Bergstrom | 58263d3 | 2018-04-26 14:11:35 -0600 | [diff] [blame] | 95 | cros_builds_tuple=cros_builds_tuple, |
Xixuan Wu | 4099889 | 2017-08-29 14:32:26 -0700 | [diff] [blame] | 96 | lab_config=self._lab_config, |
| 97 | db_client=self._cidb_client) |
| 98 | logging.info('Finished processing all tasks') |