blob: 9dacbef94b93ec60e55ed286a4b24c60abb94be9 [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
Xinan Lin3ba18a02019-08-13 15:44:55 -070010import buildbucket
11import constants
Xixuan Wua5a29442017-10-11 11:03:02 -070012import global_config
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
22
23class 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 Lin9e4917d2019-11-04 10:58:47 -080030 def __init__(self, queue_name):
Xixuan Wu835dee22017-09-07 10:47:29 -070031 """Initialize a task executor for further pulling & execution.
32
33 Args:
Xinan Lin3ba18a02019-08-13 15:44:55 -070034 queue_name: The name of a pull queue.
Xixuan Wu835dee22017-09-07 10:47:29 -070035 """
Xixuan Wua5a29442017-10-11 11:03:02 -070036 self.queue = taskqueue.Queue(queue_name)
linxinane5eb4552019-08-26 05:44:45 +000037 # 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 Lin3ba18a02019-08-13 15:44:55 -070043 self.test_platform_client = buildbucket.TestPlatformClient(
44 constants.Buildbucket.HOST,
45 constants.Buildbucket.PROJECT,
46 constants.Buildbucket.BUCKET,
linxinane5eb4552019-08-26 05:44:45 +000047 builder)
Xixuan Wu835dee22017-09-07 10:47:29 -070048
49 def batch_execute(self):
Craig Bergstrom58263d32018-04-26 14:11:35 -060050 """Execute tasks."""
Xinan Lin9e4917d2019-11-04 10:58:47 -080051 executed_tasks_count = 0
52 while (executed_tasks_count +
53 constants.Buildbucket.MULTIREQUEST_SIZE <= BATCH_SIZE):
Xixuan Wu835dee22017-09-07 10:47:29 -070054 try:
Xinan Lin9e4917d2019-11-04 10:58:47 -080055 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 Wua5a29442017-10-11 11:03:02 -070062
Xinan Lin9e4917d2019-11-04 10:58:47 -080063 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 Wu835dee22017-09-07 10:47:29 -070075 finally:
76 if executed_tasks:
Xinan Lin9e4917d2019-11-04 10:58:47 -080077 executed_tasks_count += len(executed_tasks)
78 logging.info('Successfully kicking %d tasks for suite %s',
79 len(executed_tasks), tasks[0].tag)
Xixuan Wua5a29442017-10-11 11:03:02 -070080 self.queue.delete_tasks(executed_tasks)
81
Xinan Lin3ba18a02019-08-13 15:44:55 -070082
Xixuan Wua5a29442017-10-11 11:03:02 -070083 def purge(self):
84 """Purge the entire tasks in the task queue."""
85 self.queue.purge()
Xixuan Wu835dee22017-09-07 10:47:29 -070086
87
Xinan Lin9e4917d2019-11-04 10:58:47 -080088def push(queue_name, tag=None, **suite_kwargs):
Xixuan Wu835dee22017-09-07 10:47:29 -070089 """Push suites to suite queue for later kickoff.
90
91 Args:
92 queue_name: the name of a pull queue.
Xinan Lin9e4917d2019-11-04 10:58:47 -080093 tag: tag of a pull queue task.
Xixuan Wu835dee22017-09-07 10:47:29 -070094 **suite_kwargs: the args for a suite to kick off.
95 """
96 queue = taskqueue.Queue(queue_name)
Xinan Lin9e4917d2019-11-04 10:58:47 -080097 queue.add(taskqueue.Task(method='PULL', tag=tag, params=suite_kwargs))