blob: b01268660d6e3a9e7bdc47499330963861d159ed [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 Tiller336ad502015-02-24 14:46:02 -080036import signal
Nicolas Nobleddef2462015-01-06 18:08:25 -080037import subprocess
38import sys
ctiller3040cb72015-01-07 12:13:17 -080039import tempfile
40import time
Nicolas Nobleddef2462015-01-06 18:08:25 -080041
ctiller3040cb72015-01-07 12:13:17 -080042
ctiller94e5dde2015-01-09 10:41:59 -080043_DEFAULT_MAX_JOBS = 16 * multiprocessing.cpu_count()
Adele Zhoud01cbe32015-11-02 14:20:43 -080044_MAX_RESULT_SIZE = 8192
Nicolas Nobleddef2462015-01-06 18:08:25 -080045
Nicolas "Pixel" Noblef72d7b52015-12-03 03:07:43 +010046def platform_string():
47 if platform.system() == 'Windows':
48 return 'windows'
49 elif platform.system()[:7] == 'MSYS_NT':
50 return 'windows'
51 elif platform.system() == 'Darwin':
52 return 'mac'
53 elif platform.system() == 'Linux':
54 return 'linux'
55 else:
56 return 'posix'
57
Nicolas Nobleddef2462015-01-06 18:08:25 -080058
Craig Tiller336ad502015-02-24 14:46:02 -080059# setup a signal handler so that signal.pause registers 'something'
60# when a child finishes
61# not using futures and threading to avoid a dependency on subprocess32
Nicolas "Pixel" Noblef72d7b52015-12-03 03:07:43 +010062if platform_string() == 'windows':
Craig Tiller5058c692015-04-08 09:42:04 -070063 pass
64else:
65 have_alarm = False
66 def alarm_handler(unused_signum, unused_frame):
67 global have_alarm
68 have_alarm = False
69
70 signal.signal(signal.SIGCHLD, lambda unused_signum, unused_frame: None)
71 signal.signal(signal.SIGALRM, alarm_handler)
Craig Tiller336ad502015-02-24 14:46:02 -080072
73
ctiller3040cb72015-01-07 12:13:17 -080074_SUCCESS = object()
75_FAILURE = object()
76_RUNNING = object()
77_KILLED = object()
78
79
Craig Tiller3b083062015-01-12 13:51:28 -080080_COLORS = {
Nicolas Noble044db742015-01-14 16:57:24 -080081 'red': [ 31, 0 ],
82 'green': [ 32, 0 ],
83 'yellow': [ 33, 0 ],
84 'lightgray': [ 37, 0],
85 'gray': [ 30, 1 ],
Craig Tillerd7e09c32015-09-25 11:33:39 -070086 'purple': [ 35, 0 ],
Craig Tiller3b083062015-01-12 13:51:28 -080087 }
88
89
90_BEGINNING_OF_LINE = '\x1b[0G'
91_CLEAR_LINE = '\x1b[2K'
92
93
94_TAG_COLOR = {
95 'FAILED': 'red',
Craig Tillerd7e09c32015-09-25 11:33:39 -070096 'FLAKE': 'purple',
Craig Tiller3dc1e4f2015-09-25 11:46:56 -070097 'TIMEOUT_FLAKE': 'purple',
Masood Malekghassemie5f70022015-06-29 09:20:26 -070098 'WARNING': 'yellow',
Craig Tillere1d0d1c2015-02-27 08:54:23 -080099 'TIMEOUT': 'red',
Craig Tiller3b083062015-01-12 13:51:28 -0800100 'PASSED': 'green',
Nicolas Noble044db742015-01-14 16:57:24 -0800101 'START': 'gray',
Craig Tiller3b083062015-01-12 13:51:28 -0800102 'WAITING': 'yellow',
Nicolas Noble044db742015-01-14 16:57:24 -0800103 'SUCCESS': 'green',
104 'IDLE': 'gray',
Craig Tiller3b083062015-01-12 13:51:28 -0800105 }
106
107
Nicolas "Pixel" Noble99768ac2015-05-13 02:34:06 +0200108def message(tag, msg, explanatory_text=None, do_newline=False):
109 if message.old_tag == tag and message.old_msg == msg and not explanatory_text:
110 return
111 message.old_tag = tag
112 message.old_msg = msg
Craig Tiller23d2f3f2015-02-24 15:23:32 -0800113 try:
Nicolas "Pixel" Noblef72d7b52015-12-03 03:07:43 +0100114 if platform_string() == 'windows' or not sys.stdout.isatty():
Craig Tiller9f3b2d72015-08-25 11:50:57 -0700115 if explanatory_text:
116 print explanatory_text
117 print '%s: %s' % (tag, msg)
118 return
vjpaia29d2d72015-07-08 10:31:15 -0700119 sys.stdout.write('%s%s%s\x1b[%d;%dm%s\x1b[0m: %s%s' % (
120 _BEGINNING_OF_LINE,
121 _CLEAR_LINE,
122 '\n%s' % explanatory_text if explanatory_text is not None else '',
123 _COLORS[_TAG_COLOR[tag]][1],
124 _COLORS[_TAG_COLOR[tag]][0],
125 tag,
126 msg,
127 '\n' if do_newline or explanatory_text is not None else ''))
Craig Tiller23d2f3f2015-02-24 15:23:32 -0800128 sys.stdout.flush()
129 except:
130 pass
Craig Tiller3b083062015-01-12 13:51:28 -0800131
Adele Zhoue4c35612015-10-16 15:34:23 -0700132message.old_tag = ''
133message.old_msg = ''
Craig Tiller3b083062015-01-12 13:51:28 -0800134
Craig Tiller71735182015-01-15 17:07:13 -0800135def which(filename):
136 if '/' in filename:
137 return filename
138 for path in os.environ['PATH'].split(os.pathsep):
139 if os.path.exists(os.path.join(path, filename)):
140 return os.path.join(path, filename)
141 raise Exception('%s not found' % filename)
142
143
Craig Tiller547db2b2015-01-30 14:08:39 -0800144class JobSpec(object):
145 """Specifies what to run for a job."""
146
Jan Tattermusch725835a2015-08-01 21:02:35 -0700147 def __init__(self, cmdline, shortname=None, environ=None, hash_targets=None,
Craig Tiller95cc07b2015-09-28 13:41:30 -0700148 cwd=None, shell=False, timeout_seconds=5*60, flake_retries=0,
Craig Tiller56c6b6a2016-01-20 08:27:37 -0800149 timeout_retries=0, kill_handler=None, cpu_cost=1.0):
Craig Tiller547db2b2015-01-30 14:08:39 -0800150 """
151 Arguments:
152 cmdline: a list of arguments to pass as the command line
153 environ: a dictionary of environment variables to set in the child process
154 hash_targets: which files to include in the hash representing the jobs version
155 (or empty, indicating the job should not be hashed)
Jan Tattermusche2686282015-10-08 16:27:07 -0700156 kill_handler: a handler that will be called whenever job.kill() is invoked
Craig Tiller56c6b6a2016-01-20 08:27:37 -0800157 cpu_cost: number of cores per second this job needs
Craig Tiller547db2b2015-01-30 14:08:39 -0800158 """
murgatroid99132ce6a2015-03-04 17:29:14 -0800159 if environ is None:
160 environ = {}
161 if hash_targets is None:
162 hash_targets = []
Craig Tiller547db2b2015-01-30 14:08:39 -0800163 self.cmdline = cmdline
164 self.environ = environ
165 self.shortname = cmdline[0] if shortname is None else shortname
166 self.hash_targets = hash_targets or []
Craig Tiller5058c692015-04-08 09:42:04 -0700167 self.cwd = cwd
Jan Tattermusche8243592015-04-17 14:14:01 -0700168 self.shell = shell
Jan Tattermusch725835a2015-08-01 21:02:35 -0700169 self.timeout_seconds = timeout_seconds
Craig Tiller91318bc2015-09-24 08:58:39 -0700170 self.flake_retries = flake_retries
Craig Tillerbfc8a062015-09-28 14:40:21 -0700171 self.timeout_retries = timeout_retries
Jan Tattermusche2686282015-10-08 16:27:07 -0700172 self.kill_handler = kill_handler
Craig Tiller56c6b6a2016-01-20 08:27:37 -0800173 self.cpu_cost = cpu_cost
Craig Tiller547db2b2015-01-30 14:08:39 -0800174
175 def identity(self):
176 return '%r %r %r' % (self.cmdline, self.environ, self.hash_targets)
177
178 def __hash__(self):
179 return hash(self.identity())
180
181 def __cmp__(self, other):
182 return self.identity() == other.identity()
Craig Tillerdb218992016-01-05 09:28:46 -0800183
Jan Tattermusch2dd156e2015-12-04 18:26:17 -0800184 def __repr__(self):
185 return 'JobSpec(shortname=%s, cmdline=%s)' % (self.shortname, self.cmdline)
Craig Tiller547db2b2015-01-30 14:08:39 -0800186
187
Adele Zhoue4c35612015-10-16 15:34:23 -0700188class JobResult(object):
189 def __init__(self):
190 self.state = 'UNKNOWN'
191 self.returncode = -1
192 self.elapsed_time = 0
Adele Zhoud5fffa52015-10-23 15:51:42 -0700193 self.num_failures = 0
Adele Zhoue4c35612015-10-16 15:34:23 -0700194 self.retries = 0
195 self.message = ''
Craig Tillerdb218992016-01-05 09:28:46 -0800196
Adele Zhoue4c35612015-10-16 15:34:23 -0700197
ctiller3040cb72015-01-07 12:13:17 -0800198class Job(object):
199 """Manages one job."""
200
Adele Zhou2271ab52015-10-28 13:59:14 -0700201 def __init__(self, spec, bin_hash, newline_on_success, travis, add_env):
Craig Tiller547db2b2015-01-30 14:08:39 -0800202 self._spec = spec
Craig Tiller71735182015-01-15 17:07:13 -0800203 self._bin_hash = bin_hash
Nicolas Noble044db742015-01-14 16:57:24 -0800204 self._newline_on_success = newline_on_success
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100205 self._travis = travis
Craig Tiller91318bc2015-09-24 08:58:39 -0700206 self._add_env = add_env.copy()
Craig Tiller91318bc2015-09-24 08:58:39 -0700207 self._retries = 0
Craig Tiller95cc07b2015-09-28 13:41:30 -0700208 self._timeout_retries = 0
Jan Tattermusch91ad0182015-10-01 09:22:03 -0700209 self._suppress_failure_message = False
Craig Tillerb84728d2015-02-26 15:40:39 -0800210 message('START', spec.shortname, do_newline=self._travis)
Adele Zhoue4c35612015-10-16 15:34:23 -0700211 self.result = JobResult()
Craig Tiller91318bc2015-09-24 08:58:39 -0700212 self.start()
213
Adele Zhoue4c35612015-10-16 15:34:23 -0700214 def GetSpec(self):
215 return self._spec
216
Craig Tiller91318bc2015-09-24 08:58:39 -0700217 def start(self):
218 self._tempfile = tempfile.TemporaryFile()
219 env = dict(os.environ)
220 env.update(self._spec.environ)
221 env.update(self._add_env)
222 self._start = time.time()
Craig Tiller60078bb2015-11-04 16:39:54 -0800223 try_start = lambda: subprocess.Popen(args=self._spec.cmdline,
224 stderr=subprocess.STDOUT,
225 stdout=self._tempfile,
226 cwd=self._spec.cwd,
227 shell=self._spec.shell,
228 env=env)
229 delay = 0.3
230 for i in range(0, 4):
231 try:
232 self._process = try_start()
233 break
234 except OSError:
235 message('WARNING', 'Failed to start %s, retrying in %f seconds' % (self._spec.shortname, delay))
236 time.sleep(delay)
237 delay *= 2
238 else:
239 self._process = try_start()
Craig Tiller91318bc2015-09-24 08:58:39 -0700240 self._state = _RUNNING
ctiller3040cb72015-01-07 12:13:17 -0800241
Craig Tiller71735182015-01-15 17:07:13 -0800242 def state(self, update_cache):
ctiller3040cb72015-01-07 12:13:17 -0800243 """Poll current state of the job. Prints messages at completion."""
Craig Tillerdb218992016-01-05 09:28:46 -0800244 def stdout(self=self):
245 self._tempfile.seek(0)
246 stdout = self._tempfile.read()
247 self.result.message = stdout[-_MAX_RESULT_SIZE:]
248 return stdout
ctiller3040cb72015-01-07 12:13:17 -0800249 if self._state == _RUNNING and self._process.poll() is not None:
Craig Tiller9d6139a2015-02-26 15:24:43 -0800250 elapsed = time.time() - self._start
Adele Zhoue4c35612015-10-16 15:34:23 -0700251 self.result.elapsed_time = elapsed
ctiller3040cb72015-01-07 12:13:17 -0800252 if self._process.returncode != 0:
Craig Tiller91318bc2015-09-24 08:58:39 -0700253 if self._retries < self._spec.flake_retries:
254 message('FLAKE', '%s [ret=%d, pid=%d]' % (
Craig Tillerd0ffe142015-05-19 21:51:13 -0700255 self._spec.shortname, self._process.returncode, self._process.pid),
Craig Tillerdb218992016-01-05 09:28:46 -0800256 stdout(), do_newline=True)
Craig Tiller91318bc2015-09-24 08:58:39 -0700257 self._retries += 1
Adele Zhoud5fffa52015-10-23 15:51:42 -0700258 self.result.num_failures += 1
Adele Zhoue4c35612015-10-16 15:34:23 -0700259 self.result.retries = self._timeout_retries + self._retries
Craig Tiller91318bc2015-09-24 08:58:39 -0700260 self.start()
261 else:
262 self._state = _FAILURE
Jan Tattermusch91ad0182015-10-01 09:22:03 -0700263 if not self._suppress_failure_message:
264 message('FAILED', '%s [ret=%d, pid=%d]' % (
265 self._spec.shortname, self._process.returncode, self._process.pid),
Craig Tillerdb218992016-01-05 09:28:46 -0800266 stdout(), do_newline=True)
Adele Zhoue4c35612015-10-16 15:34:23 -0700267 self.result.state = 'FAILED'
Adele Zhoud5fffa52015-10-23 15:51:42 -0700268 self.result.num_failures += 1
Adele Zhoue4c35612015-10-16 15:34:23 -0700269 self.result.returncode = self._process.returncode
ctiller3040cb72015-01-07 12:13:17 -0800270 else:
271 self._state = _SUCCESS
Craig Tiller95cc07b2015-09-28 13:41:30 -0700272 message('PASSED', '%s [time=%.1fsec; retries=%d;%d]' % (
273 self._spec.shortname, elapsed, self._retries, self._timeout_retries),
274 do_newline=self._newline_on_success or self._travis)
Adele Zhoue4c35612015-10-16 15:34:23 -0700275 self.result.state = 'PASSED'
Craig Tiller547db2b2015-01-30 14:08:39 -0800276 if self._bin_hash:
277 update_cache.finished(self._spec.identity(), self._bin_hash)
Craig Tiller590105a2016-01-19 13:03:46 -0800278 elif (self._state == _RUNNING and
279 self._spec.timeout_seconds is not None and
280 time.time() - self._start > self._spec.timeout_seconds):
Craig Tiller95cc07b2015-09-28 13:41:30 -0700281 if self._timeout_retries < self._spec.timeout_retries:
Craig Tillerdb218992016-01-05 09:28:46 -0800282 message('TIMEOUT_FLAKE', '%s [pid=%d]' % (self._spec.shortname, self._process.pid), stdout(), do_newline=True)
Craig Tiller95cc07b2015-09-28 13:41:30 -0700283 self._timeout_retries += 1
Adele Zhoud5fffa52015-10-23 15:51:42 -0700284 self.result.num_failures += 1
Adele Zhoue4c35612015-10-16 15:34:23 -0700285 self.result.retries = self._timeout_retries + self._retries
Jan Tattermusch39e3cb32015-10-22 18:21:08 -0700286 if self._spec.kill_handler:
287 self._spec.kill_handler(self)
Craig Tiller3dc1e4f2015-09-25 11:46:56 -0700288 self._process.terminate()
289 self.start()
290 else:
Craig Tillerdb218992016-01-05 09:28:46 -0800291 message('TIMEOUT', '%s [pid=%d]' % (self._spec.shortname, self._process.pid), stdout(), do_newline=True)
Craig Tiller3dc1e4f2015-09-25 11:46:56 -0700292 self.kill()
Adele Zhoue4c35612015-10-16 15:34:23 -0700293 self.result.state = 'TIMEOUT'
Adele Zhoud5fffa52015-10-23 15:51:42 -0700294 self.result.num_failures += 1
ctiller3040cb72015-01-07 12:13:17 -0800295 return self._state
296
297 def kill(self):
298 if self._state == _RUNNING:
299 self._state = _KILLED
Jan Tattermusche2686282015-10-08 16:27:07 -0700300 if self._spec.kill_handler:
301 self._spec.kill_handler(self)
ctiller3040cb72015-01-07 12:13:17 -0800302 self._process.terminate()
303
Jan Tattermusch91ad0182015-10-01 09:22:03 -0700304 def suppress_failure_message(self):
305 self._suppress_failure_message = True
Craig Tillerdb218992016-01-05 09:28:46 -0800306
ctiller3040cb72015-01-07 12:13:17 -0800307
Nicolas Nobleddef2462015-01-06 18:08:25 -0800308class Jobset(object):
309 """Manages one run of jobs."""
310
Craig Tiller533b1a22015-05-29 08:41:29 -0700311 def __init__(self, check_cancelled, maxjobs, newline_on_success, travis,
Adele Zhou2271ab52015-10-28 13:59:14 -0700312 stop_on_failure, add_env, cache):
ctiller3040cb72015-01-07 12:13:17 -0800313 self._running = set()
314 self._check_cancelled = check_cancelled
315 self._cancelled = False
Nicolas Nobleddef2462015-01-06 18:08:25 -0800316 self._failures = 0
Craig Tiller738c3342015-01-12 14:28:33 -0800317 self._completed = 0
ctiller94e5dde2015-01-09 10:41:59 -0800318 self._maxjobs = maxjobs
Nicolas Noble044db742015-01-14 16:57:24 -0800319 self._newline_on_success = newline_on_success
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100320 self._travis = travis
Craig Tiller71735182015-01-15 17:07:13 -0800321 self._cache = cache
Craig Tiller533b1a22015-05-29 08:41:29 -0700322 self._stop_on_failure = stop_on_failure
Craig Tiller74e770d2015-06-11 09:38:09 -0700323 self._hashes = {}
Craig Tillerf53d9c82015-08-04 14:19:43 -0700324 self._add_env = add_env
Adele Zhoue4c35612015-10-16 15:34:23 -0700325 self.resultset = {}
Craig Tiller6364dcb2015-11-24 16:29:06 -0800326 self._remaining = None
327
328 def set_remaining(self, remaining):
329 self._remaining = remaining
330
Adele Zhoue4c35612015-10-16 15:34:23 -0700331 def get_num_failures(self):
Craig Tiller6364dcb2015-11-24 16:29:06 -0800332 return self._failures
Nicolas Nobleddef2462015-01-06 18:08:25 -0800333
Craig Tiller56c6b6a2016-01-20 08:27:37 -0800334 def cpu_cost(self):
335 c = 0
336 for job in self._running:
337 c += job._spec.cpu_cost
338 return c
339
Craig Tiller547db2b2015-01-30 14:08:39 -0800340 def start(self, spec):
ctiller3040cb72015-01-07 12:13:17 -0800341 """Start a job. Return True on success, False on failure."""
Craig Tiller56c6b6a2016-01-20 08:27:37 -0800342 while True:
ctiller3040cb72015-01-07 12:13:17 -0800343 if self.cancelled(): return False
Craig Tiller56c6b6a2016-01-20 08:27:37 -0800344 current_cpu_cost = self.cpu_cost()
345 if current_cpu_cost == 0: break
346 if current_cpu_cost + spec.cpu_cost < self._maxjobs: break
ctiller3040cb72015-01-07 12:13:17 -0800347 self.reap()
348 if self.cancelled(): return False
Craig Tiller547db2b2015-01-30 14:08:39 -0800349 if spec.hash_targets:
Craig Tiller74e770d2015-06-11 09:38:09 -0700350 if spec.identity() in self._hashes:
351 bin_hash = self._hashes[spec.identity()]
352 else:
353 bin_hash = hashlib.sha1()
354 for fn in spec.hash_targets:
355 with open(which(fn)) as f:
356 bin_hash.update(f.read())
357 bin_hash = bin_hash.hexdigest()
358 self._hashes[spec.identity()] = bin_hash
Craig Tiller547db2b2015-01-30 14:08:39 -0800359 should_run = self._cache.should_run(spec.identity(), bin_hash)
360 else:
361 bin_hash = None
362 should_run = True
363 if should_run:
Adele Zhoue4c35612015-10-16 15:34:23 -0700364 job = Job(spec,
365 bin_hash,
366 self._newline_on_success,
367 self._travis,
Adele Zhou2271ab52015-10-28 13:59:14 -0700368 self._add_env)
Adele Zhoue4c35612015-10-16 15:34:23 -0700369 self._running.add(job)
Adele Zhoud5fffa52015-10-23 15:51:42 -0700370 self.resultset[job.GetSpec().shortname] = []
ctiller3040cb72015-01-07 12:13:17 -0800371 return True
Nicolas Nobleddef2462015-01-06 18:08:25 -0800372
ctiller3040cb72015-01-07 12:13:17 -0800373 def reap(self):
374 """Collect the dead jobs."""
375 while self._running:
376 dead = set()
377 for job in self._running:
Craig Tiller71735182015-01-15 17:07:13 -0800378 st = job.state(self._cache)
ctiller3040cb72015-01-07 12:13:17 -0800379 if st == _RUNNING: continue
Craig Tiller533b1a22015-05-29 08:41:29 -0700380 if st == _FAILURE or st == _KILLED:
381 self._failures += 1
382 if self._stop_on_failure:
383 self._cancelled = True
384 for job in self._running:
385 job.kill()
ctiller3040cb72015-01-07 12:13:17 -0800386 dead.add(job)
Craig Tiller74e770d2015-06-11 09:38:09 -0700387 break
ctiller3040cb72015-01-07 12:13:17 -0800388 for job in dead:
Craig Tiller738c3342015-01-12 14:28:33 -0800389 self._completed += 1
Adele Zhoud5fffa52015-10-23 15:51:42 -0700390 self.resultset[job.GetSpec().shortname].append(job.result)
ctiller3040cb72015-01-07 12:13:17 -0800391 self._running.remove(job)
Craig Tiller3b083062015-01-12 13:51:28 -0800392 if dead: return
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100393 if (not self._travis):
Craig Tiller2c23ad52015-12-04 06:50:38 -0800394 rstr = '' if self._remaining is None else '%d queued, ' % self._remaining
395 message('WAITING', '%s%d jobs running, %d complete, %d failed' % (
396 rstr, len(self._running), self._completed, self._failures))
Nicolas "Pixel" Noblef72d7b52015-12-03 03:07:43 +0100397 if platform_string() == 'windows':
Craig Tiller5058c692015-04-08 09:42:04 -0700398 time.sleep(0.1)
399 else:
400 global have_alarm
401 if not have_alarm:
402 have_alarm = True
403 signal.alarm(10)
404 signal.pause()
ctiller3040cb72015-01-07 12:13:17 -0800405
406 def cancelled(self):
407 """Poll for cancellation."""
408 if self._cancelled: return True
409 if not self._check_cancelled(): return False
410 for job in self._running:
411 job.kill()
412 self._cancelled = True
413 return True
414
415 def finish(self):
416 while self._running:
417 if self.cancelled(): pass # poll cancellation
418 self.reap()
419 return not self.cancelled() and self._failures == 0
Nicolas Nobleddef2462015-01-06 18:08:25 -0800420
421
ctiller3040cb72015-01-07 12:13:17 -0800422def _never_cancelled():
423 return False
424
425
Craig Tiller71735182015-01-15 17:07:13 -0800426# cache class that caches nothing
427class NoCache(object):
428 def should_run(self, cmdline, bin_hash):
429 return True
430
431 def finished(self, cmdline, bin_hash):
432 pass
433
434
Craig Tiller6364dcb2015-11-24 16:29:06 -0800435def tag_remaining(xs):
436 staging = []
437 for x in xs:
438 staging.append(x)
439 if len(staging) > 1000:
440 yield (staging.pop(0), None)
441 n = len(staging)
442 for i, x in enumerate(staging):
443 yield (x, n - i - 1)
444
445
Nicolas Nobleb09078f2015-01-14 18:06:05 -0800446def run(cmdlines,
447 check_cancelled=_never_cancelled,
448 maxjobs=None,
Craig Tiller71735182015-01-15 17:07:13 -0800449 newline_on_success=False,
Nicolas "Pixel" Noblea7df3f92015-02-26 22:07:04 +0100450 travis=False,
David Garcia Quintase90cd372015-05-31 18:15:26 -0700451 infinite_runs=False,
Craig Tiller533b1a22015-05-29 08:41:29 -0700452 stop_on_failure=False,
Nicolas "Pixel" Noble5937b5b2015-06-26 02:04:12 +0200453 cache=None,
Craig Tillerf53d9c82015-08-04 14:19:43 -0700454 add_env={}):
ctiller94e5dde2015-01-09 10:41:59 -0800455 js = Jobset(check_cancelled,
Nicolas Noble044db742015-01-14 16:57:24 -0800456 maxjobs if maxjobs is not None else _DEFAULT_MAX_JOBS,
Craig Tillerf53d9c82015-08-04 14:19:43 -0700457 newline_on_success, travis, stop_on_failure, add_env,
Adele Zhou2271ab52015-10-28 13:59:14 -0700458 cache if cache is not None else NoCache())
Craig Tiller6364dcb2015-11-24 16:29:06 -0800459 for cmdline, remaining in tag_remaining(cmdlines):
ctiller3040cb72015-01-07 12:13:17 -0800460 if not js.start(cmdline):
461 break
Craig Tiller6364dcb2015-11-24 16:29:06 -0800462 if remaining is not None:
463 js.set_remaining(remaining)
464 js.finish()
Adele Zhoue4c35612015-10-16 15:34:23 -0700465 return js.get_num_failures(), js.resultset