def test_safe_repr(self): self.assertTrue(encoding.safe_repr(object())) class foo(object): def __repr__(self): raise ValueError('foo') self.assertTrue(encoding.safe_repr(foo()))
def test_safe_repr(self): self.assertTrue(encoding.safe_repr(object())) class foo(object): def __repr__(self): raise ValueError("foo") self.assertTrue(encoding.safe_repr(foo()))
def __repr__(self): return '<ModelEntry: {0} {1}(*{2}, **{3}) {{4}}>'.format( safe_str(self.name), self.task, safe_repr(self.args), safe_repr(self.kwargs), self.schedule, )
def __repr__(self): return "<ModelEntry: %s %s(*%s, **%s) {%s}>" % ( safe_str(self.name), self.task, safe_repr(self.args), safe_repr(self.kwargs), self.schedule, )
def test_safe_repr(self): assert encoding.safe_repr(object()) class foo: def __repr__(self): raise ValueError('foo') assert encoding.safe_repr(foo())
def test_safe_repr(self): assert encoding.safe_repr(object()) class foo(object): def __repr__(self): raise ValueError('foo') assert encoding.safe_repr(foo())
def info(self, safe=False): return {"id": self.task_id, "name": self.task_name, "args": self.args if safe else safe_repr(self.args), "kwargs": self.kwargs if safe else safe_repr(self.kwargs), "hostname": self.hostname, "time_start": self.time_start, "acknowledged": self.acknowledged, "delivery_info": self.delivery_info, "worker_pid": self.worker_pid}
def on_retry(self, exc_info): """Handler called if the task should be retried.""" self.send_event("task-retried", uuid=self.task_id, exception=safe_repr(exc_info.exception.exc), traceback=safe_repr(exc_info.traceback)) self.logger.info(self.retry_msg.strip() % { "id": self.task_id, "name": self.task_name, "exc": safe_repr(exc_info.exception.exc)})
def info(self, safe=False): return { "id": self.task_id, "name": self.task_name, "args": self.args if safe else safe_repr(self.args), "kwargs": self.kwargs if safe else safe_repr(self.kwargs), "hostname": self.hostname, "time_start": self.time_start, "acknowledged": self.acknowledged, "delivery_info": self.delivery_info, "worker_pid": self.worker_pid }
def on_retry(self, exc_info): """Handler called if the task should be retried.""" self.send_event("task-retried", uuid=self.task_id, exception=safe_repr(exc_info.exception.exc), traceback=safe_repr(exc_info.traceback)) self.logger.info( self.retry_msg.strip() % { "id": self.task_id, "name": self.task_name, "exc": safe_repr(exc_info.exception.exc) })
def on_failure(self, exc_info): """Handler called if the task raised an exception.""" state.task_ready(self) if self.task.acks_late: self.acknowledge() if isinstance(exc_info.exception, RetryTaskError): return self.on_retry(exc_info) # This is a special case as the process would not have had # time to write the result. if isinstance(exc_info.exception, WorkerLostError): if self._store_errors: self.task.backend.mark_as_failure(self.task_id, exc_info.exception) self.send_event("task-failed", uuid=self.task_id, exception=safe_repr(exc_info.exception), traceback=safe_str(exc_info.traceback)) context = { "hostname": self.hostname, "id": self.task_id, "name": self.task_name, "exc": safe_repr(exc_info.exception), "traceback": safe_str(exc_info.traceback), "args": self.args, "kwargs": self.kwargs } log_with_extra(self.logger, logging.ERROR, self.error_msg.strip() % context, exc_info=exc_info, extra={ "data": { "hostname": self.hostname, "id": self.task_id, "name": self.task_name } }) task_obj = tasks.get(self.task_name, object) self.send_error_email(task_obj, context, exc_info.exception, enabled=task_obj.send_error_emails, whitelist=task_obj.error_whitelist)
def info(self, safe=False): args = self.args kwargs = self.kwargs if not safe: args = safe_repr(args) kwargs = safe_repr(self.kwargs) return {"id": self.task_id, "name": self.task_name, "args": args, "kwargs": kwargs, "hostname": self.hostname, "time_start": self.time_start, "acknowledged": self.acknowledged, "delivery_info": self.delivery_info}
def info(self, safe=False): args = self.args kwargs = self.kwargs if not safe: args = safe_repr(args) kwargs = safe_repr(self.kwargs) return { "id": self.task_id, "name": self.task_name, "args": args, "kwargs": kwargs, "hostname": self.hostname, "time_start": self.time_start, "acknowledged": self.acknowledged, "delivery_info": self.delivery_info, "worker_pid": self.worker_pid }
def _inner(*args, **kwargs): try: retval = fun(*args, **kwargs) except Exception as exc: response = {'status': 'failure', 'reason': safe_repr(exc)} else: response = {'status': 'success', 'retval': retval} return JsonResponse(response)
def _inner(*args, **kwargs): try: retval = fun(*args, **kwargs) except Exception as exc: response = {"status": "failure", "reason": safe_repr(exc)} else: response = {"status": "success", "retval": retval} return JsonResponse(response)
def task_status(request, task_id): """Returns task status and result in JSON format.""" result = AsyncResult(task_id) state, retval = result.state, result.result response_data = {'id': task_id, 'status': state, 'result': retval} if state in states.EXCEPTION_STATES: traceback = result.traceback response_data.update({'result': safe_repr(retval), 'exc': get_full_cls_name(retval.__class__), 'traceback': traceback}) return JsonResponse({'task': response_data})
def task_status(request, task_id): """Returns task status and result in JSON format.""" result = AsyncResult(task_id) state, retval = result.state, result.result response_data = dict(id=task_id, status=state, result=retval) if state in states.EXCEPTION_STATES: traceback = result.traceback response_data.update({"result": safe_repr(retval), "exc": get_full_cls_name(retval.__class__), "traceback": traceback}) return JsonResponse({"task": response_data})
def on_failure(self, exc_info): """Handler called if the task raised an exception.""" state.task_ready(self) if self.task.acks_late: self.acknowledge() if isinstance(exc_info.exception, RetryTaskError): return self.on_retry(exc_info) # This is a special case as the process would not have had # time to write the result. if isinstance(exc_info.exception, WorkerLostError): if self._store_errors: self.task.backend.mark_as_failure(self.task_id, exc_info.exception) self.send_event("task-failed", uuid=self.task_id, exception=safe_repr(exc_info.exception), traceback=safe_str(exc_info.traceback)) context = {"hostname": self.hostname, "id": self.task_id, "name": self.task_name, "exc": safe_repr(exc_info.exception), "traceback": safe_str(exc_info.traceback), "args": self.args, "kwargs": self.kwargs} log_with_extra(self.logger, logging.ERROR, self.error_msg.strip() % context, exc_info=exc_info, extra={"data": {"hostname": self.hostname, "id": self.task_id, "name": self.task_name}}) task_obj = tasks.get(self.task_name, object) self.send_error_email(task_obj, context, exc_info.exception, enabled=task_obj.send_error_emails, whitelist=task_obj.error_whitelist)
def __init__(self, exc_module, exc_cls_name, exc_args, text=None): safe_exc_args = [] for arg in exc_args: try: pickle.dumps(arg) safe_exc_args.append(arg) except Exception: # pylint: disable=broad-except safe_exc_args.append(safe_repr(arg)) self.exc_module = exc_module self.exc_cls_name = exc_cls_name self.exc_args = safe_exc_args self.text = text Exception.__init__(self, exc_module, exc_cls_name, safe_exc_args, text)
def get(self, id): '''Get a tasks status given its ID''' result = AsyncResult(id, app=celery) status, retval = result.status, result.result data = {'id': id, 'status': status, 'result': retval} if status in states.EXCEPTION_STATES: traceback = result.traceback data.update({ 'result': safe_repr(retval), 'exc': get_full_cls_name(retval.__class__), 'traceback': traceback, }) return data
def on_success(self, ret_value): """Handler called if the task was successfully processed.""" state.task_ready(self) if self.task.acks_late: self.acknowledge() runtime = self.time_start and (time.time() - self.time_start) or 0 self.send_event("task-succeeded", uuid=self.task_id, result=safe_repr(ret_value), runtime=runtime) self.logger.info(self.success_msg.strip() % { "id": self.task_id, "name": self.task_name, "return_value": self.repr_result(ret_value), "runtime": runtime})
def task_status(request, task_id): """Returns task status and result in JSON format.""" result = AsyncResult(task_id) #print dir(result) print result.state state, retval, success = result.state, False, result.successful() response_data = dict(task_id=task_id, status=state, result=retval, success=success) if state in states.EXCEPTION_STATES: traceback = result.traceback response_data.update({ 'result': safe_repr(retval), 'exc': get_full_cls_name(retval.__class__), 'traceback': traceback }) return JsonResponse(response_data)
def on_success(self, ret_value): """Handler called if the task was successfully processed.""" state.task_ready(self) if self.task.acks_late: self.acknowledge() runtime = self.time_start and (time.time() - self.time_start) or 0 self.send_event("task-succeeded", uuid=self.task_id, result=safe_repr(ret_value), runtime=runtime) self.logger.info( self.success_msg.strip() % { "id": self.task_id, "name": self.task_name, "return_value": self.repr_result(ret_value), "runtime": runtime })
def repr_result(self, result, maxlen=46): # 46 is the length needed to fit # "the quick brown fox jumps over the lazy dog" :) return truncate_text(safe_repr(result), maxlen)
def _inner(*args, **kwargs): try: retval = fun(*args, **kwargs) except Exception, exc: response = {'status': 'failure', 'reason': safe_repr(exc)}
def __repr__(self): return '<ModelEntry: {0} {1}(*{2}, **{3}) {4}>'.format( safe_str(self.name), self.task, safe_repr(self.args), safe_repr(self.kwargs), self.schedule, )
def _inner(*args, **kwargs): try: retval = fun(*args, **kwargs) except Exception, exc: response = {"status": "failure", "reason": safe_repr(exc)}
def from_exception(cls, exc): return cls(exc.__class__.__module__, exc.__class__.__name__, getattr(exc, 'args', []), safe_repr(exc))
def __repr__(self): return '<ModelEntry: %s %s(*%s, **%s) {%s}>' % (safe_str( self.name), self.task, safe_repr(self.args), safe_repr( self.kwargs), self.schedule)
def session_tasks( request ): """Gets task statuses from Celery API, appends to task dicts from session. This function is used to generate the list of pending/successful/failed tasks in the webapp page notification area. @param request: A Django request object @return tasks: a dict with task_id for key """ # basic tasks info from session: # task_id, action ('name' argument of @task), start time, args tasks = request.session.get(settings.CELERY_TASKS_SESSION_KEY, {}) # add entity URLs for task_id in tasks.keys(): task = tasks.get(task_id, None) if task and task['action'] in ['webui-file-new-master', 'webui-file-new-mezzanine', 'webui-file-new-access']: # Add entity_url to task for newly-created file repo,org,cid,eid = task['entity_id'].split('-') task['entity_url'] = reverse('webui-entity', args=[repo,org,cid,eid]) # Hit the celery-task_status view for status updates on each task. # get status, retval from celery # TODO Don't create a new ctask/task dict here!!! >:-O traceback = None for task_id in tasks.keys(): # Skip the HTTP and get directly from Celery API # djcelery.views.task_status result = AsyncResult(task_id) state, retval = result.state, result.result response_data = {'id': task_id, 'status': state, 'result': retval} if state in states.EXCEPTION_STATES: traceback = result.traceback response_data.update({'result': safe_repr(retval), 'exc': get_full_cls_name(retval.__class__), 'traceback': traceback}) # end djcelery.views.task_status task = response_data # construct collection/entity/file urls if possible if task: ctask = tasks[task['id']] ctask['status'] = task.get('status', None) ctask['result'] = task.get('result', None) # try to convert 'result' into a collection/entity/file URL if (ctask['status'] != 'FAILURE') and ctask['result']: r = ctask['result'] if type(r) == type({}): if r.get('id', None): oid = Identifier(r['id']) object_url = reverse('webui-%s' % oid.model, args=oid.parts.values()) ctask['%s_url' % oid.model] = object_url tasks[task['id']] = ctask # pretty status messages for task_id in tasks.keys(): task = tasks[task_id] action = task.get('action', None) if action: messages = TASK_STATUS_MESSAGES.get(action, None) status = task.get('status', None) template = None if messages and status: template = messages.get(status, None) if template: msg = template.format(**task) task['message'] = msg # indicate if task is dismiss or not for task_id in tasks.keys(): task = tasks[task_id] if task.get('status', None): task['dismissable'] = (task['status'] in TASK_STATUSES_DISMISSABLE) # include traceback in task if present if traceback: task['traceback'] = traceback # done return tasks