Search code examples
djangoubuntucelerydjango-celery

Celery executing scheduled task a hundred times


I've configured celery in my django app in order to run a task every morning. The task simply sends an email to a group of users. The problem is that the same email is being sent a few hundred times!!

This is my celery config:

BROKER_URL = 'redis://127.0.0.1:6379/0'
BROKER_TRANSPORT = 'redis'
CELERYBEAT_SCHEDULER = 'djcelery.schedulers.DatabaseScheduler'

from celery.schedules import crontab

CELERYBEAT_SCHEDULE = {
    'alert_user_is_not_buying-everyday-at-7': {
        'task': 'opti.tasks.alert_users_not_buying',
        'schedule': crontab(hour=7, minute=0),
    },
}

and the task is:

@app.task(bind=True)
def alert_user_is_not_buying(self):
    send_mail_to_users()

And I use this commands to start the worker and beat (I use supervisor for that):

exec celery --app=opti beat --loglevel=INFO
exec celery --app=opti worker --loglevel=INFO

I believe that there's no problem wih my send_mail_to_users() method, It looks like the emails are sent every 30 seconds....

What is missing?


Solution

  • Your CELERYBEAT_SCHEDULE setting is likely going unused, as you have CELERYBEAT_SCHEDULER set to use the DatabaseScheduler. How is that scheduler configured? I would guess that's where the problem is coming from.