我想安排每天在 16h UTC 运行两个任务。
为此,我实现了这个 celery config:
from celery.schedules import crontab
CELERY_IMPORTS = ('api.tasks')
CELERY_TASK_RESULT_EXPIRES = 30
CELERY_TIMEZONE = 'UTC'
CELERYBEAT_SCHEDULE = {
'book-task': {
'task': 'api.tasks.get_data',
# At 16h UTC everyday
'schedule': crontab(hour=16),
'args': ({'book'}),
},
'pencils-task': {
'task': 'api.tasks.get_data',
# At 16h UTC everyday
'schedule': crontab(hour=16),
'args': ({'pencil'}),
}
}
在运行 celery beat -A app.celery
以启动 celery 节拍之后,我运行 celery worker -A app.celery --loglevel=info --pool=solo
以运行 celery 工作程序。
使用上面的 config ,我的两个任务从 UTC 时间 16 小时开始每分钟运行一次。我的 config 有什么问题以及如何解决?
回答1
您需要在 crontab
中指定 minute
,不传递 minute
意味着 https://docs.celeryq.dev/en/stable/reference/celery.schedules.html#celery.schedules.crontab,它每分钟运行一次作业。
传递 minute=0
以在整点开始运行
from celery.schedules import crontab
CELERY_IMPORTS = ('api.tasks')
CELERY_TASK_RESULT_EXPIRES = 30
CELERY_TIMEZONE = 'UTC'
CELERYBEAT_SCHEDULE = {
'book-task': {
'task': 'api.tasks.get_data',
# At 16h UTC everyday
'schedule': crontab(minute=0, hour=16),
'args': ({'book'}),
},
'pencils-task': {
'task': 'api.tasks.get_data',
# At 16h UTC everyday
'schedule': crontab(minute=0, hour=16),
'args': ({'pencil'}),
}
}