def on_failure(self, exc, task_id, args, kwargs, einfo): """ This overrides the error handler run by the worker when the task fails. It updates state, finish_time and traceback of the relevant task status for asynchronous tasks. Skip updating status for synchronous tasks. :param exc: The exception raised by the task. :param task_id: Unique id of the failed task. :param args: Original arguments for the executed task. :param kwargs: Original keyword arguments for the executed task. :param einfo: celery's ExceptionInfo instance, containing serialized traceback. """ if isinstance(exc, PulpCodedException): _logger.info(_('Task failed : [%(task_id)s] : %(msg)s') % {'task_id': task_id, 'msg': str(exc)}) _logger.debug(traceback.format_exc()) else: _logger.info(_('Task failed : [%s]') % task_id) # celery will log the traceback if kwargs.get('scheduled_call_id') is not None: utils.increment_failure_count(kwargs['scheduled_call_id']) if not self.request.called_directly: now = datetime.now(dateutils.utc_tz()) finish_time = dateutils.format_iso8601_datetime(now) task_status = TaskStatus.objects.get(task_id=task_id) task_status['state'] = constants.CALL_ERROR_STATE task_status['finish_time'] = finish_time task_status['traceback'] = einfo.traceback if not isinstance(exc, PulpException): exc = PulpException(str(exc)) task_status['error'] = exc.to_dict() task_status.save() self._handle_cProfile(task_id) common_utils.delete_working_directory()
def on_failure(self, exc, task_id, args, kwargs, einfo): """ This overrides the error handler run by the worker when the task fails. It updates state, finish_time and traceback of the relevant task status for asynchronous tasks. Skip updating status for synchronous tasks. :param exc: The exception raised by the task. :param task_id: Unique id of the failed task. :param args: Original arguments for the executed task. :param kwargs: Original keyword arguments for the executed task. :param einfo: celery's ExceptionInfo instance, containing serialized traceback. """ _logger.debug("Task failed : [%s]" % task_id) if not self.request.called_directly: now = datetime.now(dateutils.utc_tz()) finish_time = dateutils.format_iso8601_datetime(now) task_status = TaskStatus.objects.get(task_id=task_id) task_status['state'] = constants.CALL_ERROR_STATE task_status['finish_time'] = finish_time task_status['traceback'] = einfo.traceback if not isinstance(exc, PulpException): exc = PulpException(str(exc)) task_status['error'] = exc.to_dict() task_status.save()
def on_failure(self, exc, task_id, args, kwargs, einfo): """ This overrides the error handler run by the worker when the task fails. It updates state, finish_time and traceback of the relevant task status for asynchronous tasks. Skip updating status for synchronous tasks. :param exc: The exception raised by the task. :param task_id: Unique id of the failed task. :param args: Original arguments for the executed task. :param kwargs: Original keyword arguments for the executed task. :param einfo: celery's ExceptionInfo instance, containing serialized traceback. """ if isinstance(exc, PulpCodedException): _logger.info(_('Task failed : [%(task_id)s] : %(msg)s') % {'task_id': task_id, 'msg': str(exc)}) _logger.debug(traceback.format_exc()) else: _logger.info(_('Task failed : [%s]') % task_id) # celery will log the traceback if not self.request.called_directly: now = datetime.now(dateutils.utc_tz()) finish_time = dateutils.format_iso8601_datetime(now) task_status = TaskStatus.objects.get(task_id=task_id) task_status['state'] = constants.CALL_ERROR_STATE task_status['finish_time'] = finish_time task_status['traceback'] = einfo.traceback if not isinstance(exc, PulpException): exc = PulpException(str(exc)) task_status['error'] = exc.to_dict() task_status.save() common_utils.delete_working_directory()
def test_to_dict(self): test_exception = PulpException("foo_msg") test_exception.error_data = {"foo": "bar"} result = test_exception.to_dict() compare_dict(result, {'code': test_exception.error_code.code, 'description': str(test_exception), 'data': {"foo": "bar"}, 'sub_errors': []})
def test_serialize(self): async_result = AsyncResult('foo') test_exception = PulpException('foo') result = tasks.TaskResult('foo', test_exception, [{'task_id': 'baz'}, async_result, "qux"]) serialized = result.serialize() self.assertEquals(serialized.get('result'), 'foo') compare_dict(test_exception.to_dict(), serialized.get('error')) self.assertEquals(serialized.get('spawned_tasks'), [{'task_id': 'baz'}, {'task_id': 'foo'}, {'task_id': 'qux'}])
def test_to_dict_nested_pulp_exception(self): test_exception = PulpException("foo_msg") test_exception.error_data = {"foo": "bar"} test_exception.add_child_exception(PulpCodedException(error_codes.PLP0001)) result = test_exception.to_dict() child_exception = result['sub_errors'][0] compare_dict(child_exception, {'code': error_codes.PLP0001.code, 'description': error_codes.PLP0001.message, 'data': {}, 'sub_errors': []})
def _handle_on_failure_cleanup(self, task_id, exc, einfo): now = datetime.now(dateutils.utc_tz()) finish_time = dateutils.format_iso8601_datetime(now) task_status = TaskStatus.objects.get(task_id=task_id) task_status['state'] = constants.CALL_ERROR_STATE task_status['finish_time'] = finish_time task_status['traceback'] = einfo.traceback if not isinstance(exc, PulpException): exc = PulpException(str(exc)) task_status['error'] = exc.to_dict() task_status.save() self._handle_cProfile(task_id) common_utils.delete_working_directory()
def test_serialize(self): async_result = AsyncResult('foo') test_exception = PulpException('foo') task_status = TaskStatus(task_id='quux') result = tasks.TaskResult('foo', test_exception, [{'task_id': 'baz'}, async_result, "qux", task_status]) serialized = result.serialize() self.assertEquals(serialized.get('result'), 'foo') compare_dict(test_exception.to_dict(), serialized.get('error')) self.assertEquals(serialized.get('spawned_tasks'), [{'task_id': 'baz'}, {'task_id': 'foo'}, {'task_id': 'qux'}, {'task_id': 'quux'}])
def test_to_dict_nested_general_exception(self): test_exception = PulpException("foo_msg") test_exception.error_data = {"foo": "bar"} test_exception.add_child_exception(Exception("Foo Message")) test_exception.add_child_exception(Exception("Bar Message")) result = test_exception.to_dict() child_exception = result['sub_errors'][0] compare_dict(child_exception, {'code': error_codes.PLP0000.code, 'description': "Foo Message", 'data': {}, 'sub_errors': []}) child_exception = result['sub_errors'][1] compare_dict(child_exception, {'code': error_codes.PLP0000.code, 'description': "Bar Message", 'data': {}, 'sub_errors': []})
def on_failure(self, exc, task_id, args, kwargs, einfo): """ This overrides the error handler run by the worker when the task fails. It updates state, finish_time and traceback of the relevant task status for asynchronous tasks. Skip updating status for synchronous tasks. :param exc: The exception raised by the task. :param task_id: Unique id of the failed task. :param args: Original arguments for the executed task. :param kwargs: Original keyword arguments for the executed task. :param einfo: celery's ExceptionInfo instance, containing serialized traceback. """ logger.debug("Task failed : [%s]" % task_id) if not self.request.called_directly: delta = {'state': dispatch_constants.CALL_ERROR_STATE, 'finish_time': dateutils.now_utc_timestamp(), 'traceback': einfo.traceback} if not isinstance(exc, PulpException): exc = PulpException(str(exc)) delta['error'] = exc.to_dict() TaskStatusManager.update_task_status(task_id=task_id, delta=delta)