1

I'd like to extend the djcelery taskmeta table, for example I have a scheduled task that returns an integer value.

I'd like to add a field to store this value so I can graph this data along with the date_done field in the taskmeta table.

I tried to create a new model and making a foreignkey relationship with the taskmeta table, and inside my task add a row in this new table based on the task_id.

But I guess while the task executes, I won't be able to do, as I assume the taskmeta data has not been saved at this point, ie:

 if mover.request.id:
    try:
        task = TaskMeta.objects.get(task_id=mover.request.id)
                    extend_task_info = TaskExtended(task=task)
                    extend_task_info.save()
    except ObjectDoesNotExist:
        logger.error('Task ID: %s not in the DB' % mover.request.id)

Addition of task code:

@task()
def mover():
    for root, dirs, files in os.walk(src_dir):
        path = root.split('/')
        for file in files:
            counter = process_file(os.path.join(root, file))

    if mover.request.id:
        try:
            tn = TaskMeta.objects.get(task_id=mover.request.id)
            extend_task_info = TaskExtended(task=task)
            extend_task_info.counter = counter
            extend_task_info.save()
        except ObjectDoesNotExist:
            logger.error('Task ID: %s not in the DB' % mover.request.id)
Joe Lones
  • 479
  • 2
  • 6
  • 17

1 Answers1

1

Use task.AsyncResult(task_id). You can read more about it in the docs here.

Drewness
  • 5,004
  • 4
  • 32
  • 50
  • I'm sorry, not sure I totally understand. This would return an AsyncResult, no? How would this help me save extended info into the taskmeta table or extend to another while I'm in the task? Would you elaborate? Thanks. – Joe Lones Feb 28 '14 at 19:10
  • @JoeLones - Sure. Using `.AsyncResult()` gives you access to the task while it's running. Which is good in your case because you are trying to extend it before it exists, or not finished, hence the `ObjectDoesNotExist` exception. – Drewness Feb 28 '14 at 19:18
  • I amended my op and added my task code, still not sure I grasp it. In the code above wouldn't the exception always get trigger. Isn't the taskmeta data save once the task ends? – Joe Lones Feb 28 '14 at 19:45
  • Correct, it would always raise the exception because the task is not saved until it completes. `.AsyncResult()` allows you to access the task, well asynchronously, while it's running. – Drewness Feb 28 '14 at 19:52
  • Right, so how would I save the TaskExtended info if I don't have an instance of the TaskMeta I refer to via the foreign key? It's not possible then? – Joe Lones Feb 28 '14 at 20:02
  • `.AsyncResult()` returns the instance of the running task. Maybe it would be helpful if you posted your `TaskExtended()` class. – Drewness Feb 28 '14 at 20:08