blob: 65c89ebb95e23fb632341b356f2d1e2cf8011c1e [file] [log] [blame]
Xixuan Wu27a61f82017-09-14 11:42:37 -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 jobs kicked off by suite scheduler."""
6# pylint: disable=g-bad-import-order
7
8import logging
9import webapp2
10
11import constants
Xixuan Wua5a29442017-10-11 11:03:02 -070012import global_config
Xixuan Wu27a61f82017-09-14 11:42:37 -070013import rest_client
14import task_executor
15import time_converter
16import trigger_receiver
17
18
19_PROD_CALENDAR_ID_MAPPING = {
20 'new_build': constants.CalendarID.PROD_NEW_BUILD,
21 'nightly': constants.CalendarID.PROD_NIGHTLY,
22 'weekly': constants.CalendarID.PROD_WEEKLY,
23}
24
25_STAGING_CALENDAR_ID_MAPPING = {
26 'new_build': constants.CalendarID.STAGING_NEW_BUILD,
27 'nightly': constants.CalendarID.STAGING_NIGHTLY,
28 'weekly': constants.CalendarID.STAGING_WEEKLY,
29}
30
31_CALENDAR_TIMEZONE = 'America/Los_Angeles'
32
33
34class TriggerEvent(webapp2.RequestHandler):
35 """Trigger events regularly to schedule tasks for suite_scheduler."""
36
37 def get(self):
Xixuan Wu9be29082017-10-11 08:49:10 -070038 # Don't kick off cron job in staging instance. This cron job can be
39 # kicked off On local development env or prod instance.
Xixuan Wua5a29442017-10-11 11:03:02 -070040 if global_config.is_in_staging():
Xixuan Wu9be29082017-10-11 08:49:10 -070041 return
42
Xixuan Wu27a61f82017-09-14 11:42:37 -070043 start_time = time_converter.pst_now()
44 suite_trigger = trigger_receiver.TriggerReceiver()
45 suite_trigger.cron()
46 end_time = time_converter.pst_now()
47
48 calendar_client = rest_client.CalendarRestClient(
49 rest_client.BaseRestClient(
50 constants.RestClient.CALENDAR_CLIENT.scopes,
51 constants.RestClient.CALENDAR_CLIENT.service_name,
52 constants.RestClient.CALENDAR_CLIENT.service_version))
53
54 for keyword, results in suite_trigger.event_results.iteritems():
55 # No finished tasks for the given keyword
56 if not results:
57 continue
58
59 event = {}
60 event['summary'] = '%s Suite Tasks - Scheduled' % keyword
61 description = '%d scheduled tasks: \n' % len(results)
62 for task_name in results:
63 description += task_name + '\n'
64
65 event['description'] = description
66 # The start time is exactly when the cron job is triggered, i.e. when
67 # all tasks of this event are pushed into the task queue. It's not
68 # when the tasks are 'really' kicked off in lab because there will be
69 # some time delay since task queue will schedule tasks batch by batch.
70 # However, the end time is not exactly when the cron job is finished,
71 # since this cron job's runtime is very short, but the calendar won't
72 # show any item that's shorter than 30 minutes. So no matter what
73 # endtime we set here, it will show a half-an-hour event.
74 event['start'] = {
75 'dateTime': start_time.strftime(time_converter.CALENDAR_TIME_FORMAT),
76 'timeZone': _CALENDAR_TIMEZONE,
77 }
78 event['end'] = {
79 'dateTime': end_time.strftime(time_converter.CALENDAR_TIME_FORMAT),
80 'timeZone': _CALENDAR_TIMEZONE,
81 }
82
83 if constants.environment() == constants.RunningEnv.ENV_PROD:
Xixuan Wu9be29082017-10-11 08:49:10 -070084 calendar_client.add_event(_PROD_CALENDAR_ID_MAPPING[keyword], event)
Xixuan Wu27a61f82017-09-14 11:42:37 -070085 else:
86 logging.info(event)
87
88
89class ExecuteTask(webapp2.RequestHandler):
90 """Run scheduled tasks regularly for suite_scheduler."""
91
92 def get(self):
Xixuan Wua5a29442017-10-11 11:03:02 -070093 if global_config.is_in_staging():
Xixuan Wu9be29082017-10-11 08:49:10 -070094 return
95
Xixuan Wu27a61f82017-09-14 11:42:37 -070096 task_processor = task_executor.TaskProcessor(task_executor.SUITES_QUEUE)
97 task_processor.batch_execute()
98
99
Xixuan Wua5a29442017-10-11 11:03:02 -0700100class TestPush(webapp2.RequestHandler):
101 """Test push for suite_scheduler on staging instance."""
102
103 def get(self):
104 if global_config.is_in_staging():
105 return
106
107 # Test Cron jobs
108 # 1) No tasks will be filtered by nightly/weekly constraints.
109 # 2) Randomly select builds in |get_cros_builds_since_date_from_db|.
110 # 3) Every task will be kicked off with dummy swarming run |dummy_run|.
111
112 suite_trigger = trigger_receiver.TriggerReceiver()
113 suite_trigger.cron()
114
115 task_processor = task_executor.TaskProcessor(task_executor.SUITES_QUEUE)
116 task_processor.batch_execute()
117 # In testing, after one round of batch_execute() to execute all tasks,
118 # the suite queue will be purged.
119 task_processor.purge()
120
121
Xixuan Wu27a61f82017-09-14 11:42:37 -0700122app = webapp2.WSGIApplication([
123 ('/cron/trigger_event', TriggerEvent),
124 ('/cron/execute_task', ExecuteTask),
Xixuan Wua5a29442017-10-11 11:03:02 -0700125 ('/cron/test_push', TestPush),
Xixuan Wu27a61f82017-09-14 11:42:37 -0700126], debug=True)