blob: f0531247fc995a263e0f0919dcb7037622f61f3f [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
Prathmesh Prabhue8182312020-03-06 23:33:07 -08008import collections
Xixuan Wu835dee22017-09-07 10:47:29 -07009import logging
10
Xinan Lin3ba18a02019-08-13 15:44:55 -070011import buildbucket
12import constants
Xixuan Wu835dee22017-09-07 10:47:29 -070013
Xixuan Wu0a8d3ee2017-10-19 11:33:26 -070014import apiclient
Xixuan Wu835dee22017-09-07 10:47:29 -070015from google.appengine.api import taskqueue
16from google.appengine.runtime import apiproxy_errors
17
18
19SUITES_QUEUE = 'suitesQueue'
20BATCH_SIZE = 100
21
Prathmesh Prabhue8182312020-03-06 23:33:07 -080022Options = collections.namedtuple('Options',
23 ['batch_size', 'multirequest_size'])
24
25_DEFAULT_OPTIONS = Options(
26 batch_size=BATCH_SIZE,
27 multirequest_size=constants.Buildbucket.MULTIREQUEST_SIZE,
28)
29
Xixuan Wu835dee22017-09-07 10:47:29 -070030
31class TaskProcessor(object):
32 """A class capable of executing tasks by kicking off suites.
33
34 This class fetches tasks from pullqueue, and kicking off suites
35 represented by tasks' params through ChromeOS swarming proxy server.
36 """
37
Prathmesh Prabhue8182312020-03-06 23:33:07 -080038 def __init__(self, queue_name, options=_DEFAULT_OPTIONS):
Xixuan Wu835dee22017-09-07 10:47:29 -070039 """Initialize a task executor for further pulling & execution.
40
41 Args:
Xinan Lin3ba18a02019-08-13 15:44:55 -070042 queue_name: The name of a pull queue.
Prathmesh Prabhu7b961d52020-03-06 23:57:42 -080043 options: Options to configure the task processor.
Xixuan Wu835dee22017-09-07 10:47:29 -070044 """
Xixuan Wua5a29442017-10-11 11:03:02 -070045 self.queue = taskqueue.Queue(queue_name)
Prathmesh Prabhue8182312020-03-06 23:33:07 -080046 self._options = options
linxinane5eb4552019-08-26 05:44:45 +000047 # Schedule tests to PROD_BUILDER if in production project, otherwise use
48 # STAGING_BUILDER.
49 builder = constants.Buildbucket.STAGING_BUILDER
50 if (constants.environment() == constants.RunningEnv.ENV_PROD and
51 constants.application_id() == constants.AppID.PROD_APP):
52 builder = constants.Buildbucket.PROD_BUILDER
Xinan Lin3ba18a02019-08-13 15:44:55 -070053 self.test_platform_client = buildbucket.TestPlatformClient(
54 constants.Buildbucket.HOST,
55 constants.Buildbucket.PROJECT,
56 constants.Buildbucket.BUCKET,
linxinane5eb4552019-08-26 05:44:45 +000057 builder)
Xixuan Wu835dee22017-09-07 10:47:29 -070058
59 def batch_execute(self):
Craig Bergstrom58263d32018-04-26 14:11:35 -060060 """Execute tasks."""
Xinan Lin9e4917d2019-11-04 10:58:47 -080061 executed_tasks_count = 0
Prathmesh Prabhue8182312020-03-06 23:33:07 -080062 while (executed_tasks_count + self._options.multirequest_size <=
63 self._options.batch_size):
Xixuan Wu835dee22017-09-07 10:47:29 -070064 try:
Prathmesh Prabhue8182312020-03-06 23:33:07 -080065 tasks = self.queue.lease_tasks_by_tag(
66 3600, self._options.multirequest_size, deadline=60)
Xinan Lin9e4917d2019-11-04 10:58:47 -080067 except (taskqueue.UnknownQueueError,
68 taskqueue.TransientError,
69 apiproxy_errors.DeadlineExceededError) as e:
70 logging.exception(e)
71 raise
Xixuan Wua5a29442017-10-11 11:03:02 -070072
Xinan Lin9e4917d2019-11-04 10:58:47 -080073 executed_tasks = []
74 if not tasks:
75 return
76
77 try:
78 executed_tasks.extend(
79 self.test_platform_client.multirequest_run(tasks, tasks[0].tag))
80 except (ValueError,
81 buildbucket.BuildbucketRunError,
82 apiclient.errors.HttpError) as e:
83 logging.exception('Failed to kick off %d tasks for suite %s',
84 len(tasks), tasks[0].tag)
Xixuan Wu835dee22017-09-07 10:47:29 -070085 finally:
86 if executed_tasks:
Xinan Lin9e4917d2019-11-04 10:58:47 -080087 executed_tasks_count += len(executed_tasks)
88 logging.info('Successfully kicking %d tasks for suite %s',
89 len(executed_tasks), tasks[0].tag)
Xixuan Wua5a29442017-10-11 11:03:02 -070090 self.queue.delete_tasks(executed_tasks)
91
92 def purge(self):
93 """Purge the entire tasks in the task queue."""
94 self.queue.purge()
Xixuan Wu835dee22017-09-07 10:47:29 -070095
96
Xinan Lin9e4917d2019-11-04 10:58:47 -080097def push(queue_name, tag=None, **suite_kwargs):
Xixuan Wu835dee22017-09-07 10:47:29 -070098 """Push suites to suite queue for later kickoff.
99
100 Args:
101 queue_name: the name of a pull queue.
Xinan Lin9e4917d2019-11-04 10:58:47 -0800102 tag: tag of a pull queue task.
Xixuan Wu835dee22017-09-07 10:47:29 -0700103 **suite_kwargs: the args for a suite to kick off.
104 """
105 queue = taskqueue.Queue(queue_name)
Xinan Lin9e4917d2019-11-04 10:58:47 -0800106 queue.add(taskqueue.Task(method='PULL', tag=tag, params=suite_kwargs))