Make sure to use the right client everywhere

This commit is contained in:
Antoine Nguyen
2025-03-26 14:24:48 +01:00
parent 5f4ad848ed
commit b434958a79
3 changed files with 13 additions and 22 deletions
+2 -13
View File
@@ -1,14 +1,12 @@
"""App related signal handlers."""
import redis
from django.conf import settings
from django.db.models import signals
from django.dispatch import receiver
from modoboa.admin import models as admin_models
from . import constants
from . import utils
def set_message_limit(instance, key):
@@ -16,16 +14,7 @@ def set_message_limit(instance, key):
old_message_limit = instance._loaded_values.get("message_limit")
if old_message_limit == instance.message_limit:
return
if not getattr(settings, "REDIS_SENTINEL", False):
rclient = redis.Redis(
host=settings.REDIS_HOST,
port=settings.REDIS_PORT,
db=settings.REDIS_QUOTA_DB,
)
else:
sentinel = redis.sentinel.Sentinel(settings.REDIS_SENTINELS, socket_timeout=0.1)
rclient = sentinel.master_for(settings.REDIS_MASTER, socket_timeout=0.1)
rclient = utils.get_redis_connection()
if instance.message_limit is None:
# delete existing key
if rclient.hexists(constants.REDIS_HASHNAME, key):
+2 -6
View File
@@ -22,6 +22,7 @@ from modoboa.lib.tests import ModoTestCase, ParametersMixin
from modoboa.policyd import core as policyd_core
from . import constants
from . import utils
def start_policy_daemon():
@@ -33,12 +34,7 @@ class RedisTestCaseMixin:
def setUp(self):
super().setUp()
self.rclient = redis.Redis(
host=settings.REDIS_HOST,
port=settings.REDIS_PORT,
db=settings.REDIS_QUOTA_DB,
)
self.rclient.set_response_callback("HGET", int)
self.rclient = utils.get_redis_connection()
self.rclient.delete(constants.REDIS_HASHNAME)
+9 -3
View File
@@ -9,9 +9,15 @@ from . import constants
def get_redis_connection():
"""Return a client connection to Redis server."""
rclient = redis.Redis(
host=settings.REDIS_HOST, port=settings.REDIS_PORT, db=settings.REDIS_QUOTA_DB
)
if not getattr(settings, "REDIS_SENTINEL", False):
rclient = redis.Redis(
host=settings.REDIS_HOST,
port=settings.REDIS_PORT,
db=settings.REDIS_QUOTA_DB,
)
else:
sentinel = redis.sentinel.Sentinel(settings.REDIS_SENTINELS, socket_timeout=0.1)
rclient = sentinel.master_for(settings.REDIS_MASTER, socket_timeout=0.1)
rclient.set_response_callback("HGET", int)
return rclient