2015-06-22 77 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 
    ...