Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -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 executing tasks queued by suite scheduler.""" |
Xixuan Wu | 0a8d3ee | 2017-10-19 11:33:26 -0700 | [diff] [blame] | 6 | # pylint: disable=g-bad-import-order |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 7 | |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 8 | import ast |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 9 | import logging |
| 10 | |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 11 | import buildbucket |
| 12 | import constants |
Xixuan Wu | a5a2944 | 2017-10-11 11:03:02 -0700 | [diff] [blame] | 13 | import global_config |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 14 | import swarming_lib |
| 15 | |
Xixuan Wu | 0a8d3ee | 2017-10-19 11:33:26 -0700 | [diff] [blame] | 16 | import apiclient |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 17 | from google.appengine.api import taskqueue |
| 18 | from google.appengine.runtime import apiproxy_errors |
| 19 | |
| 20 | |
| 21 | SUITES_QUEUE = 'suitesQueue' |
| 22 | BATCH_SIZE = 100 |
| 23 | |
| 24 | |
| 25 | class TaskProcessor(object): |
| 26 | """A class capable of executing tasks by kicking off suites. |
| 27 | |
| 28 | This class fetches tasks from pullqueue, and kicking off suites |
| 29 | represented by tasks' params through ChromeOS swarming proxy server. |
| 30 | """ |
| 31 | |
Xixuan Wu | 5d700dc | 2018-08-21 15:13:10 -0700 | [diff] [blame] | 32 | def __init__(self, queue_name, afe_server, skylab_swarming_server): |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 33 | """Initialize a task executor for further pulling & execution. |
| 34 | |
| 35 | Args: |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 36 | queue_name: The name of a pull queue. |
| 37 | afe_server: A string of the address for the afe_server. |
Xixuan Wu | 5d700dc | 2018-08-21 15:13:10 -0700 | [diff] [blame] | 38 | skylab_swarming_server: A string of swarming server url for skylab. |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 39 | """ |
Xixuan Wu | a5a2944 | 2017-10-11 11:03:02 -0700 | [diff] [blame] | 40 | self.queue = taskqueue.Queue(queue_name) |
Xixuan Wu | 5d700dc | 2018-08-21 15:13:10 -0700 | [diff] [blame] | 41 | self.swarming = swarming_lib.SwarmingRunner( |
| 42 | afe_server, skylab_swarming_server) |
linxinan | e5eb455 | 2019-08-26 05:44:45 +0000 | [diff] [blame] | 43 | # Schedule tests to PROD_BUILDER if in production project, otherwise use |
| 44 | # STAGING_BUILDER. |
| 45 | builder = constants.Buildbucket.STAGING_BUILDER |
| 46 | if (constants.environment() == constants.RunningEnv.ENV_PROD and |
| 47 | constants.application_id() == constants.AppID.PROD_APP): |
| 48 | builder = constants.Buildbucket.PROD_BUILDER |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 49 | self.test_platform_client = buildbucket.TestPlatformClient( |
| 50 | constants.Buildbucket.HOST, |
| 51 | constants.Buildbucket.PROJECT, |
| 52 | constants.Buildbucket.BUCKET, |
linxinan | e5eb455 | 2019-08-26 05:44:45 +0000 | [diff] [blame] | 53 | builder) |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 54 | |
| 55 | def batch_execute(self): |
Craig Bergstrom | 58263d3 | 2018-04-26 14:11:35 -0600 | [diff] [blame] | 56 | """Execute tasks.""" |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 57 | try: |
Xixuan Wu | a5a2944 | 2017-10-11 11:03:02 -0700 | [diff] [blame] | 58 | tasks = self.queue.lease_tasks(3600, BATCH_SIZE, deadline=60) |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 59 | except (taskqueue.UnknownQueueError, |
| 60 | taskqueue.TransientError, |
| 61 | apiproxy_errors.DeadlineExceededError) as e: |
| 62 | logging.exception(e) |
| 63 | raise |
| 64 | |
| 65 | if tasks: |
| 66 | executed_tasks = [] |
| 67 | try: |
| 68 | for task in tasks: |
Xixuan Wu | 0a8d3ee | 2017-10-19 11:33:26 -0700 | [diff] [blame] | 69 | try: |
| 70 | params = task.extract_params() |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 71 | self._execute(**params) |
Xixuan Wu | a5a2944 | 2017-10-11 11:03:02 -0700 | [diff] [blame] | 72 | |
Xixuan Wu | 0a8d3ee | 2017-10-19 11:33:26 -0700 | [diff] [blame] | 73 | executed_tasks.append(task) |
| 74 | except (ValueError, swarming_lib.SwarmingRunError, |
| 75 | apiclient.errors.HttpError) as e: |
| 76 | logging.exception('Failed to kick off %r', params) |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 77 | finally: |
| 78 | if executed_tasks: |
| 79 | logging.info('Successfully kicking %d tasks', len(executed_tasks)) |
Xixuan Wu | a5a2944 | 2017-10-11 11:03:02 -0700 | [diff] [blame] | 80 | self.queue.delete_tasks(executed_tasks) |
| 81 | |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 82 | def _execute(self, **params): |
| 83 | """A wrapper to execute each single task.""" |
linxinan | e5eb455 | 2019-08-26 05:44:45 +0000 | [diff] [blame] | 84 | # No dummy run for frontdoor enabled test. |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 85 | is_frontdoor = ast.literal_eval(params.get('is_frontdoor', 'False')) |
linxinan | e5eb455 | 2019-08-26 05:44:45 +0000 | [diff] [blame] | 86 | if is_frontdoor: |
| 87 | return self.test_platform_client.run(**params) |
| 88 | |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 89 | # Test environment. |
| 90 | if global_config.GAE_TESTING: |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 91 | self.swarming.dummy_run(**params) |
| 92 | return |
| 93 | # Production environment. |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 94 | return self.swarming.run(**params) |
| 95 | |
Xixuan Wu | a5a2944 | 2017-10-11 11:03:02 -0700 | [diff] [blame] | 96 | def purge(self): |
| 97 | """Purge the entire tasks in the task queue.""" |
| 98 | self.queue.purge() |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 99 | |
| 100 | |
| 101 | def push(queue_name, **suite_kwargs): |
| 102 | """Push suites to suite queue for later kickoff. |
| 103 | |
| 104 | Args: |
| 105 | queue_name: the name of a pull queue. |
| 106 | **suite_kwargs: the args for a suite to kick off. |
| 107 | """ |
| 108 | queue = taskqueue.Queue(queue_name) |
| 109 | queue.add(taskqueue.Task(method='PULL', params=suite_kwargs)) |