blob: d1cfc5952f1bdf2e7ec997483c92764e090d447b [file] [log] [blame]
murgatroid993466c4b2016-01-12 10:26:04 -08001# Copyright 2015-2016, Google Inc.
Craig Tillerc2c79212015-02-16 12:00:01 -08002# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met:
7#
8# * Redistributions of source code must retain the above copyright
9# notice, this list of conditions and the following disclaimer.
10# * Redistributions in binary form must reproduce the above
11# copyright notice, this list of conditions and the following disclaimer
12# in the documentation and/or other materials provided with the
13# distribution.
14# * Neither the name of Google Inc. nor the names of its
15# contributors may be used to endorse or promote products derived from
16# this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
Nicolas Nobleddef2462015-01-06 18:08:25 -080030"""Run a group of subprocesses and then finish."""
31
Craig Tiller71735182015-01-15 17:07:13 -080032import hashlib
Nicolas Nobleddef2462015-01-06 18:08:25 -080033import multiprocessing
Craig Tiller71735182015-01-15 17:07:13 -080034import os
Craig Tiller5058c692015-04-08 09:42:04 -070035import platform
Craig Tiller5f735a62016-01-20 09:31:15 -080036import re
Craig Tiller336ad502015-02-24 14:46:02 -080037import signal
Nicolas Nobleddef2462015-01-06 18:08:25 -080038import subprocess
39import sys
ctiller3040cb72015-01-07 12:13:17 -080040import tempfile
41import time
Nicolas Nobleddef2462015-01-06 18:08:25 -080042
ctiller3040cb72015-01-07 12:13:17 -080043
Craig Tiller5f735a62016-01-20 09:31:15 -080044# cpu cost measurement
45measure_cpu_costs = False
46
47
ctiller94e5dde2015-01-09 10:41:59 -080048_DEFAULT_MAX_JOBS = 16 * multiprocessing.cpu_count()
Adele Zhoud01cbe32015-11-02 14:20:43 -080049_MAX_RESULT_SIZE = 8192
Nicolas Nobleddef2462015-01-06 18:08:25 -080050
Nicolas "Pixel" Noblef72d7b52015-12-03 03:07:43 +010051def platform_string():
52 if platform.system() == 'Windows':
53 return 'windows'
54 elif platform.system()[:7] == 'MSYS_NT':
55 return 'windows'
56 elif platform.system() == 'Darwin':
57 return 'mac'
58 elif platform.system() == 'Linux':
59 return 'linux'
60 else:
61 return 'posix'
62
Nicolas Nobleddef2462015-01-06 18:08:25 -080063
Craig Tiller336ad502015-02-24 14:46:02 -080064# setup a signal handler so that signal.pause registers 'something'
65# when a child finishes
66# not using futures and threading to avoid a dependency on subprocess32
Nicolas "Pixel" Noblef72d7b52015-12-03 03:07:43 +010067if platform_string() == 'windows':
Craig Tiller5058c692015-04-08 09:42:04 -070068 pass
69else:
70 have_alarm = False
71 def alarm_handler(unused_signum, unused_frame):
72 global have_alarm
73 have_alarm = False
74
75 signal.signal(signal.SIGCHLD, lambda unused_signum, unused_frame: None)
76 signal.signal(signal.SIGALRM, alarm_handler)
Craig Tiller336ad502015-02-24 14:46:02 -080077
78
ctiller3040cb72015-01-07 12:13:17 -080079_SUCCESS = object()
80_FAILURE = object()
81_RUNNING = object()
82_KILLED = object()
83
84
Craig Tiller3b083062015-01-12 13:51:28 -080085_COLORS = {
Nicolas Noble044db742015-01-14 16:57:24 -080086 'red': [ 31, 0 ],
87 'green': [ 32, 0 ],
88 'yellow': [ 33, 0 ],
89 'lightgray': [ 37, 0],
90 'gray': [ 30, 1 ],
Craig Tillerd7e09c32015-09-25 11:33:39 -070091 'purple': [ 35, 0 ],
Craig Tiller3b083062015-01-12 13:51:28 -080092 }
93
94
95_BEGINNING_OF_LINE = '\x1b[0G'
96_CLEAR_LINE = '\x1b[2K'
97
98
99_TAG_COLOR = {
100 'FAILED': 'red',
Craig Tillerd7e09c32015-09-25 11:33:39 -0700101 'FLAKE': 'purple',
Craig Tiller3dc1e4f2015-09-25 11:46:56 -0700102 'TIMEOUT_FLAKE': 'purple',
Masood Malekghassemie5f70022015-06-29 09:20:26 -0700103 'WARNING': 'yellow',
Craig Tillere1d0d1c2015-02-27 08:54:23 -0800104 'TIMEOUT': 'red',
Craig Tiller3b083062015-01-12 13:51:28 -0800105 'PASSED': 'green',
Nicolas Noble044db742015-01-14 16:57:24 -0800106 'START': 'gray',
Craig Tiller3b083062015-01-12 13:51:28 -0800107 'WAITING': 'yellow',
Nicolas Noble044db742015-01-14 16:57:24 -0800108 'SUCCESS': 'green',
109 'IDLE': 'gray',
Craig Tiller3b083062015-01-12 13:51:28 -0800110 }
111
112
Nicolas "Pixel" Noble99768ac2015-05-13 02:34:06 +0200113def message(tag, msg, explanatory_text=None, do_newline=False):
114 if message.old_tag == tag and message.old_msg == msg and not explanatory_text:
115 return
116 message.old_tag = tag
117 message.old_msg = msg
Craig Tiller23d2f3f2015-02-24 15:23:32 -0800118 try:
Nicolas "Pixel" Noblef72d7b52015-12-03 03:07:43 +0100119 if platform_string() == 'windows' or not sys.stdout.isatty():
Craig Tiller9f3b2d72015-08-25 11:50:57 -0700120 if explanatory_text:
121 print explanatory_text
122 print '%s: %s' % (tag, msg)
123 return
vjpaia29d2d72015-07-08 10:31:15 -0700124 sys.stdout.write('%s%s%s\x1b[%d;%dm%s\x1b[0m: %s%s' % (
125 _BEGINNING_OF_LINE,
126 _CLEAR_LINE,
127 '\n%s' % explanatory_text if explanatory_text is not None else '',
128 _COLORS[_TAG_COLOR[tag]][1],
129 _COLORS[_TAG_COLOR[tag]][0],
130 tag,
131 msg,
132 '\n' if do_newline or explanatory_text is not None else ''))
Craig Tiller23d2f3f2015-02-24 15:23:32 -0800133 sys.stdout.flush()
134 except:
135 pass
Craig Tiller3b083062015-01-12 13:51:28 -0800136
Adele Zhoue4c35612015-10-16 15:34:23 -0700137message.old_tag = ''
138message.old_msg = ''
Craig Tiller3b083062015-01-12 13:51:28 -0800139
Craig Tiller71735182015-01-15 17:07:13 -0800140def which(filename):
141 if '/' in filename:
142 return filename
143 for path in os.environ['PATH'].split(os.pathsep):
144 if os.path.exists(os.path.join(path, filename)):
145 return os.path.join(path, filename)
146 raise Exception('%s not found' % filename)
147
148
Craig Tiller547db2b2015-01-30 14:08:39 -0800149class JobSpec(object):
150 """Specifies what to run for a job."""
151
Jan Tattermusch725835a2015-08-01 21:02:35 -0700152 def __init__(self, cmdline, shortname=None, environ=None, hash_targets=None,
Craig Tiller95cc07b2015-09-28 13:41:30 -0700153 cwd=None, shell=False, timeout_seconds=5*60, flake_retries=0,
Jan Tattermuschb2758442016-03-28 09:32:20 -0700154 timeout_retries=0, kill_handler=None, cpu_cost=1.0,
155 verbose_success=False):
Craig Tiller547db2b2015-01-30 14:08:39 -0800156 """
157 Arguments:
158 cmdline: a list of arguments to pass as the command line
159 environ: a dictionary of environment variables to set in the child process
160 hash_targets: which files to include in the hash representing the jobs version
161 (or empty, indicating the job should not be hashed)
Jan Tattermusche2686282015-10-08 16:27:07 -0700162 kill_handler: a handler that will be called whenever job.kill() is invoked
Craig Tiller56c6b6a2016-01-20 08:27:37 -0800163 cpu_cost: number of cores per second this job needs
Craig Tiller547db2b2015-01-30 14:08:39 -0800164 """
murgatroid99132ce6a2015-03-04 17:29:14 -0800165 if environ is None:
166 environ = {}
167 if hash_targets is None:
168 hash_targets = []
Craig Tiller547db2b2015-01-30 14:08:39 -0800169 self.cmdline = cmdline
170 self.environ = environ
171 self.shortname = cmdline[0] if shortname is None else shortname
172 self.hash_targets = hash_targets or []
Craig Tiller5058c692015-04-08 09:42:04 -0700173 self.cwd = cwd
Jan Tattermusche8243592015-04-17 14:14:01 -0700174 self.shell = shell
Jan Tattermusch725835a2015-08-01 21:02:35 -0700175 self.timeout_seconds = timeout_seconds
Craig Tiller91318bc2015-09-24 08:58:39 -0700176 self.flake_retries = flake_retries
Craig Tillerbfc8a062015-09-28 14:40:21 -0700177 self.timeout_retries = timeout_retries
Jan Tattermusche2686282015-10-08 16:27:07 -0700178 self.kill_handler = kill_handler
Craig Tiller56c6b6a2016-01-20 08:27:37 -0800179 self.cpu_cost = cpu_cost
Jan Tattermuschb2758442016-03-28 09:32:20 -0700180 self.verbose_success = verbose_success
Craig Tiller547db2b2015-01-30 14:08:39 -0800181
182 def identity(self):
183 return '%r %r %r' % (self.cmdline, self.environ, self.hash_targets)
184
185 def __hash__(self):
186 return hash(self.identity())
187
188 def __cmp__(self, other):
189 return self.identity() == other.identity()
Craig Tillerdb218992016-01-05 09:28:46 -0800190
Jan Tattermusch2dd156e2015-12-04 18:26:17 -0800191 def __repr__(self):
192 return 'JobSpec(shortname=%s, cmdline=%s)' % (self.shortname, self.cmdline)
Craig Tiller547db2b2015-01-30 14:08:39 -0800193
194
Adele Zhoue4c35612015-10-16 15:34:23 -0700195class JobResult(object):
196 def __init__(self):
197 self.state = 'UNKNOWN'
198 self.returncode = -1
199 self.elapsed_time = 0
Adele Zhoud5fffa52015-10-23 15:51:42 -0700200 self.num_failures = 0
Adele Zhoue4c35612015-10-16 15:34:23 -0700201 self.retries = 0
202 self.message = ''
Craig Tillerdb218992016-01-05 09:28:46 -0800203
Adele Zhoue4c35612015-10-16 15:34:23 -0700204
ctiller3040cb72015-01-07 12:13:17 -0800205class Job(object):
206 """Manages one job."""
207
Adele Zhou2271ab52015-10-28 13:59:14 -0700208 def __init__(self, spec, bin_hash, newline_on_success, travis, add_env):
Craig Tiller547db2b2015-01-30 14:08:39 -0800209 self._spec = spec
Craig Tiller71735182015-01-15 17:07:13 -0800210 self._bin_hash = bin_hash
Nicolas Noble044db742015-01-14 16:57:24 -0800211 self._newline_on_success = newline_on_success
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100212 self._travis = travis
Craig Tiller91318bc2015-09-24 08:58:39 -0700213 self._add_env = add_env.copy()
Craig Tiller91318bc2015-09-24 08:58:39 -0700214 self._retries = 0
Craig Tiller95cc07b2015-09-28 13:41:30 -0700215 self._timeout_retries = 0
Jan Tattermusch91ad0182015-10-01 09:22:03 -0700216 self._suppress_failure_message = False
Craig Tillerb84728d2015-02-26 15:40:39 -0800217 message('START', spec.shortname, do_newline=self._travis)
Adele Zhoue4c35612015-10-16 15:34:23 -0700218 self.result = JobResult()
Craig Tiller91318bc2015-09-24 08:58:39 -0700219 self.start()
220
Adele Zhoue4c35612015-10-16 15:34:23 -0700221 def GetSpec(self):
222 return self._spec
223
Craig Tiller91318bc2015-09-24 08:58:39 -0700224 def start(self):
225 self._tempfile = tempfile.TemporaryFile()
226 env = dict(os.environ)
227 env.update(self._spec.environ)
228 env.update(self._add_env)
229 self._start = time.time()
Craig Tiller5f735a62016-01-20 09:31:15 -0800230 cmdline = self._spec.cmdline
231 if measure_cpu_costs:
232 cmdline = ['time', '--portability'] + cmdline
233 try_start = lambda: subprocess.Popen(args=cmdline,
Craig Tiller60078bb2015-11-04 16:39:54 -0800234 stderr=subprocess.STDOUT,
235 stdout=self._tempfile,
236 cwd=self._spec.cwd,
237 shell=self._spec.shell,
238 env=env)
239 delay = 0.3
240 for i in range(0, 4):
241 try:
242 self._process = try_start()
243 break
244 except OSError:
245 message('WARNING', 'Failed to start %s, retrying in %f seconds' % (self._spec.shortname, delay))
246 time.sleep(delay)
247 delay *= 2
248 else:
249 self._process = try_start()
Craig Tiller91318bc2015-09-24 08:58:39 -0700250 self._state = _RUNNING
ctiller3040cb72015-01-07 12:13:17 -0800251
Craig Tiller71735182015-01-15 17:07:13 -0800252 def state(self, update_cache):
ctiller3040cb72015-01-07 12:13:17 -0800253 """Poll current state of the job. Prints messages at completion."""
Craig Tillerdb218992016-01-05 09:28:46 -0800254 def stdout(self=self):
255 self._tempfile.seek(0)
256 stdout = self._tempfile.read()
257 self.result.message = stdout[-_MAX_RESULT_SIZE:]
258 return stdout
ctiller3040cb72015-01-07 12:13:17 -0800259 if self._state == _RUNNING and self._process.poll() is not None:
Craig Tiller9d6139a2015-02-26 15:24:43 -0800260 elapsed = time.time() - self._start
Adele Zhoue4c35612015-10-16 15:34:23 -0700261 self.result.elapsed_time = elapsed
ctiller3040cb72015-01-07 12:13:17 -0800262 if self._process.returncode != 0:
Craig Tiller91318bc2015-09-24 08:58:39 -0700263 if self._retries < self._spec.flake_retries:
264 message('FLAKE', '%s [ret=%d, pid=%d]' % (
Craig Tillerd0ffe142015-05-19 21:51:13 -0700265 self._spec.shortname, self._process.returncode, self._process.pid),
Craig Tillerdb218992016-01-05 09:28:46 -0800266 stdout(), do_newline=True)
Craig Tiller91318bc2015-09-24 08:58:39 -0700267 self._retries += 1
Adele Zhoud5fffa52015-10-23 15:51:42 -0700268 self.result.num_failures += 1
Adele Zhoue4c35612015-10-16 15:34:23 -0700269 self.result.retries = self._timeout_retries + self._retries
Craig Tiller91318bc2015-09-24 08:58:39 -0700270 self.start()
271 else:
272 self._state = _FAILURE
Jan Tattermusch91ad0182015-10-01 09:22:03 -0700273 if not self._suppress_failure_message:
274 message('FAILED', '%s [ret=%d, pid=%d]' % (
275 self._spec.shortname, self._process.returncode, self._process.pid),
Craig Tillerdb218992016-01-05 09:28:46 -0800276 stdout(), do_newline=True)
Adele Zhoue4c35612015-10-16 15:34:23 -0700277 self.result.state = 'FAILED'
Adele Zhoud5fffa52015-10-23 15:51:42 -0700278 self.result.num_failures += 1
Adele Zhoue4c35612015-10-16 15:34:23 -0700279 self.result.returncode = self._process.returncode
ctiller3040cb72015-01-07 12:13:17 -0800280 else:
281 self._state = _SUCCESS
Craig Tiller5f735a62016-01-20 09:31:15 -0800282 measurement = ''
283 if measure_cpu_costs:
284 m = re.search(r'real ([0-9.]+)\nuser ([0-9.]+)\nsys ([0-9.]+)', stdout())
285 real = float(m.group(1))
286 user = float(m.group(2))
287 sys = float(m.group(3))
288 if real > 0.5:
289 cores = (user + sys) / real
Craig Tillerbfe69362016-01-20 09:38:21 -0800290 measurement = '; cpu_cost=%.01f; estimated=%.01f' % (cores, self._spec.cpu_cost)
Craig Tiller5f735a62016-01-20 09:31:15 -0800291 message('PASSED', '%s [time=%.1fsec; retries=%d:%d%s]' % (
Jan Tattermuschb2758442016-03-28 09:32:20 -0700292 self._spec.shortname, elapsed, self._retries, self._timeout_retries, measurement),
293 stdout() if self._spec.verbose_success else None,
Craig Tiller95cc07b2015-09-28 13:41:30 -0700294 do_newline=self._newline_on_success or self._travis)
Adele Zhoue4c35612015-10-16 15:34:23 -0700295 self.result.state = 'PASSED'
Craig Tiller547db2b2015-01-30 14:08:39 -0800296 if self._bin_hash:
297 update_cache.finished(self._spec.identity(), self._bin_hash)
Craig Tiller5f735a62016-01-20 09:31:15 -0800298 elif (self._state == _RUNNING and
299 self._spec.timeout_seconds is not None and
Craig Tiller590105a2016-01-19 13:03:46 -0800300 time.time() - self._start > self._spec.timeout_seconds):
Craig Tiller95cc07b2015-09-28 13:41:30 -0700301 if self._timeout_retries < self._spec.timeout_retries:
Craig Tillerdb218992016-01-05 09:28:46 -0800302 message('TIMEOUT_FLAKE', '%s [pid=%d]' % (self._spec.shortname, self._process.pid), stdout(), do_newline=True)
Craig Tiller95cc07b2015-09-28 13:41:30 -0700303 self._timeout_retries += 1
Adele Zhoud5fffa52015-10-23 15:51:42 -0700304 self.result.num_failures += 1
Adele Zhoue4c35612015-10-16 15:34:23 -0700305 self.result.retries = self._timeout_retries + self._retries
Jan Tattermusch39e3cb32015-10-22 18:21:08 -0700306 if self._spec.kill_handler:
307 self._spec.kill_handler(self)
Craig Tiller3dc1e4f2015-09-25 11:46:56 -0700308 self._process.terminate()
309 self.start()
310 else:
Craig Tillerdb218992016-01-05 09:28:46 -0800311 message('TIMEOUT', '%s [pid=%d]' % (self._spec.shortname, self._process.pid), stdout(), do_newline=True)
Craig Tiller3dc1e4f2015-09-25 11:46:56 -0700312 self.kill()
Adele Zhoue4c35612015-10-16 15:34:23 -0700313 self.result.state = 'TIMEOUT'
Adele Zhoud5fffa52015-10-23 15:51:42 -0700314 self.result.num_failures += 1
ctiller3040cb72015-01-07 12:13:17 -0800315 return self._state
316
317 def kill(self):
318 if self._state == _RUNNING:
319 self._state = _KILLED
Jan Tattermusche2686282015-10-08 16:27:07 -0700320 if self._spec.kill_handler:
321 self._spec.kill_handler(self)
ctiller3040cb72015-01-07 12:13:17 -0800322 self._process.terminate()
323
Jan Tattermusch91ad0182015-10-01 09:22:03 -0700324 def suppress_failure_message(self):
325 self._suppress_failure_message = True
Craig Tillerdb218992016-01-05 09:28:46 -0800326
ctiller3040cb72015-01-07 12:13:17 -0800327
Nicolas Nobleddef2462015-01-06 18:08:25 -0800328class Jobset(object):
329 """Manages one run of jobs."""
330
Craig Tiller533b1a22015-05-29 08:41:29 -0700331 def __init__(self, check_cancelled, maxjobs, newline_on_success, travis,
Adele Zhou2271ab52015-10-28 13:59:14 -0700332 stop_on_failure, add_env, cache):
ctiller3040cb72015-01-07 12:13:17 -0800333 self._running = set()
334 self._check_cancelled = check_cancelled
335 self._cancelled = False
Nicolas Nobleddef2462015-01-06 18:08:25 -0800336 self._failures = 0
Craig Tiller738c3342015-01-12 14:28:33 -0800337 self._completed = 0
ctiller94e5dde2015-01-09 10:41:59 -0800338 self._maxjobs = maxjobs
Nicolas Noble044db742015-01-14 16:57:24 -0800339 self._newline_on_success = newline_on_success
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100340 self._travis = travis
Craig Tiller71735182015-01-15 17:07:13 -0800341 self._cache = cache
Craig Tiller533b1a22015-05-29 08:41:29 -0700342 self._stop_on_failure = stop_on_failure
Craig Tiller74e770d2015-06-11 09:38:09 -0700343 self._hashes = {}
Craig Tillerf53d9c82015-08-04 14:19:43 -0700344 self._add_env = add_env
Adele Zhoue4c35612015-10-16 15:34:23 -0700345 self.resultset = {}
Craig Tiller6364dcb2015-11-24 16:29:06 -0800346 self._remaining = None
347
348 def set_remaining(self, remaining):
349 self._remaining = remaining
350
Adele Zhoue4c35612015-10-16 15:34:23 -0700351 def get_num_failures(self):
Craig Tiller6364dcb2015-11-24 16:29:06 -0800352 return self._failures
Nicolas Nobleddef2462015-01-06 18:08:25 -0800353
Craig Tiller56c6b6a2016-01-20 08:27:37 -0800354 def cpu_cost(self):
355 c = 0
356 for job in self._running:
357 c += job._spec.cpu_cost
358 return c
359
Craig Tiller547db2b2015-01-30 14:08:39 -0800360 def start(self, spec):
ctiller3040cb72015-01-07 12:13:17 -0800361 """Start a job. Return True on success, False on failure."""
Craig Tiller56c6b6a2016-01-20 08:27:37 -0800362 while True:
ctiller3040cb72015-01-07 12:13:17 -0800363 if self.cancelled(): return False
Craig Tiller56c6b6a2016-01-20 08:27:37 -0800364 current_cpu_cost = self.cpu_cost()
365 if current_cpu_cost == 0: break
Craig Tiller3e301a32016-01-27 15:36:52 -0800366 if current_cpu_cost + spec.cpu_cost <= self._maxjobs: break
ctiller3040cb72015-01-07 12:13:17 -0800367 self.reap()
368 if self.cancelled(): return False
Craig Tiller547db2b2015-01-30 14:08:39 -0800369 if spec.hash_targets:
Craig Tiller74e770d2015-06-11 09:38:09 -0700370 if spec.identity() in self._hashes:
371 bin_hash = self._hashes[spec.identity()]
372 else:
373 bin_hash = hashlib.sha1()
374 for fn in spec.hash_targets:
375 with open(which(fn)) as f:
376 bin_hash.update(f.read())
377 bin_hash = bin_hash.hexdigest()
378 self._hashes[spec.identity()] = bin_hash
Craig Tiller547db2b2015-01-30 14:08:39 -0800379 should_run = self._cache.should_run(spec.identity(), bin_hash)
380 else:
381 bin_hash = None
382 should_run = True
383 if should_run:
Adele Zhoue4c35612015-10-16 15:34:23 -0700384 job = Job(spec,
385 bin_hash,
386 self._newline_on_success,
387 self._travis,
Adele Zhou2271ab52015-10-28 13:59:14 -0700388 self._add_env)
Adele Zhoue4c35612015-10-16 15:34:23 -0700389 self._running.add(job)
Jan Tattermusch494f3122016-03-01 13:55:42 -0800390 if not self.resultset.has_key(job.GetSpec().shortname):
391 self.resultset[job.GetSpec().shortname] = []
ctiller3040cb72015-01-07 12:13:17 -0800392 return True
Nicolas Nobleddef2462015-01-06 18:08:25 -0800393
ctiller3040cb72015-01-07 12:13:17 -0800394 def reap(self):
395 """Collect the dead jobs."""
396 while self._running:
397 dead = set()
398 for job in self._running:
Craig Tiller71735182015-01-15 17:07:13 -0800399 st = job.state(self._cache)
ctiller3040cb72015-01-07 12:13:17 -0800400 if st == _RUNNING: continue
Craig Tiller533b1a22015-05-29 08:41:29 -0700401 if st == _FAILURE or st == _KILLED:
402 self._failures += 1
403 if self._stop_on_failure:
404 self._cancelled = True
405 for job in self._running:
406 job.kill()
ctiller3040cb72015-01-07 12:13:17 -0800407 dead.add(job)
Craig Tiller74e770d2015-06-11 09:38:09 -0700408 break
ctiller3040cb72015-01-07 12:13:17 -0800409 for job in dead:
Craig Tiller738c3342015-01-12 14:28:33 -0800410 self._completed += 1
Adele Zhoud5fffa52015-10-23 15:51:42 -0700411 self.resultset[job.GetSpec().shortname].append(job.result)
ctiller3040cb72015-01-07 12:13:17 -0800412 self._running.remove(job)
Craig Tiller3b083062015-01-12 13:51:28 -0800413 if dead: return
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100414 if (not self._travis):
Craig Tiller2c23ad52015-12-04 06:50:38 -0800415 rstr = '' if self._remaining is None else '%d queued, ' % self._remaining
416 message('WAITING', '%s%d jobs running, %d complete, %d failed' % (
417 rstr, len(self._running), self._completed, self._failures))
Nicolas "Pixel" Noblef72d7b52015-12-03 03:07:43 +0100418 if platform_string() == 'windows':
Craig Tiller5058c692015-04-08 09:42:04 -0700419 time.sleep(0.1)
420 else:
421 global have_alarm
422 if not have_alarm:
423 have_alarm = True
424 signal.alarm(10)
425 signal.pause()
ctiller3040cb72015-01-07 12:13:17 -0800426
427 def cancelled(self):
428 """Poll for cancellation."""
429 if self._cancelled: return True
430 if not self._check_cancelled(): return False
431 for job in self._running:
432 job.kill()
433 self._cancelled = True
434 return True
435
436 def finish(self):
437 while self._running:
438 if self.cancelled(): pass # poll cancellation
439 self.reap()
440 return not self.cancelled() and self._failures == 0
Nicolas Nobleddef2462015-01-06 18:08:25 -0800441
442
ctiller3040cb72015-01-07 12:13:17 -0800443def _never_cancelled():
444 return False
445
446
Craig Tiller71735182015-01-15 17:07:13 -0800447# cache class that caches nothing
448class NoCache(object):
449 def should_run(self, cmdline, bin_hash):
450 return True
451
452 def finished(self, cmdline, bin_hash):
453 pass
454
455
Craig Tiller6364dcb2015-11-24 16:29:06 -0800456def tag_remaining(xs):
457 staging = []
458 for x in xs:
459 staging.append(x)
460 if len(staging) > 1000:
461 yield (staging.pop(0), None)
462 n = len(staging)
463 for i, x in enumerate(staging):
464 yield (x, n - i - 1)
465
466
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800467def run(cmdlines,
468 check_cancelled=_never_cancelled,
469 maxjobs=None,
Craig Tiller71735182015-01-15 17:07:13 -0800470 newline_on_success=False,
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100471 travis=False,
David Garcia Quintase90cd372015-05-31 18:15:26 -0700472 infinite_runs=False,
Craig Tiller533b1a22015-05-29 08:41:29 -0700473 stop_on_failure=False,
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +0200474 cache=None,
Craig Tillerf53d9c82015-08-04 14:19:43 -0700475 add_env={}):
ctiller94e5dde2015-01-09 10:41:59 -0800476 js = Jobset(check_cancelled,
Nicolas Noble044db742015-01-14 16:57:24 -0800477 maxjobs if maxjobs is not None else _DEFAULT_MAX_JOBS,
Craig Tillerf53d9c82015-08-04 14:19:43 -0700478 newline_on_success, travis, stop_on_failure, add_env,
Adele Zhou2271ab52015-10-28 13:59:14 -0700479 cache if cache is not None else NoCache())
Craig Tiller6364dcb2015-11-24 16:29:06 -0800480 for cmdline, remaining in tag_remaining(cmdlines):
ctiller3040cb72015-01-07 12:13:17 -0800481 if not js.start(cmdline):
482 break
Craig Tiller6364dcb2015-11-24 16:29:06 -0800483 if remaining is not None:
484 js.set_remaining(remaining)
485 js.finish()
Adele Zhoue4c35612015-10-16 15:34:23 -0700486 return js.get_num_failures(), js.resultset