Avinash Bakshi
Avinash Bakshi

Reputation: 319

How to customize celery job to work at a specific time given by user?

I have to create an application with user interface using django , So now I will have a user interface where user will select certain mp3.file from drop down and he will select some date and time .At that time my server has to schedule that particular job and it has to do it on time.

In order to do this in python I have tried schedule, apschedule now but i was unable to run a job at given time (there can be many jobs to run)

Then I got a suggestion like using celery-- I have got few doubts here :

  1. Is celery used only for scheduling n number of jobs only at predefined time?

2.Can I run a job after filtering datatime and filename from database using celery ?

  1. Is this the only way to schedule a job in celery or can we customize it by filtering data from database?

CELERYBEAT_SCHEDULE = {
    # Executes every Monday morning at 7:30 A.M
    'add-every-monday-morning': {
        'task': 'tasks.add',
        'schedule': crontab(hour=7, minute=30, day_of_week=1),
        'args': (16, 16),
    },
}

Upvotes: 1

Views: 1021

Answers (2)

itzMEonTV
itzMEonTV

Reputation: 20339

There are 2 ways. One is countdown and other is eta. Any way eta will convert to countdown behind the scenes.

>>> from datetime import datetime, timedelta

>>> tomorrow = datetime.utcnow() + timedelta(days=1) #will work after 24 hour
>>> add.apply_async((2, 2), eta=tomorrow)

For more http://docs.celeryproject.org/en/latest/userguide/calling.html#eta-and-countdown

Upvotes: 2

rafalmp
rafalmp

Reputation: 4068

You can schedule your tasks dynamically using ETA parameter, for example using DateTime obtained from a database model:

dt = SomeModel.objects.get(pk=12345).datetimefield
add.apply_async((2, 2), eta=dt)

eta must be a datetime object with timezone information. The task will be executed at or some time after the specified date and time - delays may arise because of many tasks waiting in queue or network latency. For more information see the docs here.

Upvotes: 2

Related Questions