[Python] How can I connect to Celery/Redis on Heroku?

Discussão em 'Python' iniciado por Stack, Janeiro 18, 2025.

  1. Stack

    Stack Membro Participativo

    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)

    Continue reading...

Compartilhe esta Página