blob: e7edcdd4550003cc77417765502f180314daeeb7 [file] [log] [blame]
Xixuan Wu835dee22017-09-07 10:47:29 -07001# 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 Wu0a8d3ee2017-10-19 11:33:26 -07006# pylint: disable=g-bad-import-order
Xixuan Wu835dee22017-09-07 10:47:29 -07007
8import logging
9
Xixuan Wua5a29442017-10-11 11:03:02 -070010import global_config
Xixuan Wu835dee22017-09-07 10:47:29 -070011import swarming_lib
12
Xixuan Wu0a8d3ee2017-10-19 11:33:26 -070013import apiclient
Xixuan Wu835dee22017-09-07 10:47:29 -070014from google.appengine.api import taskqueue
15from google.appengine.runtime import apiproxy_errors
16
17
18SUITES_QUEUE = 'suitesQueue'
19BATCH_SIZE = 100
20
21
22class 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
Craig Bergstrom3212fb42018-04-17 19:24:15 -060029 def __init__(self, queue_name, afe_server):
Xixuan Wu835dee22017-09-07 10:47:29 -070030 """Initialize a task executor for further pulling & execution.
31
32 Args:
33 queue_name: the name of a pull queue.
Craig Bergstrom3212fb42018-04-17 19:24:15 -060034 afe_server: A str- the address of the afe_server.
Xixuan Wu835dee22017-09-07 10:47:29 -070035 """
Xixuan Wua5a29442017-10-11 11:03:02 -070036 self.queue = taskqueue.Queue(queue_name)
Craig Bergstrom3212fb42018-04-17 19:24:15 -060037 self.swarming = swarming_lib.SwarmingRunner(afe_server)
Xixuan Wu835dee22017-09-07 10:47:29 -070038
39 def batch_execute(self):
Craig Bergstrom58263d32018-04-26 14:11:35 -060040 """Execute tasks."""
Xixuan Wu835dee22017-09-07 10:47:29 -070041 try:
Xixuan Wua5a29442017-10-11 11:03:02 -070042 tasks = self.queue.lease_tasks(3600, BATCH_SIZE, deadline=60)
Xixuan Wu835dee22017-09-07 10:47:29 -070043 except (taskqueue.UnknownQueueError,
44 taskqueue.TransientError,
45 apiproxy_errors.DeadlineExceededError) as e:
46 logging.exception(e)
47 raise
48
49 if tasks:
50 executed_tasks = []
51 try:
52 for task in tasks:
Xixuan Wu0a8d3ee2017-10-19 11:33:26 -070053 try:
54 params = task.extract_params()
55 if global_config.GAE_TESTING:
56 self.swarming.dummy_run()
57 else:
58 self.swarming.run(**params)
Xixuan Wua5a29442017-10-11 11:03:02 -070059
Xixuan Wu0a8d3ee2017-10-19 11:33:26 -070060 executed_tasks.append(task)
61 except (ValueError, swarming_lib.SwarmingRunError,
62 apiclient.errors.HttpError) as e:
63 logging.exception('Failed to kick off %r', params)
Xixuan Wu835dee22017-09-07 10:47:29 -070064 finally:
65 if executed_tasks:
66 logging.info('Successfully kicking %d tasks', len(executed_tasks))
Xixuan Wua5a29442017-10-11 11:03:02 -070067 self.queue.delete_tasks(executed_tasks)
68
69 def purge(self):
70 """Purge the entire tasks in the task queue."""
71 self.queue.purge()
Xixuan Wu835dee22017-09-07 10:47:29 -070072
73
74def push(queue_name, **suite_kwargs):
75 """Push suites to suite queue for later kickoff.
76
77 Args:
78 queue_name: the name of a pull queue.
79 **suite_kwargs: the args for a suite to kick off.
80 """
81 queue = taskqueue.Queue(queue_name)
82 queue.add(taskqueue.Task(method='PULL', params=suite_kwargs))