Craig Tiller | c2c7921 | 2015-02-16 12:00:01 -0800 | [diff] [blame] | 1 | # Copyright 2015, Google Inc. |
| 2 | # 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 Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 30 | """Run a group of subprocesses and then finish.""" |
| 31 | |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 32 | import hashlib |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 33 | import multiprocessing |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 34 | import os |
Craig Tiller | 5058c69 | 2015-04-08 09:42:04 -0700 | [diff] [blame] | 35 | import platform |
Craig Tiller | 336ad50 | 2015-02-24 14:46:02 -0800 | [diff] [blame] | 36 | import signal |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 37 | import subprocess |
| 38 | import sys |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 39 | import tempfile |
| 40 | import time |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 41 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 42 | |
ctiller | 94e5dde | 2015-01-09 10:41:59 -0800 | [diff] [blame] | 43 | _DEFAULT_MAX_JOBS = 16 * multiprocessing.cpu_count() |
Adele Zhou | d01cbe3 | 2015-11-02 14:20:43 -0800 | [diff] [blame] | 44 | _MAX_RESULT_SIZE = 8192 |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 45 | |
| 46 | |
Craig Tiller | 336ad50 | 2015-02-24 14:46:02 -0800 | [diff] [blame] | 47 | # setup a signal handler so that signal.pause registers 'something' |
| 48 | # when a child finishes |
| 49 | # not using futures and threading to avoid a dependency on subprocess32 |
Adele Zhou | e4c3561 | 2015-10-16 15:34:23 -0700 | [diff] [blame] | 50 | if platform.system() == 'Windows': |
Craig Tiller | 5058c69 | 2015-04-08 09:42:04 -0700 | [diff] [blame] | 51 | pass |
| 52 | else: |
| 53 | have_alarm = False |
| 54 | def alarm_handler(unused_signum, unused_frame): |
| 55 | global have_alarm |
| 56 | have_alarm = False |
| 57 | |
| 58 | signal.signal(signal.SIGCHLD, lambda unused_signum, unused_frame: None) |
| 59 | signal.signal(signal.SIGALRM, alarm_handler) |
Craig Tiller | 336ad50 | 2015-02-24 14:46:02 -0800 | [diff] [blame] | 60 | |
| 61 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 62 | _SUCCESS = object() |
| 63 | _FAILURE = object() |
| 64 | _RUNNING = object() |
| 65 | _KILLED = object() |
| 66 | |
| 67 | |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 68 | _COLORS = { |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 69 | 'red': [ 31, 0 ], |
| 70 | 'green': [ 32, 0 ], |
| 71 | 'yellow': [ 33, 0 ], |
| 72 | 'lightgray': [ 37, 0], |
| 73 | 'gray': [ 30, 1 ], |
Craig Tiller | d7e09c3 | 2015-09-25 11:33:39 -0700 | [diff] [blame] | 74 | 'purple': [ 35, 0 ], |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | |
| 78 | _BEGINNING_OF_LINE = '\x1b[0G' |
| 79 | _CLEAR_LINE = '\x1b[2K' |
| 80 | |
| 81 | |
| 82 | _TAG_COLOR = { |
| 83 | 'FAILED': 'red', |
Craig Tiller | d7e09c3 | 2015-09-25 11:33:39 -0700 | [diff] [blame] | 84 | 'FLAKE': 'purple', |
Craig Tiller | 3dc1e4f | 2015-09-25 11:46:56 -0700 | [diff] [blame] | 85 | 'TIMEOUT_FLAKE': 'purple', |
Masood Malekghassemi | e5f7002 | 2015-06-29 09:20:26 -0700 | [diff] [blame] | 86 | 'WARNING': 'yellow', |
Craig Tiller | e1d0d1c | 2015-02-27 08:54:23 -0800 | [diff] [blame] | 87 | 'TIMEOUT': 'red', |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 88 | 'PASSED': 'green', |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 89 | 'START': 'gray', |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 90 | 'WAITING': 'yellow', |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 91 | 'SUCCESS': 'green', |
| 92 | 'IDLE': 'gray', |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | |
Nicolas "Pixel" Noble | 99768ac | 2015-05-13 02:34:06 +0200 | [diff] [blame] | 96 | def message(tag, msg, explanatory_text=None, do_newline=False): |
| 97 | if message.old_tag == tag and message.old_msg == msg and not explanatory_text: |
| 98 | return |
| 99 | message.old_tag = tag |
| 100 | message.old_msg = msg |
Craig Tiller | 23d2f3f | 2015-02-24 15:23:32 -0800 | [diff] [blame] | 101 | try: |
Craig Tiller | 9f3b2d7 | 2015-08-25 11:50:57 -0700 | [diff] [blame] | 102 | if platform.system() == 'Windows' or not sys.stdout.isatty(): |
| 103 | if explanatory_text: |
| 104 | print explanatory_text |
| 105 | print '%s: %s' % (tag, msg) |
| 106 | return |
vjpai | a29d2d7 | 2015-07-08 10:31:15 -0700 | [diff] [blame] | 107 | sys.stdout.write('%s%s%s\x1b[%d;%dm%s\x1b[0m: %s%s' % ( |
| 108 | _BEGINNING_OF_LINE, |
| 109 | _CLEAR_LINE, |
| 110 | '\n%s' % explanatory_text if explanatory_text is not None else '', |
| 111 | _COLORS[_TAG_COLOR[tag]][1], |
| 112 | _COLORS[_TAG_COLOR[tag]][0], |
| 113 | tag, |
| 114 | msg, |
| 115 | '\n' if do_newline or explanatory_text is not None else '')) |
Craig Tiller | 23d2f3f | 2015-02-24 15:23:32 -0800 | [diff] [blame] | 116 | sys.stdout.flush() |
| 117 | except: |
| 118 | pass |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 119 | |
Adele Zhou | e4c3561 | 2015-10-16 15:34:23 -0700 | [diff] [blame] | 120 | message.old_tag = '' |
| 121 | message.old_msg = '' |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 122 | |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 123 | def which(filename): |
| 124 | if '/' in filename: |
| 125 | return filename |
| 126 | for path in os.environ['PATH'].split(os.pathsep): |
| 127 | if os.path.exists(os.path.join(path, filename)): |
| 128 | return os.path.join(path, filename) |
| 129 | raise Exception('%s not found' % filename) |
| 130 | |
| 131 | |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 132 | class JobSpec(object): |
| 133 | """Specifies what to run for a job.""" |
| 134 | |
Jan Tattermusch | 725835a | 2015-08-01 21:02:35 -0700 | [diff] [blame] | 135 | def __init__(self, cmdline, shortname=None, environ=None, hash_targets=None, |
Craig Tiller | 95cc07b | 2015-09-28 13:41:30 -0700 | [diff] [blame] | 136 | cwd=None, shell=False, timeout_seconds=5*60, flake_retries=0, |
Jan Tattermusch | e268628 | 2015-10-08 16:27:07 -0700 | [diff] [blame] | 137 | timeout_retries=0, kill_handler=None): |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 138 | """ |
| 139 | Arguments: |
| 140 | cmdline: a list of arguments to pass as the command line |
| 141 | environ: a dictionary of environment variables to set in the child process |
| 142 | hash_targets: which files to include in the hash representing the jobs version |
| 143 | (or empty, indicating the job should not be hashed) |
Jan Tattermusch | e268628 | 2015-10-08 16:27:07 -0700 | [diff] [blame] | 144 | kill_handler: a handler that will be called whenever job.kill() is invoked |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 145 | """ |
murgatroid99 | 132ce6a | 2015-03-04 17:29:14 -0800 | [diff] [blame] | 146 | if environ is None: |
| 147 | environ = {} |
| 148 | if hash_targets is None: |
| 149 | hash_targets = [] |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 150 | self.cmdline = cmdline |
| 151 | self.environ = environ |
| 152 | self.shortname = cmdline[0] if shortname is None else shortname |
| 153 | self.hash_targets = hash_targets or [] |
Craig Tiller | 5058c69 | 2015-04-08 09:42:04 -0700 | [diff] [blame] | 154 | self.cwd = cwd |
Jan Tattermusch | e824359 | 2015-04-17 14:14:01 -0700 | [diff] [blame] | 155 | self.shell = shell |
Jan Tattermusch | 725835a | 2015-08-01 21:02:35 -0700 | [diff] [blame] | 156 | self.timeout_seconds = timeout_seconds |
Craig Tiller | 91318bc | 2015-09-24 08:58:39 -0700 | [diff] [blame] | 157 | self.flake_retries = flake_retries |
Craig Tiller | bfc8a06 | 2015-09-28 14:40:21 -0700 | [diff] [blame] | 158 | self.timeout_retries = timeout_retries |
Jan Tattermusch | e268628 | 2015-10-08 16:27:07 -0700 | [diff] [blame] | 159 | self.kill_handler = kill_handler |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 160 | |
| 161 | def identity(self): |
| 162 | return '%r %r %r' % (self.cmdline, self.environ, self.hash_targets) |
| 163 | |
| 164 | def __hash__(self): |
| 165 | return hash(self.identity()) |
| 166 | |
| 167 | def __cmp__(self, other): |
| 168 | return self.identity() == other.identity() |
| 169 | |
| 170 | |
Adele Zhou | e4c3561 | 2015-10-16 15:34:23 -0700 | [diff] [blame] | 171 | class JobResult(object): |
| 172 | def __init__(self): |
| 173 | self.state = 'UNKNOWN' |
| 174 | self.returncode = -1 |
| 175 | self.elapsed_time = 0 |
Adele Zhou | d5fffa5 | 2015-10-23 15:51:42 -0700 | [diff] [blame] | 176 | self.num_failures = 0 |
Adele Zhou | e4c3561 | 2015-10-16 15:34:23 -0700 | [diff] [blame] | 177 | self.retries = 0 |
| 178 | self.message = '' |
| 179 | |
| 180 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 181 | class Job(object): |
| 182 | """Manages one job.""" |
| 183 | |
Adele Zhou | 2271ab5 | 2015-10-28 13:59:14 -0700 | [diff] [blame] | 184 | def __init__(self, spec, bin_hash, newline_on_success, travis, add_env): |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 185 | self._spec = spec |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 186 | self._bin_hash = bin_hash |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 187 | self._newline_on_success = newline_on_success |
Nicolas "Pixel" Noble | a7df3f9 | 2015-02-26 22:07:04 +0100 | [diff] [blame] | 188 | self._travis = travis |
Craig Tiller | 91318bc | 2015-09-24 08:58:39 -0700 | [diff] [blame] | 189 | self._add_env = add_env.copy() |
Craig Tiller | 91318bc | 2015-09-24 08:58:39 -0700 | [diff] [blame] | 190 | self._retries = 0 |
Craig Tiller | 95cc07b | 2015-09-28 13:41:30 -0700 | [diff] [blame] | 191 | self._timeout_retries = 0 |
Jan Tattermusch | 91ad018 | 2015-10-01 09:22:03 -0700 | [diff] [blame] | 192 | self._suppress_failure_message = False |
Craig Tiller | b84728d | 2015-02-26 15:40:39 -0800 | [diff] [blame] | 193 | message('START', spec.shortname, do_newline=self._travis) |
Adele Zhou | e4c3561 | 2015-10-16 15:34:23 -0700 | [diff] [blame] | 194 | self.result = JobResult() |
Craig Tiller | 91318bc | 2015-09-24 08:58:39 -0700 | [diff] [blame] | 195 | self.start() |
| 196 | |
Adele Zhou | e4c3561 | 2015-10-16 15:34:23 -0700 | [diff] [blame] | 197 | def GetSpec(self): |
| 198 | return self._spec |
| 199 | |
Craig Tiller | 91318bc | 2015-09-24 08:58:39 -0700 | [diff] [blame] | 200 | def start(self): |
| 201 | self._tempfile = tempfile.TemporaryFile() |
| 202 | env = dict(os.environ) |
| 203 | env.update(self._spec.environ) |
| 204 | env.update(self._add_env) |
| 205 | self._start = time.time() |
Craig Tiller | 60078bb | 2015-11-04 16:39:54 -0800 | [diff] [blame] | 206 | try_start = lambda: subprocess.Popen(args=self._spec.cmdline, |
| 207 | stderr=subprocess.STDOUT, |
| 208 | stdout=self._tempfile, |
| 209 | cwd=self._spec.cwd, |
| 210 | shell=self._spec.shell, |
| 211 | env=env) |
| 212 | delay = 0.3 |
| 213 | for i in range(0, 4): |
| 214 | try: |
| 215 | self._process = try_start() |
| 216 | break |
| 217 | except OSError: |
| 218 | message('WARNING', 'Failed to start %s, retrying in %f seconds' % (self._spec.shortname, delay)) |
| 219 | time.sleep(delay) |
| 220 | delay *= 2 |
| 221 | else: |
| 222 | self._process = try_start() |
Craig Tiller | 91318bc | 2015-09-24 08:58:39 -0700 | [diff] [blame] | 223 | self._state = _RUNNING |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 224 | |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 225 | def state(self, update_cache): |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 226 | """Poll current state of the job. Prints messages at completion.""" |
Adele Zhou | d01cbe3 | 2015-11-02 14:20:43 -0800 | [diff] [blame] | 227 | self._tempfile.seek(0) |
| 228 | stdout = self._tempfile.read() |
| 229 | self.result.message = stdout[-_MAX_RESULT_SIZE:] |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 230 | if self._state == _RUNNING and self._process.poll() is not None: |
Craig Tiller | 9d6139a | 2015-02-26 15:24:43 -0800 | [diff] [blame] | 231 | elapsed = time.time() - self._start |
Adele Zhou | e4c3561 | 2015-10-16 15:34:23 -0700 | [diff] [blame] | 232 | self.result.elapsed_time = elapsed |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 233 | if self._process.returncode != 0: |
Craig Tiller | 91318bc | 2015-09-24 08:58:39 -0700 | [diff] [blame] | 234 | if self._retries < self._spec.flake_retries: |
| 235 | message('FLAKE', '%s [ret=%d, pid=%d]' % ( |
Craig Tiller | d0ffe14 | 2015-05-19 21:51:13 -0700 | [diff] [blame] | 236 | self._spec.shortname, self._process.returncode, self._process.pid), |
| 237 | stdout, do_newline=True) |
Craig Tiller | 91318bc | 2015-09-24 08:58:39 -0700 | [diff] [blame] | 238 | self._retries += 1 |
Adele Zhou | d5fffa5 | 2015-10-23 15:51:42 -0700 | [diff] [blame] | 239 | self.result.num_failures += 1 |
Adele Zhou | e4c3561 | 2015-10-16 15:34:23 -0700 | [diff] [blame] | 240 | self.result.retries = self._timeout_retries + self._retries |
Craig Tiller | 91318bc | 2015-09-24 08:58:39 -0700 | [diff] [blame] | 241 | self.start() |
| 242 | else: |
| 243 | self._state = _FAILURE |
Jan Tattermusch | 91ad018 | 2015-10-01 09:22:03 -0700 | [diff] [blame] | 244 | if not self._suppress_failure_message: |
| 245 | message('FAILED', '%s [ret=%d, pid=%d]' % ( |
| 246 | self._spec.shortname, self._process.returncode, self._process.pid), |
| 247 | stdout, do_newline=True) |
Adele Zhou | e4c3561 | 2015-10-16 15:34:23 -0700 | [diff] [blame] | 248 | self.result.state = 'FAILED' |
Adele Zhou | d5fffa5 | 2015-10-23 15:51:42 -0700 | [diff] [blame] | 249 | self.result.num_failures += 1 |
Adele Zhou | e4c3561 | 2015-10-16 15:34:23 -0700 | [diff] [blame] | 250 | self.result.returncode = self._process.returncode |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 251 | else: |
| 252 | self._state = _SUCCESS |
Craig Tiller | 95cc07b | 2015-09-28 13:41:30 -0700 | [diff] [blame] | 253 | message('PASSED', '%s [time=%.1fsec; retries=%d;%d]' % ( |
| 254 | self._spec.shortname, elapsed, self._retries, self._timeout_retries), |
| 255 | do_newline=self._newline_on_success or self._travis) |
Adele Zhou | e4c3561 | 2015-10-16 15:34:23 -0700 | [diff] [blame] | 256 | self.result.state = 'PASSED' |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 257 | if self._bin_hash: |
| 258 | update_cache.finished(self._spec.identity(), self._bin_hash) |
Jan Tattermusch | 725835a | 2015-08-01 21:02:35 -0700 | [diff] [blame] | 259 | elif self._state == _RUNNING and time.time() - self._start > self._spec.timeout_seconds: |
Craig Tiller | 95cc07b | 2015-09-28 13:41:30 -0700 | [diff] [blame] | 260 | if self._timeout_retries < self._spec.timeout_retries: |
Craig Tiller | 3dc1e4f | 2015-09-25 11:46:56 -0700 | [diff] [blame] | 261 | message('TIMEOUT_FLAKE', self._spec.shortname, stdout, do_newline=True) |
Craig Tiller | 95cc07b | 2015-09-28 13:41:30 -0700 | [diff] [blame] | 262 | self._timeout_retries += 1 |
Adele Zhou | d5fffa5 | 2015-10-23 15:51:42 -0700 | [diff] [blame] | 263 | self.result.num_failures += 1 |
Adele Zhou | e4c3561 | 2015-10-16 15:34:23 -0700 | [diff] [blame] | 264 | self.result.retries = self._timeout_retries + self._retries |
Jan Tattermusch | 39e3cb3 | 2015-10-22 18:21:08 -0700 | [diff] [blame] | 265 | if self._spec.kill_handler: |
| 266 | self._spec.kill_handler(self) |
Craig Tiller | 3dc1e4f | 2015-09-25 11:46:56 -0700 | [diff] [blame] | 267 | self._process.terminate() |
| 268 | self.start() |
| 269 | else: |
| 270 | message('TIMEOUT', self._spec.shortname, stdout, do_newline=True) |
| 271 | self.kill() |
Adele Zhou | e4c3561 | 2015-10-16 15:34:23 -0700 | [diff] [blame] | 272 | self.result.state = 'TIMEOUT' |
Adele Zhou | d5fffa5 | 2015-10-23 15:51:42 -0700 | [diff] [blame] | 273 | self.result.num_failures += 1 |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 274 | return self._state |
| 275 | |
| 276 | def kill(self): |
| 277 | if self._state == _RUNNING: |
| 278 | self._state = _KILLED |
Jan Tattermusch | e268628 | 2015-10-08 16:27:07 -0700 | [diff] [blame] | 279 | if self._spec.kill_handler: |
| 280 | self._spec.kill_handler(self) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 281 | self._process.terminate() |
| 282 | |
Jan Tattermusch | 91ad018 | 2015-10-01 09:22:03 -0700 | [diff] [blame] | 283 | def suppress_failure_message(self): |
| 284 | self._suppress_failure_message = True |
Adele Zhou | d5fffa5 | 2015-10-23 15:51:42 -0700 | [diff] [blame] | 285 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 286 | |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 287 | class Jobset(object): |
| 288 | """Manages one run of jobs.""" |
| 289 | |
Craig Tiller | 533b1a2 | 2015-05-29 08:41:29 -0700 | [diff] [blame] | 290 | def __init__(self, check_cancelled, maxjobs, newline_on_success, travis, |
Adele Zhou | 2271ab5 | 2015-10-28 13:59:14 -0700 | [diff] [blame] | 291 | stop_on_failure, add_env, cache): |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 292 | self._running = set() |
| 293 | self._check_cancelled = check_cancelled |
| 294 | self._cancelled = False |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 295 | self._failures = 0 |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 296 | self._completed = 0 |
ctiller | 94e5dde | 2015-01-09 10:41:59 -0800 | [diff] [blame] | 297 | self._maxjobs = maxjobs |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 298 | self._newline_on_success = newline_on_success |
Nicolas "Pixel" Noble | a7df3f9 | 2015-02-26 22:07:04 +0100 | [diff] [blame] | 299 | self._travis = travis |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 300 | self._cache = cache |
Craig Tiller | 533b1a2 | 2015-05-29 08:41:29 -0700 | [diff] [blame] | 301 | self._stop_on_failure = stop_on_failure |
Craig Tiller | 74e770d | 2015-06-11 09:38:09 -0700 | [diff] [blame] | 302 | self._hashes = {} |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 303 | self._add_env = add_env |
Adele Zhou | e4c3561 | 2015-10-16 15:34:23 -0700 | [diff] [blame] | 304 | self.resultset = {} |
| 305 | |
| 306 | def get_num_failures(self): |
| 307 | return self._failures |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 308 | |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 309 | def start(self, spec): |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 310 | """Start a job. Return True on success, False on failure.""" |
ctiller | 94e5dde | 2015-01-09 10:41:59 -0800 | [diff] [blame] | 311 | while len(self._running) >= self._maxjobs: |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 312 | if self.cancelled(): return False |
| 313 | self.reap() |
| 314 | if self.cancelled(): return False |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 315 | if spec.hash_targets: |
Craig Tiller | 74e770d | 2015-06-11 09:38:09 -0700 | [diff] [blame] | 316 | if spec.identity() in self._hashes: |
| 317 | bin_hash = self._hashes[spec.identity()] |
| 318 | else: |
| 319 | bin_hash = hashlib.sha1() |
| 320 | for fn in spec.hash_targets: |
| 321 | with open(which(fn)) as f: |
| 322 | bin_hash.update(f.read()) |
| 323 | bin_hash = bin_hash.hexdigest() |
| 324 | self._hashes[spec.identity()] = bin_hash |
Craig Tiller | 547db2b | 2015-01-30 14:08:39 -0800 | [diff] [blame] | 325 | should_run = self._cache.should_run(spec.identity(), bin_hash) |
| 326 | else: |
| 327 | bin_hash = None |
| 328 | should_run = True |
| 329 | if should_run: |
Adele Zhou | e4c3561 | 2015-10-16 15:34:23 -0700 | [diff] [blame] | 330 | job = Job(spec, |
| 331 | bin_hash, |
| 332 | self._newline_on_success, |
| 333 | self._travis, |
Adele Zhou | 2271ab5 | 2015-10-28 13:59:14 -0700 | [diff] [blame] | 334 | self._add_env) |
Adele Zhou | e4c3561 | 2015-10-16 15:34:23 -0700 | [diff] [blame] | 335 | self._running.add(job) |
Adele Zhou | d5fffa5 | 2015-10-23 15:51:42 -0700 | [diff] [blame] | 336 | self.resultset[job.GetSpec().shortname] = [] |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 337 | return True |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 338 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 339 | def reap(self): |
| 340 | """Collect the dead jobs.""" |
| 341 | while self._running: |
| 342 | dead = set() |
| 343 | for job in self._running: |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 344 | st = job.state(self._cache) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 345 | if st == _RUNNING: continue |
Craig Tiller | 533b1a2 | 2015-05-29 08:41:29 -0700 | [diff] [blame] | 346 | if st == _FAILURE or st == _KILLED: |
| 347 | self._failures += 1 |
| 348 | if self._stop_on_failure: |
| 349 | self._cancelled = True |
| 350 | for job in self._running: |
| 351 | job.kill() |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 352 | dead.add(job) |
Craig Tiller | 74e770d | 2015-06-11 09:38:09 -0700 | [diff] [blame] | 353 | break |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 354 | for job in dead: |
Craig Tiller | 738c334 | 2015-01-12 14:28:33 -0800 | [diff] [blame] | 355 | self._completed += 1 |
Adele Zhou | d5fffa5 | 2015-10-23 15:51:42 -0700 | [diff] [blame] | 356 | self.resultset[job.GetSpec().shortname].append(job.result) |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 357 | self._running.remove(job) |
Craig Tiller | 3b08306 | 2015-01-12 13:51:28 -0800 | [diff] [blame] | 358 | if dead: return |
Nicolas "Pixel" Noble | a7df3f9 | 2015-02-26 22:07:04 +0100 | [diff] [blame] | 359 | if (not self._travis): |
| 360 | message('WAITING', '%d jobs running, %d complete, %d failed' % ( |
| 361 | len(self._running), self._completed, self._failures)) |
Craig Tiller | 5058c69 | 2015-04-08 09:42:04 -0700 | [diff] [blame] | 362 | if platform.system() == 'Windows': |
| 363 | time.sleep(0.1) |
| 364 | else: |
| 365 | global have_alarm |
| 366 | if not have_alarm: |
| 367 | have_alarm = True |
| 368 | signal.alarm(10) |
| 369 | signal.pause() |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 370 | |
| 371 | def cancelled(self): |
| 372 | """Poll for cancellation.""" |
| 373 | if self._cancelled: return True |
| 374 | if not self._check_cancelled(): return False |
| 375 | for job in self._running: |
| 376 | job.kill() |
| 377 | self._cancelled = True |
| 378 | return True |
| 379 | |
| 380 | def finish(self): |
| 381 | while self._running: |
| 382 | if self.cancelled(): pass # poll cancellation |
| 383 | self.reap() |
| 384 | return not self.cancelled() and self._failures == 0 |
Nicolas Noble | ddef246 | 2015-01-06 18:08:25 -0800 | [diff] [blame] | 385 | |
| 386 | |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 387 | def _never_cancelled(): |
| 388 | return False |
| 389 | |
| 390 | |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 391 | # cache class that caches nothing |
| 392 | class NoCache(object): |
| 393 | def should_run(self, cmdline, bin_hash): |
| 394 | return True |
| 395 | |
| 396 | def finished(self, cmdline, bin_hash): |
| 397 | pass |
| 398 | |
| 399 | |
Nicolas Noble | b09078f | 2015-01-14 18:06:05 -0800 | [diff] [blame] | 400 | def run(cmdlines, |
| 401 | check_cancelled=_never_cancelled, |
| 402 | maxjobs=None, |
Craig Tiller | 7173518 | 2015-01-15 17:07:13 -0800 | [diff] [blame] | 403 | newline_on_success=False, |
Nicolas "Pixel" Noble | a7df3f9 | 2015-02-26 22:07:04 +0100 | [diff] [blame] | 404 | travis=False, |
David Garcia Quintas | e90cd37 | 2015-05-31 18:15:26 -0700 | [diff] [blame] | 405 | infinite_runs=False, |
Craig Tiller | 533b1a2 | 2015-05-29 08:41:29 -0700 | [diff] [blame] | 406 | stop_on_failure=False, |
Nicolas "Pixel" Noble | 5937b5b | 2015-06-26 02:04:12 +0200 | [diff] [blame] | 407 | cache=None, |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 408 | add_env={}): |
ctiller | 94e5dde | 2015-01-09 10:41:59 -0800 | [diff] [blame] | 409 | js = Jobset(check_cancelled, |
Nicolas Noble | 044db74 | 2015-01-14 16:57:24 -0800 | [diff] [blame] | 410 | maxjobs if maxjobs is not None else _DEFAULT_MAX_JOBS, |
Craig Tiller | f53d9c8 | 2015-08-04 14:19:43 -0700 | [diff] [blame] | 411 | newline_on_success, travis, stop_on_failure, add_env, |
Adele Zhou | 2271ab5 | 2015-10-28 13:59:14 -0700 | [diff] [blame] | 412 | cache if cache is not None else NoCache()) |
Craig Tiller | b84728d | 2015-02-26 15:40:39 -0800 | [diff] [blame] | 413 | for cmdline in cmdlines: |
ctiller | 3040cb7 | 2015-01-07 12:13:17 -0800 | [diff] [blame] | 414 | if not js.start(cmdline): |
| 415 | break |
Adele Zhou | e4c3561 | 2015-10-16 15:34:23 -0700 | [diff] [blame] | 416 | js.finish() |
| 417 | return js.get_num_failures(), js.resultset |