Schedule Tasks
You are able to run any Celery task at a specific time through eta
(means "Estimated Time of Arrival") parameter.
import datetime
import celery
@celery.shared_task(bind=True)
def add_tag(task, user_id, tag):
User.objects.filter(id=user_id, tags__ne=tag).update(push__tags=tag)
return True
user_id = '582ee32a5b9c861c87dc297e'
tag = 'new_tag'
started_at = datetime.datetime(2018, 3, 12, tzinfo=datetime.timezone.utc)
add_tag.apply_async((user_id, tag), eta=started_at)
ref:
http://docs.celeryproject.org/en/master/userguide/calling.html#eta-and-countdown
Revoke Tasks
Revoked tasks will be discarded until their eta.
from celery.result import AsyncResult
AsyncResult(task_id).revoke()
Revoking tasks works by sending a broadcast message to all the workers, the workers then keep a list of revoked tasks in memory. When a worker starts up it will synchronize revoked tasks with other workers in the cluster.
The list of revoked tasks is in-memory so if all workers restart the list of revoked ids will also vanish. If you want to preserve this list between restarts you need to specify a file for these to be stored in by using the –statedb
argument to celery worker
.
ref:
http://docs.celeryproject.org/en/latest/userguide/workers.html#worker-persistent-revokes