我需要设置芹菜crontab在月底运行,甚至不同于月底的日期(28~31)。我知道如何在shell命令中设置crontab在月底运行,如下所示:
55 23 28-31 * * /usr/bin/test $( date -d '+1 day' +%d ) -eq 1 && exec something
但是在芹菜时间表上,我不知道怎么做这个设置。有没有办法在芹菜上安排月底运行的任务?
似乎唯一的方法就是覆盖celery.schedules.crontab
上的is_due
方法。
发布于 2017-10-04 11:51:59
如果你不介意开销的话--你可以在CELERYBEAT_SCHEDULE中设置每天运行的任务。
然后在任务本身中,您可以检查这一天是否是该月的最后一天:
import calendar
from datetime import datetime
@task
def task_to_run_at_end_of_month():
today = datetime.today()
day_in_month = today.day
month = today.month
year = today.year
day_of_week, number_of_days_in_month = calendar.monthrange(year, month)
if day_in_month != number_of_days_in_month:
# not last day of month yet, do nothing
return
# process stuff on last day of month
...
发布于 2015-06-23 06:44:28
Celery附带了一个module,它就可以做到这一点。
从命令行运行它的方法如下所示
celery -A $project_name beat
通常您会使用worker
而不是beat
然后在您的celery_config.py
中包含CELERYBEAT_SCHEDULE
的定义。像这样的东西
from celery.schedules import crontab
CELERYBEAT_SCHEDULE = {
"end_of_month_task": {
"task": "module.task_name", # this is the task name
"schedule": crontab(0, 0, day_of_month=0),
"args": the_arguments_to_this_function
}
}
https://stackoverflow.com/questions/30978329
复制相似问题