Search code examples
djangocelerydjango-celerycelery-task

Storing a task id for each celery task in database


I have using celery with MySQL. I want to to store the task id as a plain integer in a database or in a variable of celery task. How can I do that?


Solution

  • Why not create a celery task model, and save a celery task id to that model?

    class CeleryModel(models.Model):
        celery_task_id = models.CharField(max_length = 50, unique=True)
    

    Then:

    def some_celery_task():
        result = celery_task.delay()
        celery_task = CeleryModel(celery_task_id = result.id)
        celery_task.save() # added save line
    

    Your integer value would then be: celery_task.id to correspond with the actual, unique celery_task_id.

    UPDATE: another way ...

    First python manage.py inspectdb > inspectdb.py. Inside that file you will find:

    class CeleryTaskmeta(models.Model):
        id = models.IntegerField(primary_key=True)
        task_id = models.CharField(max_length=765, unique=True)
        status = models.CharField(max_length=150)
        result = models.TextField(blank=True)
        date_done = models.DateTimeField()
        traceback = models.TextField(blank=True)
        hidden = models.IntegerField()
        meta = models.TextField(blank=True)
        class Meta:
            db_table = u'celery_taskmeta'
    

    Next, python manage.py startapp celery_model. Put this file in the models.py file. I use south, so my final step would be python manage.py convert_app celery_model. However, its unnecessary. Now you have django level access to this celery datatable, and can read the primary key for each task as your integer value. E.g

    >>> ct = CeleryTaskmeta.objects.get(id=1)
    >>> for k,v in ct.__dict__.items(): print k,v
    ... 
    status SUCCESS
    task_id 2fa95f24-7640-434c-9fef-0732ac1d23c7
    date_done 2013-02-17 19:22:56+00:00
    traceback None
    _state <django.db.models.base.ModelState object at 0x10263fa90>
    meta eJxrYKotZAzlSM7IzEkpSs0rZIotZC7WAwBREgb9
    result gAJLBC4=
    hidden 0
    id 1
    

    Someone clever would know how to make your CeleryTaskmeta a read-only model because I don't think you would want to tamper with the datatable.

    UPDATE: to the last part of your question:

    >>> from celerytest.tasks import add
    >>> result = add.delay()
    >>> result.int_id = 1
    >>> for k,v in result.__dict__.items(): print k,v
    ...
    parent None
    app <Celery default:0x10264df10>
    task_name celerytest.tasks.add
    int_id 1
    id 01503afd-d196-47af-8e10-e7dc06603cfc
    backend <djcelery.backends.database.DatabaseBackend object at 0x1026842d0>