In my project, I use django celery beat package to execute scheduled tasks. It works well but I have one case that I can't handle.
All the tasks have a PeriodicTack that schedules them.
So the following task:
from celery import shared_task
@shared_task
def foo(**kwargs):
# Here I can to things like this :
whatever_method(kwargs["bar"])
Don't know if it is luck but it turns out that kwargs "points" to the kwargs attribute of the PeriodicTask model.
My question is :
- How can I access the
PeriodicTaskinstance that made the task run ? - What if I have 2
PeriodicTaskthat use the same shared_task but with different schedules/parameters, will it find out which one was the source for that particular run ?
Thanks in advance for your help.
Ok I found a way to do this.
As I said in the comment, making use of
@app.tasksolves my needs.I end up with a task like this :
Where
appis the Celery app as described in the docs.The
bind=Trueis, as I understood, necessary to make the task having its own request and thus having access toselfwith information.