如何安排每月1日运行的Celery任务?

时间:2010-12-09 11:07:56

标签: python django scheduled-tasks scheduling celery

如何安排每月1日运行的任务?

2 个答案:

答案 0 :(得分:13)

自Celery 3.0起,c​​rontab计划现在支持day_of_monthmonth_of_year参数:http://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html#crontab-schedules

答案 1 :(得分:2)

您可以使用Crontab schedules执行此操作,然后您可以定义:

  • 在您的django settings.py
from celery.schedules import crontab

CELERYBEAT_SCHEDULE = {
    'my_periodic_task': {
        'task': 'my_app.tasks.my_periodic_task',
        'schedule': crontab(0, 0, day_of_month='1'), # Execute on the first day of every month.
    },
}
  • in celery.py config:
from celery import Celery
from celery.schedules import crontab

app = Celery('app_name')
app.conf.beat_schedule = {
    'my_periodic_task': {
        'task': 'my_app.tasks.my_periodic_task',
        'schedule': crontab(0, 0, day_of_month='1'), # Execute on the first day of every month.
    },
}