django-celery之定时任务

celery定时任务

Posted by dzt on January 14, 2019

前言

之前我详细的写了celery异步任务的创建和使用

这篇就文章详细的写如何创建一个定时任务

配置settings

基于之前的settings配置增加

CELERY_IMPORTS = ('tools.tasks',) # 导入tasks.py
CELERYBEAT_SCHEDULER = 'djcelery.schedulers.DatabaseScheduler'  # 定时任务
# 下面是定时任务的设置,我一共配置了三个定时任务.
from celery.schedules import crontab
CELERYBEAT_SCHEDULE = {
    # 定时任务: 每1分钟,执行任务(listen_ngx_celery)
    u'监听手表服务器': {
        'task': 'tools.tasks.listen_ngx_celery', 
        'schedule': crontab(minute='*'),
        "args": ()
    },
}

listen_ngx_celery为tasks.py中的定时函数

官方文档中 时间的设定

class celery.schedules.crontab(minute=u'*', hour=u'*', day_of_week=u'*', day_of_month=u'*', month_of_year=u'*', **kwargs)

You can specify a minute, an hour, a day of the week, a day of the month, and/or a month in the year in any of the following formats:

  • minute

    A (list of) integers from 0-59 that represent the minutes of an hour of when execution should occur; orA string representing a Crontab pattern. This may get pretty advanced, like minute='*/15' (for every quarter) or minute='1,13,30-45,50-59/2'.

  • hour

    A (list of) integers from 0-23 that represent the hours of a day of when execution should occur; orA string representing a Crontab pattern. This may get pretty advanced, like hour='*/3' (for every three hours) or hour='0,8-17/2' (at midnight, and every two hours during office hours).

  • day_of_week

    A (list of) integers from 0-6, where Sunday = 0 and Saturday = 6, that represent the days of a week that execution should occur.A string representing a Crontab pattern. This may get pretty advanced, like day_of_week='mon-fri' (for weekdays only). (Beware that day_of_week='*/2' does not literally mean ‘every two days’, but ‘every day that is divisible by two’!)

  • day_of_month

    A (list of) integers from 1-31 that represents the days of the month that execution should occur.A string representing a Crontab pattern. This may get pretty advanced, such as day_of_month='2-30/3' (for every even numbered day) or day_of_month='1-7,15-21' (for the first and third weeks of the month).

  • month_of_year

    A (list of) integers from 1-12 that represents the months of the year during which execution can occur.A string representing a Crontab pattern. This may get pretty advanced, such as month_of_year='*/3' (for the first month of every quarter) or month_of_year='2-12/2' (for every even numbered month).

  • nowfun

    Function returning the current date and time (datetime).

  • app

    The Celery app instance.

基本的参数设置

crontab()

默认为minute=u’’, hour=u’’, day_of_week=u’’, day_of_month=u’’, month_of_year=u’*’

每分钟执行一次

crontab(hour='*/24')

每24小时执行一次

crontab(minute=30, hour=0)

每天的凌晨0:30执行一次

crontab(hour=7, minute=0,   day_of_month=1)

每个月的1号的7:00执行一次

crontab(hour=7, minute=30, day_of_week=1)

每个星期一的早上7:30执行一次

执行任务

test_celery为app

先启动celery

celery worker -A test_celery -l info

再启动定时任务

celery -A test_celery beat

beat与worker一起开启

celery -B -A test_celery worker -l info

本地效果查看

能看到每分钟就执行一次任务 就完成了

写得比较简单, 但我自己也踩了几个坑

挂在nginx服务器上就需要修改supervisord.conf中的command

或者在执行的.sh脚本中加入celery -A test_celery beat