In my web-application I start backgrounds jobs with celery without storing their id. Some of the task are periodic and some are triggered by user-interaction. The celery-tasks just do their thing and eventually the user will see the updated data in their browser. When a task has recently failed, I want to notify all logged-in admin-users about it (since they are usually the ones who triggered the recent failure). So they at least know something's up.
The relevant celery-methods I found, either require a valid task-id (e.g. celery.result.AsyncResult
) or they only have infos about active tasks, but not about finished/failed tasks (e.g. celery.app.control.Inspect
).
I am using a flask-frontend, a redis-backend for celery and also a regular DB for persistent data.
How would I collect information about recently finished or failed celery tasks in this scenario?
What I have tried:
# I setup celery with
my_celery_project = Celery(__name__,
backend='redis://localhost:1234/0',
broker='redis://localhost:1234/0')
# later in the view I want to collect status information:
i = my_celery_project.control.inspect()
i.active() # this one exists, but I don't care about it
i.failed() # this is what I want, but it doesn't exist
i.results() # this also doesn't exist
# getting the result directly also doesn't work, since they require an id, which i don't have
res = AsyncResult(id_i_don_have,app=app)
It should be possible, since I can get the results manually from redis with redis-cli --scan
and then do my_task.AsyncResult('id_from_redis').status
to check the result. Something similar to flower could also work, but that would't work so well with the state-less nature of a web-application, I think.
this is not a duplicate of these questions, since they don't assume a redis-backend. Also they are 4+ years out-of-date:
- Celery: list all tasks, scheduled, active *and* finished
- Python celery: Retrieve tasks arguments if there's an exception
- How can I get a list of succeeded Celery task ids?
this is not a duplicate of these questions, since my redis-backend is in fact working:
this is not a duplicate of this questions, since it is exactly the opposite to my questions. They care about old results, while I care explicitly only about recent results: How to read celery results from redis result backend