blob: 9913689c19fc7daeb6ee057920e72e1bf4008f59 [file] [log] [blame]
Xixuan Wu40998892017-08-29 14:32:26 -07001# 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
7import logging
8
Xixuan Wu40998892017-08-29 14:32:26 -07009import config_reader
10import constants
11import datastore_client
Xixuan Wu40998892017-08-29 14:32:26 -070012import file_getter
13import rest_client
Xixuan Wu51bb7102019-03-18 14:51:44 -070014import task_config_reader
Xixuan Wu40998892017-08-29 14:32:26 -070015
16
17class TriggerReceiver(object):
18 """The class for receiving event triggers."""
19
Brigit Rossbach50d086f2020-09-17 13:29:49 -060020 def __init__(self, fake=False):
Xixuan Wu40998892017-08-29 14:32:26 -070021 """Initialize a trigger receiver.
22
23 Its job is to fetch triggers from config files and trigger them.
24 """
Xixuan Wuc6819012019-05-23 11:34:59 -070025 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 Wu40998892017-08-29 14:32:26 -070030 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))
Brigit Rossbach50d086f2020-09-17 13:29:49 -060036 if fake:
37 self._task_config = task_config_reader.TaskConfig(
38 config_reader.ConfigReader(
39 file_getter.TEST_SUITE_SCHEDULER_CONFIG_FILE))
40 fake_lab_config_reader = config_reader.ConfigReader(
41 file_getter.LAB_CONFIG_FILE)
42 self._lab_config = config_reader.LabConfig(fake_lab_config_reader)
43 else:
44 file_getter.clone_config()
45 self._task_config = task_config_reader.TaskConfig(
46 config_reader.ConfigReader(
47 file_getter.NEW_SUITE_SCHEDULER_CONFIG_FILE))
48 lab_config_reader = config_reader.ConfigReader(
49 file_getter.NEW_LAB_CONFIG_FILE)
50 self._lab_config = config_reader.LabConfig(lab_config_reader)
Xixuan Wu40998892017-08-29 14:32:26 -070051
Xixuan Wu40998892017-08-29 14:32:26 -070052 # Initialize events
53 self.events = {}
Xixuan Wu51bb7102019-03-18 14:51:44 -070054 for keyword, klass in task_config_reader.EVENT_CLASSES.iteritems():
Xixuan Wu40998892017-08-29 14:32:26 -070055 logging.info('Initializing %s event', keyword)
56 new_event = klass(
57 self._task_config.get_event_setting(klass.section_name()),
58 self._last_exec_client.get_last_execute_time(klass.KEYWORD))
59
60 if new_event.should_handle:
61 new_event.set_task_list(
Xixuan Wu4ac56dd2017-10-12 11:59:30 -070062 self._task_config.get_tasks_by_keyword(klass.KEYWORD)['tasks'])
Xixuan Wu40998892017-08-29 14:32:26 -070063 logging.info('Got %d tasks for %s event',
64 len(new_event.task_list), klass.KEYWORD)
65
66 self.events[keyword] = new_event
67
68 self.event_results = {}
69
70 def cron(self):
71 """The cron job to scheduler suite jobs by config.
72
73 This cron job executes:
74 1. Filter out the tasks that shoud be run at this round.
75 2. Fetch launch_control_build for Android boards from API.
Xixuan Wu6ec23e32019-05-23 11:56:02 -070076 3. Fetch cros_builds for ChromeOS boards from Buildbucket bigquery.
Xixuan Wu40998892017-08-29 14:32:26 -070077 4. Schedule corresponding jobs with fetched builds.
78 5. Reset event when it's finished.
79 """
80 for keyword, event in self.events.iteritems():
81 logging.info('Handling %s event in cron job', keyword)
82
83 event.filter_tasks()
84 if event.task_list:
85 logging.info('Processing %d tasks.', len(event.task_list))
86 self._schedule_tasks(event)
87 else:
88 logging.info('No task list found')
89 event.finish()
90
91 def _schedule_tasks(self, event):
92 """Schedule tasks based on given event.
93
94 Args:
95 event: a kind of event, which contains its tasks that should be
96 scheduled.
97 """
Craig Bergstrom58263d32018-04-26 14:11:35 -060098 cros_builds_tuple = event.get_cros_builds(
Xixuan Wuc6819012019-05-23 11:34:59 -070099 self._lab_config, self._build_client)
Craig Bergstrom58263d32018-04-26 14:11:35 -0600100 logging.debug('Found CrOS builds: %r', cros_builds_tuple)
Xixuan Wu40998892017-08-29 14:32:26 -0700101 launch_control_builds = event.get_launch_control_builds(
102 self._lab_config, self._android_client)
103 logging.debug('Found launch_control_builds: %r', launch_control_builds)
Xinan Lin8b291982019-12-16 10:03:56 -0800104 firmware_builds = None
105 if [x for x in event.task_list if (x.firmware_rw_build_spec or
106 x.firmware_ro_build_spec)]:
107 firmware_builds = event.get_firmware_builds(self._build_client)
Xixuan Wu40998892017-08-29 14:32:26 -0700108
109 self.event_results[event.KEYWORD] = event.process_tasks(
110 launch_control_builds=launch_control_builds,
Craig Bergstrom58263d32018-04-26 14:11:35 -0600111 cros_builds_tuple=cros_builds_tuple,
Xinan Lin028f9582019-12-11 10:55:33 -0800112 firmware_builds=firmware_builds,
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700113 configs=config_reader.Configs(
Xinan Lin576022d2020-01-21 18:12:47 -0800114 lab_config=self._lab_config))
Xixuan Wu40998892017-08-29 14:32:26 -0700115 logging.info('Finished processing all tasks')