Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to schedule weekday-aware jobs in celery

Is it possible to configure a sophisticated schedule with celery beat? For example, something like this:

On Monday-Friday, do job A with parameters (x1, y1), then do job B On Saturday, Sunday, do job A with parameters (x2, y2), don't do job B

I know I can implement a high frequency "tick" task that will check for this schedule, but I don't want to reinvent the wheel if something for this already exists.

like image 512
Goro Avatar asked Feb 20 '13 18:02

Goro


People also ask

How do I schedule a task in Django Celery?

Here are some key points: DJANGO_SETTINGS_MODULE must be set in the environment before starting a Celery process. Its presence in the environment triggers internal magic in Celery to run the Django setup at the right time. The Celery "application" must be created and configured during startup of both Django and Celery.

What is Shared_task in Celery?

The "shared_task" decorator allows creation of Celery tasks for reusable apps as it doesn't need the instance of the Celery app. It is also easier way to define a task as you don't need to import the Celery app instance.

What is Celery job scheduler?

Celery is an asynchronous task queue/job queue based on distributed message passing. It is focused on real-time operations but supports scheduling as well. The execution units, called tasks, are executed concurrently on one or more worker servers using multiprocessing, Eventlet, or gevent.


1 Answers

Sure, use a crontab schedule for your tasks.

from celery.schedules import crontab

CELERYBEAT_SCHEDULE = {
    # Executes every weekday morning at 7:30 A.M
    'weekdays': {
        'task': 'tasks.A',
        'schedule': crontab(hour=7, minute=30, day_of_week='1-5'),
        'args': (x1, y1),
    },
    # Executes saturday at 4:00 A.M
    'saturday': {
        'task': 'tasks.B',
        'schedule': crontab(hour=4, minute=0, day_of_week='sat'),
        'args': (x1, y1),
    },
    # Executes sunday morning at 2:15 A.M
    'sunday': {
        'task': 'tasks.A',
        'schedule': crontab(hour=2, minute=15, day_of_week='sun'),
        'args': (x2, y2),
    },
}
like image 97
Martijn Pieters Avatar answered Sep 19 '22 08:09

Martijn Pieters