Everytime I call my api endpoint that uses Celery, I get:
ERROR:celery.backends.redis:Connection to Redis lost: Retry (0/20) now.
I don't understand because everything works on my local machine, but not on Heroku.
I have configured the REDIS_URL environment variable and tested that the AWS redis host also works on my local machine.
I can't figure out what the problem is.
Also, when I push my code to Heroku, I see this in the logs:
Connected to rediss://:**@ec2-34-194-16-21pute-1.amazonaws:11930//
but when I call my endpoint I get this
ERROR:celery.backends.redis:Connection to Redis lost: Retry (0/20) now.
Here is a code snippet.
celery_app.py
from celery import Celery
import ssl
def make_celery(app=None):
celery = Celery(
app.import_name if app else "celery_app",
broker=app.config.get('CELERY_BROKER_URL', 'redis://localhost:6379/0') if app else 'redis://localhost:6379/0',
backend=app.config.get('result_backend', 'redis://localhost:6379/0') if app else 'redis://localhost:6379/0'
)
if app:
celery.conf.update(app.config)
# Configure SSL for broker and backend if using rediss://
if celery.conf.broker_url.startswith('rediss://'):
celery.conf.update(
broker_use_ssl={
'ssl_cert_reqs': ssl.CERT_NONE # Enforce SSL validation
}
)
if celery.conf.result_backend.startswith('rediss://'):
celery.conf.update(
redis_backend_use_ssl={
'ssl_cert_reqs': ssl.CERT_NONE # Enforce SSL validation
}
)
return celery
app.py
app = Flask(__name__)
app.config['CELERY_BROKER_URL'] = os.getenv('REDIS_URL', 'redis://localhost:6379/0')
app.config['result_backend'] = os.getenv('REDIS_URL', 'redis://localhost:6379/0')
# Initialize Celery with Flask app
celery = make_celery(app)
Everytime I call my api endpoint that uses Celery, I get:
ERROR:celery.backends.redis:Connection to Redis lost: Retry (0/20) now.
I don't understand because everything works on my local machine, but not on Heroku.
I have configured the REDIS_URL environment variable and tested that the AWS redis host also works on my local machine.
I can't figure out what the problem is.
Also, when I push my code to Heroku, I see this in the logs:
Connected to rediss://:**@ec2-34-194-16-21.compute-1.amazonaws.com:11930//
but when I call my endpoint I get this
ERROR:celery.backends.redis:Connection to Redis lost: Retry (0/20) now.
Here is a code snippet.
celery_app.py
from celery import Celery
import ssl
def make_celery(app=None):
celery = Celery(
app.import_name if app else "celery_app",
broker=app.config.get('CELERY_BROKER_URL', 'redis://localhost:6379/0') if app else 'redis://localhost:6379/0',
backend=app.config.get('result_backend', 'redis://localhost:6379/0') if app else 'redis://localhost:6379/0'
)
if app:
celery.conf.update(app.config)
# Configure SSL for broker and backend if using rediss://
if celery.conf.broker_url.startswith('rediss://'):
celery.conf.update(
broker_use_ssl={
'ssl_cert_reqs': ssl.CERT_NONE # Enforce SSL validation
}
)
if celery.conf.result_backend.startswith('rediss://'):
celery.conf.update(
redis_backend_use_ssl={
'ssl_cert_reqs': ssl.CERT_NONE # Enforce SSL validation
}
)
return celery
app.py
app = Flask(__name__)
app.config['CELERY_BROKER_URL'] = os.getenv('REDIS_URL', 'redis://localhost:6379/0')
app.config['result_backend'] = os.getenv('REDIS_URL', 'redis://localhost:6379/0')
# Initialize Celery with Flask app
celery = make_celery(app)
Share
Improve this question
asked Jan 19 at 0:08
davidocodesdavidocodes
1551 gold badge1 silver badge9 bronze badges
1 Answer
Reset to default 0Heroku Key-Value Store (Redis) is configured to disconnect after 5 minutes of inactivity by default. If this is what you see, the app should be able to try reconnecting when connection is detected lost.