2015-06-22 79 views
1

我需要设置芹菜crontab在月末(28〜31)与月末运行。 我知道如何设置的crontab运行在本月的shell命令是这样结尾:如何使用在月底运行的芹菜来安排任务?

55 23 28-31 * * /usr/bin/test $(date -d '+1 day' +%d) -eq 1 && exec something 

但在芹菜日程我不知道如何做到这一点的设置。 有没有什么办法可以安排在芹菜月底运行的任务?
似乎唯一的方法就是覆盖celery.schedules.crontab上的is_due方法。

+0

有谁知道解决办法? –

+0

我找到了在celery.schedules.crontab上编辑is_due方法的方法。这是解决问题的唯一方法吗? –

回答

0

芹菜附带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 
     } 
    } 
+0

嗨感谢您的回答。我试过你的代码,但我得到了无效的corntab错误: 'ValueError:无效的crontab模式。有效范围是1-31。 '0'被发现。' 我使用芹菜版本3.1。 –

+0

啊,你说得对。我认为这段代码使用了内建的'crontab'类,但它实际上是一个覆盖'is_due'方法的自定义类。 – mehtunguh

+0

你能告诉我一个自定义的'crontab'类的链接或代码吗? –

0

如果你不介意的一点开销 - 你可以设置为每天运行在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 
    ...