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 | |
Xixuan Wu | a5a2944 | 2017-10-11 11:03:02 -0700 | [diff] [blame] | 10 | import global_config |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 11 | import swarming_lib |
| 12 | |
Xixuan Wu | 0a8d3ee | 2017-10-19 11:33:26 -0700 | [diff] [blame] | 13 | import apiclient |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 14 | from google.appengine.api import taskqueue |
| 15 | from google.appengine.runtime import apiproxy_errors |
| 16 | |
| 17 | |
| 18 | SUITES_QUEUE = 'suitesQueue' |
| 19 | BATCH_SIZE = 100 |
| 20 | |
| 21 | |
| 22 | class TaskProcessor(object): |
| 23 | """A class capable of executing tasks by kicking off suites. |
| 24 | |
| 25 | This class fetches tasks from pullqueue, and kicking off suites |
| 26 | represented by tasks' params through ChromeOS swarming proxy server. |
| 27 | """ |
| 28 | |
Xixuan Wu | 5d700dc | 2018-08-21 15:13:10 -0700 | [diff] [blame] | 29 | def __init__(self, queue_name, afe_server, skylab_swarming_server): |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 30 | """Initialize a task executor for further pulling & execution. |
| 31 | |
| 32 | Args: |
| 33 | queue_name: the name of a pull queue. |
Craig Bergstrom | 3212fb4 | 2018-04-17 19:24:15 -0600 | [diff] [blame] | 34 | afe_server: A str- the address of the afe_server. |
Xixuan Wu | 5d700dc | 2018-08-21 15:13:10 -0700 | [diff] [blame] | 35 | skylab_swarming_server: A string of swarming server url for skylab. |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 36 | """ |
Xixuan Wu | a5a2944 | 2017-10-11 11:03:02 -0700 | [diff] [blame] | 37 | self.queue = taskqueue.Queue(queue_name) |
Xixuan Wu | 5d700dc | 2018-08-21 15:13:10 -0700 | [diff] [blame] | 38 | self.swarming = swarming_lib.SwarmingRunner( |
| 39 | afe_server, skylab_swarming_server) |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 40 | |
| 41 | def batch_execute(self): |
Craig Bergstrom | 58263d3 | 2018-04-26 14:11:35 -0600 | [diff] [blame] | 42 | """Execute tasks.""" |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 43 | try: |
Xixuan Wu | a5a2944 | 2017-10-11 11:03:02 -0700 | [diff] [blame] | 44 | tasks = self.queue.lease_tasks(3600, BATCH_SIZE, deadline=60) |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 45 | except (taskqueue.UnknownQueueError, |
| 46 | taskqueue.TransientError, |
| 47 | apiproxy_errors.DeadlineExceededError) as e: |
| 48 | logging.exception(e) |
| 49 | raise |
| 50 | |
| 51 | if tasks: |
| 52 | executed_tasks = [] |
| 53 | try: |
| 54 | for task in tasks: |
Xixuan Wu | 0a8d3ee | 2017-10-19 11:33:26 -0700 | [diff] [blame] | 55 | try: |
| 56 | params = task.extract_params() |
| 57 | if global_config.GAE_TESTING: |
| 58 | self.swarming.dummy_run() |
Xixuan Wu | a17e328 | 2018-08-21 16:11:11 -0700 | [diff] [blame^] | 59 | self.swarming.dummy_run(is_skylab=True) |
Xixuan Wu | 0a8d3ee | 2017-10-19 11:33:26 -0700 | [diff] [blame] | 60 | else: |
| 61 | self.swarming.run(**params) |
Xixuan Wu | a5a2944 | 2017-10-11 11:03:02 -0700 | [diff] [blame] | 62 | |
Xixuan Wu | 0a8d3ee | 2017-10-19 11:33:26 -0700 | [diff] [blame] | 63 | executed_tasks.append(task) |
| 64 | except (ValueError, swarming_lib.SwarmingRunError, |
| 65 | apiclient.errors.HttpError) as e: |
| 66 | logging.exception('Failed to kick off %r', params) |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 67 | finally: |
| 68 | if executed_tasks: |
| 69 | logging.info('Successfully kicking %d tasks', len(executed_tasks)) |
Xixuan Wu | a5a2944 | 2017-10-11 11:03:02 -0700 | [diff] [blame] | 70 | self.queue.delete_tasks(executed_tasks) |
| 71 | |
| 72 | def purge(self): |
| 73 | """Purge the entire tasks in the task queue.""" |
| 74 | self.queue.purge() |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 75 | |
| 76 | |
| 77 | def push(queue_name, **suite_kwargs): |
| 78 | """Push suites to suite queue for later kickoff. |
| 79 | |
| 80 | Args: |
| 81 | queue_name: the name of a pull queue. |
| 82 | **suite_kwargs: the args for a suite to kick off. |
| 83 | """ |
| 84 | queue = taskqueue.Queue(queue_name) |
| 85 | queue.add(taskqueue.Task(method='PULL', params=suite_kwargs)) |