Xixuan Wu | 27a61f8 | 2017-09-14 11:42:37 -0700 | [diff] [blame] | 1 | # 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 | |
| 8 | import logging |
| 9 | import webapp2 |
| 10 | |
| 11 | import constants |
Xixuan Wu | a5a2944 | 2017-10-11 11:03:02 -0700 | [diff] [blame] | 12 | import global_config |
Xixuan Wu | 27a61f8 | 2017-09-14 11:42:37 -0700 | [diff] [blame] | 13 | import rest_client |
| 14 | import task_executor |
| 15 | import time_converter |
| 16 | import 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 | |
| 34 | class TriggerEvent(webapp2.RequestHandler): |
| 35 | """Trigger events regularly to schedule tasks for suite_scheduler.""" |
| 36 | |
| 37 | def get(self): |
Xixuan Wu | 9be2908 | 2017-10-11 08:49:10 -0700 | [diff] [blame] | 38 | # 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 Wu | a5a2944 | 2017-10-11 11:03:02 -0700 | [diff] [blame] | 40 | if global_config.is_in_staging(): |
Xixuan Wu | 9be2908 | 2017-10-11 08:49:10 -0700 | [diff] [blame] | 41 | return |
| 42 | |
Xixuan Wu | 27a61f8 | 2017-09-14 11:42:37 -0700 | [diff] [blame] | 43 | 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 Wu | 9be2908 | 2017-10-11 08:49:10 -0700 | [diff] [blame] | 84 | calendar_client.add_event(_PROD_CALENDAR_ID_MAPPING[keyword], event) |
Xixuan Wu | 27a61f8 | 2017-09-14 11:42:37 -0700 | [diff] [blame] | 85 | else: |
| 86 | logging.info(event) |
| 87 | |
| 88 | |
| 89 | class ExecuteTask(webapp2.RequestHandler): |
| 90 | """Run scheduled tasks regularly for suite_scheduler.""" |
| 91 | |
| 92 | def get(self): |
Xixuan Wu | a5a2944 | 2017-10-11 11:03:02 -0700 | [diff] [blame] | 93 | if global_config.is_in_staging(): |
Xixuan Wu | 9be2908 | 2017-10-11 08:49:10 -0700 | [diff] [blame] | 94 | return |
| 95 | |
Xixuan Wu | 27a61f8 | 2017-09-14 11:42:37 -0700 | [diff] [blame] | 96 | task_processor = task_executor.TaskProcessor(task_executor.SUITES_QUEUE) |
| 97 | task_processor.batch_execute() |
| 98 | |
| 99 | |
Xixuan Wu | a5a2944 | 2017-10-11 11:03:02 -0700 | [diff] [blame] | 100 | class 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 Wu | 27a61f8 | 2017-09-14 11:42:37 -0700 | [diff] [blame] | 122 | app = webapp2.WSGIApplication([ |
| 123 | ('/cron/trigger_event', TriggerEvent), |
| 124 | ('/cron/execute_task', ExecuteTask), |
Xixuan Wu | a5a2944 | 2017-10-11 11:03:02 -0700 | [diff] [blame] | 125 | ('/cron/test_push', TestPush), |
Xixuan Wu | 27a61f8 | 2017-09-14 11:42:37 -0700 | [diff] [blame] | 126 | ], debug=True) |