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 | |
Prathmesh Prabhu | e818231 | 2020-03-06 23:33:07 -0800 | [diff] [blame] | 8 | import collections |
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 | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 13 | |
Xixuan Wu | 0a8d3ee | 2017-10-19 11:33:26 -0700 | [diff] [blame] | 14 | import apiclient |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 15 | from google.appengine.api import taskqueue |
| 16 | from google.appengine.runtime import apiproxy_errors |
| 17 | |
| 18 | |
| 19 | SUITES_QUEUE = 'suitesQueue' |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 20 | |
Prathmesh Prabhu | e818231 | 2020-03-06 23:33:07 -0800 | [diff] [blame] | 21 | Options = collections.namedtuple('Options', |
| 22 | ['batch_size', 'multirequest_size']) |
| 23 | |
| 24 | _DEFAULT_OPTIONS = Options( |
Prathmesh Prabhu | c23748e | 2020-03-07 00:00:51 -0800 | [diff] [blame] | 25 | batch_size=100, |
Prathmesh Prabhu | e818231 | 2020-03-06 23:33:07 -0800 | [diff] [blame] | 26 | multirequest_size=constants.Buildbucket.MULTIREQUEST_SIZE, |
| 27 | ) |
| 28 | |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 29 | |
| 30 | class TaskProcessor(object): |
| 31 | """A class capable of executing tasks by kicking off suites. |
| 32 | |
| 33 | This class fetches tasks from pullqueue, and kicking off suites |
| 34 | represented by tasks' params through ChromeOS swarming proxy server. |
| 35 | """ |
| 36 | |
Prathmesh Prabhu | e818231 | 2020-03-06 23:33:07 -0800 | [diff] [blame] | 37 | def __init__(self, queue_name, options=_DEFAULT_OPTIONS): |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 38 | """Initialize a task executor for further pulling & execution. |
| 39 | |
| 40 | Args: |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 41 | queue_name: The name of a pull queue. |
Prathmesh Prabhu | 7b961d5 | 2020-03-06 23:57:42 -0800 | [diff] [blame] | 42 | options: Options to configure the task processor. |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 43 | """ |
Xixuan Wu | a5a2944 | 2017-10-11 11:03:02 -0700 | [diff] [blame] | 44 | self.queue = taskqueue.Queue(queue_name) |
Prathmesh Prabhu | e818231 | 2020-03-06 23:33:07 -0800 | [diff] [blame] | 45 | self._options = options |
linxinan | e5eb455 | 2019-08-26 05:44:45 +0000 | [diff] [blame] | 46 | # Schedule tests to PROD_BUILDER if in production project, otherwise use |
| 47 | # STAGING_BUILDER. |
| 48 | builder = constants.Buildbucket.STAGING_BUILDER |
| 49 | if (constants.environment() == constants.RunningEnv.ENV_PROD and |
| 50 | constants.application_id() == constants.AppID.PROD_APP): |
| 51 | builder = constants.Buildbucket.PROD_BUILDER |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 52 | self.test_platform_client = buildbucket.TestPlatformClient( |
| 53 | constants.Buildbucket.HOST, |
| 54 | constants.Buildbucket.PROJECT, |
| 55 | constants.Buildbucket.BUCKET, |
linxinan | e5eb455 | 2019-08-26 05:44:45 +0000 | [diff] [blame] | 56 | builder) |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 57 | |
| 58 | def batch_execute(self): |
Craig Bergstrom | 58263d3 | 2018-04-26 14:11:35 -0600 | [diff] [blame] | 59 | """Execute tasks.""" |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 60 | executed_tasks_count = 0 |
Prathmesh Prabhu | e818231 | 2020-03-06 23:33:07 -0800 | [diff] [blame] | 61 | while (executed_tasks_count + self._options.multirequest_size <= |
| 62 | self._options.batch_size): |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 63 | try: |
Prathmesh Prabhu | e818231 | 2020-03-06 23:33:07 -0800 | [diff] [blame] | 64 | tasks = self.queue.lease_tasks_by_tag( |
| 65 | 3600, self._options.multirequest_size, deadline=60) |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 66 | except (taskqueue.UnknownQueueError, |
| 67 | taskqueue.TransientError, |
| 68 | apiproxy_errors.DeadlineExceededError) as e: |
| 69 | logging.exception(e) |
| 70 | raise |
Xixuan Wu | a5a2944 | 2017-10-11 11:03:02 -0700 | [diff] [blame] | 71 | |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 72 | executed_tasks = [] |
| 73 | if not tasks: |
| 74 | return |
| 75 | |
| 76 | try: |
| 77 | executed_tasks.extend( |
| 78 | self.test_platform_client.multirequest_run(tasks, tasks[0].tag)) |
| 79 | except (ValueError, |
| 80 | buildbucket.BuildbucketRunError, |
| 81 | apiclient.errors.HttpError) as e: |
| 82 | logging.exception('Failed to kick off %d tasks for suite %s', |
| 83 | len(tasks), tasks[0].tag) |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 84 | finally: |
| 85 | if executed_tasks: |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 86 | executed_tasks_count += len(executed_tasks) |
| 87 | logging.info('Successfully kicking %d tasks for suite %s', |
| 88 | len(executed_tasks), tasks[0].tag) |
Xixuan Wu | a5a2944 | 2017-10-11 11:03:02 -0700 | [diff] [blame] | 89 | self.queue.delete_tasks(executed_tasks) |
| 90 | |
| 91 | def purge(self): |
| 92 | """Purge the entire tasks in the task queue.""" |
| 93 | self.queue.purge() |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 94 | |
| 95 | |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 96 | def push(queue_name, tag=None, **suite_kwargs): |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 97 | """Push suites to suite queue for later kickoff. |
| 98 | |
| 99 | Args: |
| 100 | queue_name: the name of a pull queue. |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 101 | tag: tag of a pull queue task. |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 102 | **suite_kwargs: the args for a suite to kick off. |
| 103 | """ |
| 104 | queue = taskqueue.Queue(queue_name) |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 105 | queue.add(taskqueue.Task(method='PULL', tag=tag, params=suite_kwargs)) |