U
U
undeadter2019-04-21 23:42:20
Django
undeadter, 2019-04-21 23:42:20

How to start a Celery task with a time delay?

I have a Django model that has an is_activated field. When a new model object is created, is_activated is automatically set to False. In order for this field to become True, the user must fulfill a certain condition within 20 minutes. If within 20 minutes the user has not fulfilled this condition, delete the account.
Whether it is possible to set a timer for a task to delay the execution of the task. Those. queue the task only 20 minutes after saving the model object to check is_activated.
Or is it better to run the task every n-seconds and check the creation time of the model object?
Or are there better solutions to this problem?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
F
FulTupFul, 2019-04-22
@undeadter

And I think it's better to use regular tasks with a delay in time:

@app.task
def main(id):
    pass

main.apply_async(kwargs={"id": id}, countdown=20 * 60)

I
Ivan Shumov, 2019-04-21
@inoise

Add the expired_at field and use it to block attempts to log in after it expires. And you can clean accounts using cron at least once a minute

A
alfss, 2019-04-22
@alfss

https://docs.celeryproject.org/en/latest/userguide...

A
Artem, 2019-04-22
@ulkoart

celery
Periodic Tasks are great for this. expired_at is also a solution, but again it will be necessary to run a task to delete such records once every N time, unless of course there is a task to store them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question