Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django celery crontab every 30 seconds - is it even possible?

Is it possible to run the django celery crontab very 30 seconds DURING SPECIFIC HOURS?

There are only settings for minutes, hours and days.

I have the crontab working, but I'd like to run it every 30 seconds, as opposed to every minute.

Alternatively...

Is it possible to turn the interval on for 30 seconds, and turn on the interval schedule only for a certain period of the day?

like image 276
snakesNbronies Avatar asked May 15 '12 08:05

snakesNbronies


2 Answers

You could just use

from datetime import timedelta

@periodic_task(run_every=timedelta(seconds=30))
def thirty_second_task():
   now = datetime.now()
   if now.hour == 12 or now.hour == 9:
       your code goes here`

That will run the function every thirty seconds, but it will only run your code if the hour value of the current time is 12 or 9.

like image 146
kophygiddie Avatar answered Sep 21 '22 10:09

kophygiddie


Very first example they have in the documentation is...

Example: Run the tasks.add task every 30 seconds.

from datetime import timedelta

CELERYBEAT_SCHEDULE = {
    "runs-every-30-seconds": {
        "task": "tasks.add",
        "schedule": timedelta(seconds=30),
        "args": (16, 16)
    },
}
like image 31
vartec Avatar answered Sep 21 '22 10:09

vartec