Reputation: 103
I want to use celery to execute a periodic task.
According to the documentation: http://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html#entries, I wrote down the following code:
app.conf.beat_schedule = {
'test_celery': {
'task': 'tasks.login.test_timertask',
'schedule': 60.0,
}
}
but when I run it as follows:
celery beat -A tasks.login --loglevel=info
the first task is executed after 60s.
I want to start the task once the worker starts rather than with 60 seconds delay. What should I do?
Upvotes: 1
Views: 2243
Reputation: 2652
It's pretty daft that there's no setting to start periodic tasks immediately, so I guess the only true solution is to just start the task manually before adding it as a periodic task, or using your own scheduler class that sets the last time started before now.
Upvotes: 0
Reputation: 23134
You can use crontab schedules for this:
from celery.schedules import crontab
app.conf.beat_schedule = {
'test_celery': {
'task': 'tasks.login.test_timertask',
'schedule': crontab(), # default, executes every minute
}
}
But you must be aware of the following (as the docs state):
A Crontab like schedule also exists, see the section on Crontab schedules.
Like with cron, the tasks may overlap if the first task doesn’t complete before the next. If that’s a concern you should use a locking strategy to ensure only one instance can run at a time (see for example Ensuring a task is only executed one at a time).
Upvotes: 2