I have a Flask application deployed on Heroku. I'm trying to set up Celery using Redis as the message broker.
This error comes up in heroku logs:
2021-10-16T15:08:12.851586 00:00 app[worker.1]: [2021-10-16 15:08:12,851: ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [Errno 111] Connection refused.
Procfile:
worker: celery -A my_app.celery worker --loglevel=debug
The Heroku Redis add-on is installed, and I used this command to start a dyno: heroku ps:scale worker=1
app/__init__.py
...
from extensions import celery
...
def create_app(config_class=Config):
app = Flask(__name__)
app.config.from_object(Config)
...
celery.init_app(app)
app/extensions.py
import flask
from celery import Celery
class FlaskCelery(Celery):
def __init__(self, *args, **kwargs):
super(FlaskCelery, self).__init__(*args, **kwargs)
self.patch_task()
if 'app' in kwargs:
self.init_app(kwargs['app'])
def patch_task(self):
TaskBase = self.Task
_celery = self
class ContextTask(TaskBase):
abstract = True
def __call__(self, *args, **kwargs):
if flask.has_app_context():
return TaskBase.__call__(self, *args, **kwargs)
else:
with _celery.app.app_context():
return TaskBase.__call__(self, *args, **kwargs)
self.Task = ContextTask
def init_app(self, app):
self.app = app
self.config_from_object(app.config)
celery = FlaskCelery()
config.py
class Config(object):
...
CELERY_BROKER_URL = os.environ.get('REDIS_URL') or 'redis://localhost:6379/0'
CELERY_BACKEND_URL = os.environ.get('REDIS_URL') or 'redis://localhost:6379/0'
...
I'm wondering if maybe I'm using the wrong command in the Procfile. The Heroku documentation recommends using worker: celery worker --app=tasks.app
, but this produces an error saying the syntax is outdated. The command I'm using is the same one I run locally to start a celery worker
I also tried to manually set the broker and backend urls to the Heroku Redis url redis://:p8... .amazonaws.com:1...
, but I received the same error.
CodePudding user response:
Changing the config variables fixes this issue:
CELERY_BROKER_URL
-> BROKER_URL
CELERY_BACKEND_URL
-> CELERY_RESULT_BACKEND
This is described in the Heroku Documentation
However, it's the case that these config variables are not updated per the Celery documentation. There, it's recommended to use broker_url
and result_backend
, and you'll receive a deprecation warning using the other variable names.