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