def _execute_task(self, worker, task): #Asynchronous execution of a Task. This method is called #on a separate thread of execution from the worker event loop thread. pubsub = self.pubsub task_id = task.id result = None status = None consumer = None time_ended = datetime.now() try: job = self.registry.get(task.name) consumer = TaskConsumer(self, worker, task_id, job) if not consumer.job: raise RuntimeError('Task "%s" not in registry %s' % (task.name, self.registry)) if task.status_code > states.PRECEDENCE_MAPPING[states.STARTED]: if task.expiry and time_ended > task.expiry: raise TaskTimeout else: worker.logger.info('starting task %s', task) yield self.save_task(task_id, status=states.STARTED, time_started=time_ended) pubsub.publish(self.channel('task_start'), task_id) result = yield job(consumer, *task.args, **task.kwargs) time_ended = datetime.now() else: consumer = None except TaskTimeout: worker.logger.info('Task %s timed-out', task) status = states.REVOKED except Exception: result = Failure(sys.exc_info()) if isinstance(result, Failure): result.log(msg='Failure in task %s' % task, log=worker.logger) status = states.FAILURE result = str(result) elif not status: status = states.SUCCESS if consumer: yield self.save_task(task_id, time_ended=time_ended, status=status, result=result) worker.logger.info('Finished task %s', task) # PUBLISH task_done pubsub.publish(self.channel('task_done'), task.id) self.concurrent_tasks.discard(task.id) yield task_id
def __call__(self): try: test.assertNotEqual(current_thread(), thread) if self.prev_loop: test.assertEqual(ioloop.num_loops, self.prev_loop + 1) except Exception: d.callback(sys.exc_info()) else: self.prev_loop = ioloop.num_loops self.c += 1 if self.c == self.loop: d.callback(self.c) try: raise ValueError('test call every') except Exception: mute_failure(test, Failure(sys.exc_info())) raise
def _response(self, environ): exc_info = None try: if 'SERVER_NAME' not in environ: raise HttpException(status=400) wsgi_iter = self.wsgi_callable(environ, self.start_response) yield self._async_wsgi(wsgi_iter) except IOError: # client disconnected, end this connection self.finished() except Exception: exc_info = sys.exc_info() if exc_info: failure = Failure(exc_info) try: wsgi_iter = handle_wsgi_error(environ, failure) self.start_response(wsgi_iter.status, wsgi_iter.get_headers(), exc_info) yield self._async_wsgi(wsgi_iter) except Exception: # Error handling did not work, Just shut down self.keep_alive = False self.finish_wsgi()
def add_failure(self, test, runner, error, exc_info=None, add_err=True): '''Add ``error`` to the list of errors. :param test: the test function object where the error occurs :param runner: the test runner :param error: the python exception for the error :param exc_info: optional system exc info :param add_err: if ``True`` the error is added to the list of errors :return: a tuple containing the ``error`` and the ``exc_info`` ''' if not exc_info: exc_info = sys.exc_info() if add_err: if isinstance(error, test.failureException): runner.addFailure(test, exc_info) elif isinstance(error, ExpectedFailure): runner.addExpectedFailure(test, exc_info) else: runner.addError(test, exc_info) else: Failure(exc_info).log() return (error, exc_info)
def __exit__(self, type, value, traceback): if type: self.result = Failure((type, value, traceback)) else: self.result = None return True
def _add_error(self, test, exc_info, container): failure = Failure(exc_info) failure.mute() test = self.getDescription(test) container.append((test, repr(failure)))