How do I schedule a task with Celery that runs on 1st of every month?
Asked Answered
P

3

16

How do I schedule a task with that runs on 1st of every month?

Plage answered 9/12, 2010 at 11:7 Comment(2)
Have you read celeryq.org/docs/reference/celery.schedules.html ?Slushy
@Deniz: Doesn't look like that covers DoM.Ooze
I
17

Since Celery 3.0 the crontab schedule now supports day_of_month and month_of_year arguments: http://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html#crontab-schedules

Incogitable answered 9/12, 2010 at 14:0 Comment(0)
R
10

You can do this using Crontab schedules and you cand define this either:

  • in your 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.
    },
}
Resolute answered 4/1, 2017 at 9:37 Comment(3)
I can't seem to find documentation on exactly how the '0's are used (I guess they are positional, minute/hour and are not optional, but not seen this explicitly stated)Cadre
It's just an interface to linux contrab have a look here to better understand the parameters and how it works: linuxconfig.org/linux-crontab-reference-guideResolute
when i added crontab(0,0,day_of_month='1') it seems to get executed every second.Anility
G
0

Use below crontab in your app.conf.beat_schedule(Celery Beat Scheduler):

crontab(hour=set_your_hours, minute=set_your_minutes, day_of_week="*",
        day_of_month=1, month_of_year="*")
Godfearing answered 2/2 at 11:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.