Skip to content

Commit 9f6216d

Browse files
authored
fix(redis): unify Redis and Celery configuration and initialize channel layer (#1024)
1 parent 190c797 commit 9f6216d

File tree

1 file changed

+72
-48
lines changed

1 file changed

+72
-48
lines changed

app/eventyay/config/settings.py

Lines changed: 72 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,6 @@ def instance_name(request):
9191
# URL settings
9292
ROOT_URLCONF = 'eventyay.multidomain.maindomain_urlconf'
9393

94-
95-
HAS_CELERY = config.has_option('celery', 'broker')
96-
if HAS_CELERY:
97-
CELERY_BROKER_URL = config.get('celery', 'broker') if not DEBUG else 'redis://localhost:6379/2'
98-
CELERY_RESULT_BACKEND = config.get('celery', 'backend') if not DEBUG else 'redis://localhost:6379/1'
99-
CELERY_TASK_ALWAYS_EAGER = False
100-
else:
101-
CELERY_TASK_ALWAYS_EAGER = True
102-
103-
10494
AUTH_USER_MODEL = 'base.User'
10595
_LIBRARY_APPS = (
10696
'bootstrap3',
@@ -740,45 +730,80 @@ def instance_name(request):
740730
'LOCATION': config.get('memcached', 'location'),
741731
}
742732

743-
HAS_REDIS = config.has_option('redis', 'location')
744-
if HAS_REDIS:
745-
redis_options = {
746-
'CLIENT_CLASS': 'django_redis.client.DefaultClient',
747-
'REDIS_CLIENT_KWARGS': {'health_check_interval': 30},
748-
}
749-
redis_tls_config = build_redis_tls_config(config)
750-
if redis_tls_config is not None:
751-
redis_options['CONNECTION_POOL_KWARGS'] = redis_tls_config
752-
redis_options['REDIS_CLIENT_KWARGS'].update(redis_tls_config)
753-
754-
if config.has_option('redis', 'password'):
755-
redis_options['PASSWORD'] = config.get('redis', 'password')
756-
757-
CACHES['redis'] = {
758-
'BACKEND': 'django_redis.cache.RedisCache',
759-
'LOCATION': config.get('redis', 'location') if not DEBUG else 'redis://localhost:6379/0',
760-
'OPTIONS': redis_options,
761-
}
762-
CACHES['redis_sessions'] = {
763-
'BACKEND': 'django_redis.cache.RedisCache',
764-
'LOCATION': config.get('redis', 'location') if not DEBUG else 'redis://localhost:6379/0',
765-
'TIMEOUT': 3600 * 24 * 30,
766-
'OPTIONS': redis_options,
767-
}
768-
REDIS_USE_PUBSUB = config.getboolean('redis', 'use_pubsub', fallback=True)
769-
if not HAS_MEMCACHED:
770-
CACHES['default'] = CACHES['redis']
771-
REAL_CACHE_USED = True
772-
if config.getboolean('redis', 'sessions', fallback=False):
773-
SESSION_ENGINE = 'django.contrib.sessions.backends.cache'
774-
SESSION_CACHE_ALIAS = 'redis_sessions'
733+
# Redis Configuration
734+
redis_connection_kwargs = {
735+
"retry": Retry(ExponentialBackoff(), 3),
736+
"health_check_interval": 30,
737+
}
738+
739+
REDIS_URL = config.get('redis', 'location') if not DEBUG else 'redis://localhost:6379/0'
740+
HAS_REDIS = bool(REDIS_URL)
741+
REDIS_HOSTS = [{
742+
"address": REDIS_URL,
743+
**redis_connection_kwargs,
744+
}]
745+
746+
REDIS_USE_PUBSUB = True
747+
748+
redis_options = {
749+
'CLIENT_CLASS': 'django_redis.client.DefaultClient',
750+
'REDIS_CLIENT_KWARGS': {'health_check_interval': 30},
751+
}
752+
redis_tls_config = build_redis_tls_config(config)
753+
if redis_tls_config is not None:
754+
redis_options['CONNECTION_POOL_KWARGS'] = redis_tls_config
755+
redis_options['REDIS_CLIENT_KWARGS'].update(redis_tls_config)
756+
757+
if config.has_option('redis', 'password'):
758+
redis_options['PASSWORD'] = config.get('redis', 'password')
759+
760+
CACHES['redis'] = {
761+
'BACKEND': 'django_redis.cache.RedisCache',
762+
'LOCATION': REDIS_URL,
763+
'OPTIONS': redis_options,
764+
}
765+
CACHES['redis_sessions'] = {
766+
'BACKEND': 'django_redis.cache.RedisCache',
767+
'LOCATION': REDIS_URL,
768+
'TIMEOUT': 3600 * 24 * 30,
769+
'OPTIONS': redis_options,
770+
}
771+
772+
# Channels (WebSocket) configuration
773+
CHANNEL_LAYERS = {
774+
"default": {
775+
"BACKEND": (
776+
"channels_redis.pubsub.RedisPubSubChannelLayer"
777+
if REDIS_USE_PUBSUB
778+
else "channels_redis.core.RedisChannelLayer"
779+
),
780+
"CONFIG": {
781+
"hosts": REDIS_HOSTS,
782+
"prefix": "eventyay:{}:asgi:".format(
783+
_config.get("redis", "db", fallback="0")
784+
),
785+
"capacity": 10000,
786+
},
787+
},
788+
}
789+
790+
if not HAS_MEMCACHED:
791+
CACHES['default'] = CACHES['redis']
792+
REAL_CACHE_USED = True
793+
if config.getboolean('redis', 'sessions', fallback=False):
794+
SESSION_ENGINE = 'django.contrib.sessions.backends.cache'
795+
SESSION_CACHE_ALIAS = 'redis_sessions'
775796

776797
if not SESSION_ENGINE:
777798
if REAL_CACHE_USED:
778799
SESSION_ENGINE = 'django.contrib.sessions.backends.cached_db'
779800
else:
780801
SESSION_ENGINE = 'django.contrib.sessions.backends.db'
781802

803+
# Celery configuration
804+
CELERY_BROKER_URL = config.get('celery', 'broker') if not DEBUG else 'redis://localhost:6379/2'
805+
CELERY_RESULT_BACKEND = config.get('celery', 'backend') if not DEBUG else 'redis://localhost:6379/1'
806+
CELERY_TASK_ALWAYS_EAGER = False if not DEBUG else True
782807
CELERY_TASK_SERIALIZER = "json"
783808
CELERY_RESULT_SERIALIZER = "json"
784809
CELERY_TASK_DEFAULT_QUEUE = "default"
@@ -791,12 +816,11 @@ def instance_name(request):
791816
)
792817
CELERY_BEAT_SCHEDULER = 'django_celery_beat.schedulers:DatabaseScheduler'
793818
CELERY_TASK_TRACK_STARTED = True
794-
CELERY_TASK_ROUTES = (
795-
[
796-
('eventyay.base.services.notifications.*', {'queue': 'notifications'}),
797-
('eventyay.api.webhooks.*', {'queue': 'notifications'}),
798-
],
799-
)
819+
CELERY_TASK_ROUTES = [
820+
('eventyay.base.services.notifications.*', {'queue': 'notifications'}),
821+
('eventyay.api.webhooks.*', {'queue': 'notifications'}),
822+
]
823+
800824

801825
# Static files (CSS, JavaScript, Images)
802826
# https://docs.djangoproject.com/en/5.1/howto/static-files/

0 commit comments

Comments
 (0)