Exemplo n.º 1
0
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
Exemplo n.º 2
0
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.
    """
    signals.task_started.send(Task)
    task_guid = None

    try:
        TaskCount.count += 1
        func = getattr(proxy_task, 'task_function', None)
        if isinstance(task, Task):
            args, kwargs = task.params()
        else:
            task_name = getattr(proxy_task, 'name', None)
            task_queue = getattr(proxy_task, 'queue', None)
            task_qs = Task.objects.get_task(task_name=task_name, args=args, kwargs=kwargs)
            if task_queue:
                task_qs = task_qs.filter(queue=task_queue)
            if task_qs:
                task = task_qs[0]

        if task and isinstance(task, Task):
            task_guid = '%s-%s' % (task.id, task.task_hash)
            TaskCount.task_guids.append(task_guid)

        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()
            signals.task_successful.send(sender=task.__class__, task_id=task.id, completed_task=completed)
            task.create_repetition()
            task.delete()
            logger.info('Ran task and deleting %s', task)

    except Exception as ex:
        t, e, traceback = sys.exc_info()
        if task:
            logger.warn('Rescheduling %s', task, exc_info=(t, e, traceback))
            signals.task_error.send(sender=ex.__class__, task=task)
            task.reschedule(t, e, traceback)
        del traceback

    finally:
        TaskCount.count -= 1
        if task_guid and task_guid in TaskCount.task_guids:
            TaskCount.task_guids.remove(task_guid)

    signals.task_finished.send(Task)
Exemplo n.º 3
0
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 len(task_qs) == 0:
            raise BackgroundTaskError("No matching task found!")
        task = task_qs[0]
        if func is None:
            raise BackgroundTaskError("Function is None, can't execute!")
        func(*args, **kwargs)

        # task done, so can delete it
        completed = CompletedTask(task_name=task.task_name,
                                  task_params=task.task_params,
                                  task_hash=task.task_hash,
                                  priority=task.priority,
                                  run_at=timezone.now(),
                                  attempts=task.attempts,
                                  failed_at=task.failed_at,
                                  last_error=task.last_error,
                                  locked_by=task.locked_by,
                                  locked_at=task.locked_at)
        completed.save()
        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.reschedule(t, e, traceback)
        del traceback