Celery beat running tasks every minute even thought It's set for every two hours

Viewed 6867

I'm trying to use celery beat to run tasks daily at a specific time. However for testing purposes, I'm setting up two tasks to run every two hours, this is what my config looks like:

CELERYBEAT_SCHEDULE = {
    'daily-google-connect': {
        'task': 'app.engine.schedule_fetcher',
        'schedule': crontab(hour='*/2'),
        'args': (['G'])
    },
    'daily-facebook-connect': {
        'task': 'app.engine.schedule_fetcher',
        'schedule': crontab(hour='*/2'),
        'args': (['F'])
    }
}

This is how I run celery:

celery beat -A app.engine.celery --schedule=/tmp/celerybeat-schedule --pidfile=/tmp/celerybeat.pid -l info

Everything runs in Docker containers using docker-compose so I make sure that I re-build the app's image and restart the containers.

I even enter into the running container and I see the crontab setup in the code... however in my logs, I see the task running every minute.

What else can I do to debug this?

I appreciate any help,

Thanks

1 Answers

Your crontab is configured to run „At every minute past every 2nd hour“.

from celery.schedules import crontab

str(crontab(hour='*/2'))
'<crontab: * */2 * * * (m/h/d/dM/MY)>'

Ref: https://crontab.guru/#*_*/2_*_*_*

Correct crontab for „Every two hours“ is: 0 */2 * * *.

Ref: https://crontab.guru/every-2-hours

This should fix your issue:

CELERYBEAT_SCHEDULE = {
    'daily-google-connect': {
        'task': 'app.engine.schedule_fetcher',
        'schedule': crontab(minute='0', hour='*/2'),
        'args': (['G'])
    },
    'daily-facebook-connect': {
        'task': 'app.engine.schedule_fetcher',
        'schedule': crontab(minute='0', hour='*/2'),
        'args': (['F'])
    }
}
Related