I'am trying to send daily mail with celery. I can see my task in worker. But celery beat doesn't see my task. Current schedule is empty and stuck starting...
Here is my config for celery in "init.py":
from celery import Celery
from celery.schedules import crontab
def make_celery(app):
celery = Celery(app.import_name)
celery.conf.update(app.config["CELERY_CONFIG"])
class ContextTask(celery.Task):
def __call__(self, *args, **kwargs):
with app.app_context():
return self.run(*args, **kwargs)
celery.Task = ContextTask
return celery
app.config.update(CELERY_CONFIG={
'broker_url': 'redis://localhost:6379',
'result_backend': 'redis://localhost:6379',
})
app.config['CELERYBEAT_SCHEDULE'] = {
'send-daily-mail' : {
'task' : 'send_daily_mail',
'schedule' : crontab(minute = "*/1")
},
}
celery_app = make_celery(app)
And here is my task for testing in "routes.py" :
@celery_app.task(name='send_daily_mail')
def send_daily_email():
current_user_mail = "[email protected]"
msg = Message("This is the test!",
sender = "[email protected]",
recipients = [current_user_mail])
msg.body = 'This is test now!'
mail.send(msg)
print("Test Mail Has Been Sended!")
When I start to worker I see my task in there :
celery -A crm.celery_app worker --pool=gevent -l info
But when I try to start celery beat, current schedule is return empty and stuck starting...
celery -A crm.celery_app beat -l debug
I tried the solutions in all the posts about this topic and none of them worked. What could be the reason for this? Thank you from now.
CodePudding user response:
I fixed problem. Solution is :
@celery_app.on_after_finalize.connect
def setup_periodic_tasks(sender, **kwargs):
sender.add_periodic_task(60.0,send_daily_email.s(), name='send_daily_mail')
After editing this function according to your code first start worker with this :
celery -A crm.celery_app worker --pool=solo -l info
And start beat with this :
celery -A crm.celery_app beat -l debug
This is worked for me!