def bg_runner(proxy_task, *args, **kwargs): """ Executes the function attached to task. Used to enable threads. """ task = None try: func = getattr(proxy_task, 'task_function', None) task_name = getattr(proxy_task, 'name', None) task_qs = Task.objects.get_task(task_name=task_name, args=args, kwargs=kwargs) if task_qs: task = task_qs[0] if func is None: raise BackgroundTaskError("Function is None, can't execute!") func(*args, **kwargs) if task: # task done, so can delete it task.increment_attempts() completed = task.create_completed_task() task_successful.send(sender=task.__class__, task_id=task.id, completed_task=completed) task.delete() logging.info('Ran task and deleting %s', task) except Exception as ex: t, e, traceback = sys.exc_info() if task: logging.warn('Rescheduling %s', task, exc_info=(t, e, traceback)) task_error.send(sender=ex.__class__, task=task) task.reschedule(t, e, traceback) del traceback
def bg_runner(proxy_task, task=None, *args, **kwargs): """ Executes the function attached to task. Used to enable threads. If a Task instance is provided, args and kwargs are ignored and retrieved from the Task itself. """ try: func = getattr(proxy_task, 'task_function', None) if isinstance(task, Task): args, kwargs = task.params() else: task_name = getattr(proxy_task, 'name', None) task_qs = Task.objects.get_task(task_name=task_name, args=args, kwargs=kwargs) if task_qs: task = task_qs[0] if func is None: raise BackgroundTaskError("Function is None, can't execute!") func(*args, **kwargs) if task: # task done, so can delete it task.increment_attempts() completed = task.create_completed_task() task_successful.send(sender=task.__class__, task_id=task.id, completed_task=completed) task.delete() logging.info('Ran task and deleting %s', task) except Exception as ex: t, e, traceback = sys.exc_info() if task: logging.warn('Rescheduling %s', task, exc_info=(t, e, traceback)) task_error.send(sender=ex.__class__, task=task) task.reschedule(t, e, traceback) del traceback