|
6 | 6 | import commands |
7 | 7 | import random |
8 | 8 |
|
9 | | -from pyres.exceptions import NoQueueError |
| 9 | +from pyres.exceptions import NoQueueError, TimeoutError |
10 | 10 | from pyres.job import Job |
11 | 11 | from pyres import ResQ, Stat, __version__ |
12 | 12 |
|
@@ -155,24 +155,33 @@ def work(self, interval=5): |
155 | 155 | try: |
156 | 156 | start = datetime.datetime.now() |
157 | 157 | result = (0, 0) |
158 | | - timed_out = False |
159 | 158 |
|
160 | 159 | # waits for the result or times out |
161 | | - while result == (0, 0) and not timed_out: |
| 160 | + while True: |
162 | 161 | result = os.waitpid(self.child, os.WNOHANG) |
163 | | - now = datetime.datetime.now() |
| 162 | + if result != (0, 0): |
| 163 | + break |
| 164 | + time.sleep(0.5) |
164 | 165 |
|
| 166 | + now = datetime.datetime.now() |
165 | 167 | if self.timeout and ((now - start).seconds > self.timeout): |
166 | 168 | os.kill(self.child, signal.SIGKILL) |
167 | 169 | os.waitpid(-1, os.WNOHANG) |
168 | | - timed_out = True |
| 170 | + raise TimeoutError("Timed out after %d seconds" % self.timeout) |
169 | 171 |
|
170 | 172 | except OSError as ose: |
171 | 173 | import errno |
172 | 174 |
|
173 | 175 | if ose.errno != errno.EINTR: |
174 | 176 | raise ose |
175 | 177 |
|
| 178 | + except TimeoutError as e: |
| 179 | + exceptionType, exceptionValue, exceptionTraceback = sys.exc_info() |
| 180 | + logger.exception("%s timed out: %s" % (job, e)) |
| 181 | + job.fail(exceptionTraceback) |
| 182 | + self.failed() |
| 183 | + self.done_working() |
| 184 | + |
176 | 185 | logger.debug('done waiting') |
177 | 186 | else: |
178 | 187 | self._setproctitle("Processing %s since %s" % |
|
0 commit comments