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