I keep getting the following error:
ERROR/MainProcess] consumer: Cannot connect to redis://localhost:6379/0: Error 99 connecting to localhost:6379. Cannot assign requested address..
I checked everything and can’t find the answer why it’s happening.
I have docker container running for Redis and Celery Broker, and I can see that Celery accepts tasks but can’t connect to Redis and doesn’t turn task over for following execution.
Here’s my __init__
file as per the Celery docs:
from .celery import app as celery_app
__all__ = ('celery_app',)
In the celery.py
I have following code:
import os
from celery import Celery
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
app = Celery('Notification')
# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')
# Load task modules from all registered Django app configs.
app.conf.task_default_queue = 'notification'
app.autodiscover_tasks()
This is my Redis connection in setting_model.py
:
class RedisConfig(BaseModel):
host: str = "redis" # name of container
port: int = 6379
db: int = 0
class Config:
arbitrary_types_allowed = True
def get_redis_connection_str(self) -> str:
"""
Return redis connection string
"""
return f"redis://{self.host}:{self.port}/{self.db}"
In the settings.py
I have the following:
# Celery
CELERY_BROKER_URL = env.str(
"REDIS_URL", default=RedisConfig().get_redis_connection_str()
)
Here’s my docker-compose file:
notification-celery:
build:
context: ./notification
dockerfile: Dockerfile
command: [ "celery", "-A", "project", "worker", "-l", "info", "-Q", "notification" ]
volumes:
- ./volumes/notification/static:/app/static
- ./volumes/notification/media:/app/media
env_file: 'index.env'
networks:
- qoovee
depends_on:
- redis
restart: always
redis:
image: redis:6.0.9
restart: always
ports:
- "6379:6379"
expose:
- 6379
healthcheck:
test: [ "CMD", "redis-cli", "-h", "localhost", "ping" ]
interval: 1m
timeout: 5s
networks:
- qoovee
I checked everything and don’t get why it doesn’t work.
I searched through Internet as well but non of the advices helped me so hope here I can find one!