[Answer]-HTTP call after celery task have changed state

0πŸ‘

βœ…

Here is some code that seems to do what I want:

In django settings:

CELERY_ANNOTATIONS = {
    "*": {
        "on_failure": celery_handlers.on_failure,
        "on_success": celery_handlers.on_success
    }
}

In the celery_handlers.py file included:

def on_failure(self, exc, task_id, *args, **kwargs):
    # Use urllib or similar to poke eg; api-int.mysite.com/task_handler/TASK_ID
    pass

def on_success(self, retval, task_id, *args, **kwargs):
    # Use urllib or similar to poke eg; api-int.mysite.com/task_handler/TASK_ID
    pass

And then you can just setup api-int to use something like:

from celery.result import AsyncResult
task_obj = AsyncResult(task_id)
# Logic to handle task_obj.result and related goes here....
πŸ‘€xeor

1πŸ‘

So, what are you mean when you tell tell to Django? Is I understand you right, django request which initiliazed a Celery task, is still alive a time when this task is finished? I that case you can check some storage ( database, memcached, etc ). and send your SSE.
Look, there is one way to do that.
1. You django view send task to Celery, after that it goes to infinite loop ( or loop with timeout 60sec?) and waits results in memcached.

  1. Celery gets task executes, and pastes results to memcached.

  2. Django view gets new results, exit the loop and sends your SSE.

Next variant is

  1. Django view sends task to Celery, and returns

  2. Celery execute tasks, after executing it makes simple HTTP requests to your django app.

  3. Django receives a http request from Celery, parse params and send SSE to your user again

πŸ‘€Rustem

Leave a comment