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 |
| 12 | import rest_client |
| 13 | import task_executor |
| 14 | import time_converter |
| 15 | import 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 | |
| 33 | class TriggerEvent(webapp2.RequestHandler): |
| 34 | """Trigger events regularly to schedule tasks for suite_scheduler.""" |
| 35 | |
| 36 | def get(self): |
| 37 | start_time = time_converter.pst_now() |
| 38 | suite_trigger = trigger_receiver.TriggerReceiver() |
| 39 | suite_trigger.cron() |
| 40 | end_time = time_converter.pst_now() |
| 41 | |
| 42 | calendar_client = rest_client.CalendarRestClient( |
| 43 | rest_client.BaseRestClient( |
| 44 | constants.RestClient.CALENDAR_CLIENT.scopes, |
| 45 | constants.RestClient.CALENDAR_CLIENT.service_name, |
| 46 | constants.RestClient.CALENDAR_CLIENT.service_version)) |
| 47 | |
| 48 | for keyword, results in suite_trigger.event_results.iteritems(): |
| 49 | # No finished tasks for the given keyword |
| 50 | if not results: |
| 51 | continue |
| 52 | |
| 53 | event = {} |
| 54 | event['summary'] = '%s Suite Tasks - Scheduled' % keyword |
| 55 | description = '%d scheduled tasks: \n' % len(results) |
| 56 | for task_name in results: |
| 57 | description += task_name + '\n' |
| 58 | |
| 59 | event['description'] = description |
| 60 | # The start time is exactly when the cron job is triggered, i.e. when |
| 61 | # all tasks of this event are pushed into the task queue. It's not |
| 62 | # when the tasks are 'really' kicked off in lab because there will be |
| 63 | # some time delay since task queue will schedule tasks batch by batch. |
| 64 | # However, the end time is not exactly when the cron job is finished, |
| 65 | # since this cron job's runtime is very short, but the calendar won't |
| 66 | # show any item that's shorter than 30 minutes. So no matter what |
| 67 | # endtime we set here, it will show a half-an-hour event. |
| 68 | event['start'] = { |
| 69 | 'dateTime': start_time.strftime(time_converter.CALENDAR_TIME_FORMAT), |
| 70 | 'timeZone': _CALENDAR_TIMEZONE, |
| 71 | } |
| 72 | event['end'] = { |
| 73 | 'dateTime': end_time.strftime(time_converter.CALENDAR_TIME_FORMAT), |
| 74 | 'timeZone': _CALENDAR_TIMEZONE, |
| 75 | } |
| 76 | |
| 77 | if constants.environment() == constants.RunningEnv.ENV_PROD: |
| 78 | if constants.application_id() == constants.AppID.PROD_APP: |
| 79 | calendar_client.add_event(_PROD_CALENDAR_ID_MAPPING[keyword], event) |
| 80 | else: |
| 81 | calendar_client.add_event(_STAGING_CALENDAR_ID_MAPPING[keyword], |
| 82 | event) |
| 83 | else: |
| 84 | logging.info(event) |
| 85 | |
| 86 | |
| 87 | class ExecuteTask(webapp2.RequestHandler): |
| 88 | """Run scheduled tasks regularly for suite_scheduler.""" |
| 89 | |
| 90 | def get(self): |
| 91 | task_processor = task_executor.TaskProcessor(task_executor.SUITES_QUEUE) |
| 92 | task_processor.batch_execute() |
| 93 | |
| 94 | |
| 95 | app = webapp2.WSGIApplication([ |
| 96 | ('/cron/trigger_event', TriggerEvent), |
| 97 | ('/cron/execute_task', ExecuteTask), |
| 98 | ], debug=True) |