blob: 58f37f5e9df316e069864acf5761b0f89010c031 [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
12import rest_client
13import task_executor
14import time_converter
15import trigger_receiver
16
17
18_PROD_CALENDAR_ID_MAPPING = {
19 'new_build': constants.CalendarID.PROD_NEW_BUILD,
20 'nightly': constants.CalendarID.PROD_NIGHTLY,
21 'weekly': constants.CalendarID.PROD_WEEKLY,
22}
23
24_STAGING_CALENDAR_ID_MAPPING = {
25 'new_build': constants.CalendarID.STAGING_NEW_BUILD,
26 'nightly': constants.CalendarID.STAGING_NIGHTLY,
27 'weekly': constants.CalendarID.STAGING_WEEKLY,
28}
29
30_CALENDAR_TIMEZONE = 'America/Los_Angeles'
31
32
Xixuan Wu9be29082017-10-11 08:49:10 -070033def _is_in_staging():
34 """Check whether this script is running on staging instance or not.
35
36 Returns:
37 A boolean indicating the script is running on staging instance
38 suite-scheduler-staging.googleplex.com or not.
39 """
40 return (constants.environment() == constants.RunningEnv.ENV_PROD and
41 constants.application_id() == constants.AppID.STAGING_APP)
42
43
Xixuan Wu27a61f82017-09-14 11:42:37 -070044class TriggerEvent(webapp2.RequestHandler):
45 """Trigger events regularly to schedule tasks for suite_scheduler."""
46
47 def get(self):
Xixuan Wu9be29082017-10-11 08:49:10 -070048 # Don't kick off cron job in staging instance. This cron job can be
49 # kicked off On local development env or prod instance.
50 if _is_in_staging():
51 return
52
Xixuan Wu27a61f82017-09-14 11:42:37 -070053 start_time = time_converter.pst_now()
54 suite_trigger = trigger_receiver.TriggerReceiver()
55 suite_trigger.cron()
56 end_time = time_converter.pst_now()
57
58 calendar_client = rest_client.CalendarRestClient(
59 rest_client.BaseRestClient(
60 constants.RestClient.CALENDAR_CLIENT.scopes,
61 constants.RestClient.CALENDAR_CLIENT.service_name,
62 constants.RestClient.CALENDAR_CLIENT.service_version))
63
64 for keyword, results in suite_trigger.event_results.iteritems():
65 # No finished tasks for the given keyword
66 if not results:
67 continue
68
69 event = {}
70 event['summary'] = '%s Suite Tasks - Scheduled' % keyword
71 description = '%d scheduled tasks: \n' % len(results)
72 for task_name in results:
73 description += task_name + '\n'
74
75 event['description'] = description
76 # The start time is exactly when the cron job is triggered, i.e. when
77 # all tasks of this event are pushed into the task queue. It's not
78 # when the tasks are 'really' kicked off in lab because there will be
79 # some time delay since task queue will schedule tasks batch by batch.
80 # However, the end time is not exactly when the cron job is finished,
81 # since this cron job's runtime is very short, but the calendar won't
82 # show any item that's shorter than 30 minutes. So no matter what
83 # endtime we set here, it will show a half-an-hour event.
84 event['start'] = {
85 'dateTime': start_time.strftime(time_converter.CALENDAR_TIME_FORMAT),
86 'timeZone': _CALENDAR_TIMEZONE,
87 }
88 event['end'] = {
89 'dateTime': end_time.strftime(time_converter.CALENDAR_TIME_FORMAT),
90 'timeZone': _CALENDAR_TIMEZONE,
91 }
92
93 if constants.environment() == constants.RunningEnv.ENV_PROD:
Xixuan Wu9be29082017-10-11 08:49:10 -070094 calendar_client.add_event(_PROD_CALENDAR_ID_MAPPING[keyword], event)
Xixuan Wu27a61f82017-09-14 11:42:37 -070095 else:
96 logging.info(event)
97
98
99class ExecuteTask(webapp2.RequestHandler):
100 """Run scheduled tasks regularly for suite_scheduler."""
101
102 def get(self):
Xixuan Wu9be29082017-10-11 08:49:10 -0700103 if _is_in_staging():
104 return
105
Xixuan Wu27a61f82017-09-14 11:42:37 -0700106 task_processor = task_executor.TaskProcessor(task_executor.SUITES_QUEUE)
107 task_processor.batch_execute()
108
109
110app = webapp2.WSGIApplication([
111 ('/cron/trigger_event', TriggerEvent),
112 ('/cron/execute_task', ExecuteTask),
113], debug=True)