blob: e43b3daa35c2a11b2608d57e0073978c30363e45 [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
9import cloud_sql_client
10import config_reader
11import constants
12import datastore_client
Xixuan Wu40998892017-08-29 14:32:26 -070013import file_getter
14import rest_client
Xixuan Wu51bb7102019-03-18 14:51:44 -070015import task_config_reader
Xixuan Wu40998892017-08-29 14:32:26 -070016
17
18class 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 Wuc6819012019-05-23 11:34:59 -070026 # TODO(xixuan): to be deprecrated, replaced by Buildbucket client.
Xixuan Wu40998892017-08-29 14:32:26 -070027 self._cidb_client = cloud_sql_client.CIDBClient('CIDB', 'cidb')
Xixuan Wuc6819012019-05-23 11:34:59 -070028 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 Wu40998892017-08-29 14:32:26 -070033 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 Wu51bb7102019-03-18 14:51:44 -070040 self._task_config = task_config_reader.TaskConfig(
41 config_reader.ConfigReader(file_getter.SUITE_SCHEDULER_CONFIG_FILE))
42
Xixuan Wu40998892017-08-29 14:32:26 -070043 lab_config_reader = config_reader.ConfigReader(
44 file_getter.LAB_CONFIG_FILE)
45 self._lab_config = config_reader.LabConfig(lab_config_reader)
Xixuan Wuf4a4c882019-03-15 14:48:26 -070046 migration_config_reader = config_reader.ConfigReader(
47 file_getter.MIGRATION_CONFIG_FILE)
48 self._migration_config = config_reader.MigrationConfig(
49 migration_config_reader)
Xixuan Wu40998892017-08-29 14:32:26 -070050
Xixuan Wu40998892017-08-29 14:32:26 -070051 # Initialize events
52 self.events = {}
Xixuan Wu51bb7102019-03-18 14:51:44 -070053 for keyword, klass in task_config_reader.EVENT_CLASSES.iteritems():
Xixuan Wu40998892017-08-29 14:32:26 -070054 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 Wu4ac56dd2017-10-12 11:59:30 -070061 self._task_config.get_tasks_by_keyword(klass.KEYWORD)['tasks'])
Xixuan Wu40998892017-08-29 14:32:26 -070062 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 Bergstrom58263d32018-04-26 14:11:35 -060097 cros_builds_tuple = event.get_cros_builds(
Xixuan Wuc6819012019-05-23 11:34:59 -070098 self._lab_config, self._build_client)
Craig Bergstrom58263d32018-04-26 14:11:35 -060099 logging.debug('Found CrOS builds: %r', cros_builds_tuple)
Xixuan Wu40998892017-08-29 14:32:26 -0700100 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 Bergstrom58263d32018-04-26 14:11:35 -0600106 cros_builds_tuple=cros_builds_tuple,
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700107 configs=config_reader.Configs(
108 lab_config=self._lab_config,
109 migration_config=self._migration_config),
Xixuan Wuc6819012019-05-23 11:34:59 -0700110 build_client=self._build_client)
Xixuan Wu40998892017-08-29 14:32:26 -0700111 logging.info('Finished processing all tasks')