blob: c7bac7e056d1b2981e11587cd0645073428e2c77 [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
15
16
17class 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)
Xixuan Wuf4a4c882019-03-15 14:48:26 -070039 migration_config_reader = config_reader.ConfigReader(
40 file_getter.MIGRATION_CONFIG_FILE)
41 self._migration_config = config_reader.MigrationConfig(
42 migration_config_reader)
Xixuan Wu40998892017-08-29 14:32:26 -070043
Xixuan Wu40998892017-08-29 14:32:26 -070044 # Initialize events
45 self.events = {}
46 for keyword, klass in config_reader.EVENT_CLASSES.iteritems():
47 logging.info('Initializing %s event', keyword)
48 new_event = klass(
49 self._task_config.get_event_setting(klass.section_name()),
50 self._last_exec_client.get_last_execute_time(klass.KEYWORD))
51
52 if new_event.should_handle:
53 new_event.set_task_list(
Xixuan Wu4ac56dd2017-10-12 11:59:30 -070054 self._task_config.get_tasks_by_keyword(klass.KEYWORD)['tasks'])
Xixuan Wu40998892017-08-29 14:32:26 -070055 logging.info('Got %d tasks for %s event',
56 len(new_event.task_list), klass.KEYWORD)
57
58 self.events[keyword] = new_event
59
60 self.event_results = {}
61
62 def cron(self):
63 """The cron job to scheduler suite jobs by config.
64
65 This cron job executes:
66 1. Filter out the tasks that shoud be run at this round.
67 2. Fetch launch_control_build for Android boards from API.
68 3. Fetch cros_builds for ChromeOS boards from CIDB.
69 4. Schedule corresponding jobs with fetched builds.
70 5. Reset event when it's finished.
71 """
72 for keyword, event in self.events.iteritems():
73 logging.info('Handling %s event in cron job', keyword)
74
75 event.filter_tasks()
76 if event.task_list:
77 logging.info('Processing %d tasks.', len(event.task_list))
78 self._schedule_tasks(event)
79 else:
80 logging.info('No task list found')
81 event.finish()
82
83 def _schedule_tasks(self, event):
84 """Schedule tasks based on given event.
85
86 Args:
87 event: a kind of event, which contains its tasks that should be
88 scheduled.
89 """
Craig Bergstrom58263d32018-04-26 14:11:35 -060090 cros_builds_tuple = event.get_cros_builds(
91 self._lab_config, self._cidb_client)
92 logging.debug('Found CrOS builds: %r', cros_builds_tuple)
Xixuan Wu40998892017-08-29 14:32:26 -070093 launch_control_builds = event.get_launch_control_builds(
94 self._lab_config, self._android_client)
95 logging.debug('Found launch_control_builds: %r', launch_control_builds)
96
97 self.event_results[event.KEYWORD] = event.process_tasks(
98 launch_control_builds=launch_control_builds,
Craig Bergstrom58263d32018-04-26 14:11:35 -060099 cros_builds_tuple=cros_builds_tuple,
Xixuan Wuf4a4c882019-03-15 14:48:26 -0700100 configs=config_reader.Configs(
101 lab_config=self._lab_config,
102 migration_config=self._migration_config),
Xixuan Wu40998892017-08-29 14:32:26 -0700103 db_client=self._cidb_client)
104 logging.info('Finished processing all tasks')