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