Skip to content

Commit

Permalink
Move RQ redis_connection helper into a mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
karls committed Oct 8, 2023
1 parent 3dab32f commit d61b5e5
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 24 deletions.
24 changes: 24 additions & 0 deletions judoscale/django/redis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import os

from redis import Redis


class RedisHelper:
def redis_connection(self, redis_config: dict = None) -> Redis:
"""
Return a Redis connection from an RQ queue configuration
"""
if redis_config is not None:
config = {k.lower(): v for k, v in redis_config.items()}
if redis_url := config.get("url"):
del config["url"]
return Redis.from_url(redis_url, **config)
else:
return Redis(**config)
elif redis_url := os.getenv("REDIS_URL"):
return Redis.from_url(redis_url)
else:
raise RuntimeError(
"Missing Redis connection configuration. Please set either "
"settings.JUDOSCALE['REDIS'] or REDIS_URL environment variable."
)
27 changes: 3 additions & 24 deletions judoscale/rq/apps.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import logging
import os

from django.apps import AppConfig
from django.conf import settings
from redis import Redis

from judoscale.core.config import config as judoconfig
from judoscale.core.reporter import reporter
from judoscale.django.redis import RedisHelper
from judoscale.rq import judoscale_rq

logger = logging.getLogger(__name__)


class JudoscaleRQConfig(AppConfig):
class JudoscaleRQConfig(AppConfig, RedisHelper):
name = "judoscale.rq"
label = "judoscale_rq"
verbose_name = "Judoscale (RQ)"
Expand All @@ -24,25 +23,5 @@ def ready(self):
logger.info("Not activated - No API URL provided")
return

judoscale_rq(self.redis_connection)
judoscale_rq(self.redis_connection(settings.JUDOSCALE.get("REDIS")))
reporter.ensure_running()

@property
def redis_connection(self):
"""
Return a Redis connection from an RQ queue configuration
"""
if redis_config := settings.JUDOSCALE.get("REDIS"):
config = {k.lower(): v for k, v in redis_config.items()}
if redis_url := config.get("url"):
del config["url"]
return Redis.from_url(redis_url, **config)
else:
return Redis(**config)
elif redis_url := os.getenv("REDIS_URL"):
return Redis.from_url(redis_url)
else:
raise RuntimeError(
"Missing Redis connection configuration. Please set either "
"settings.JUDOSCALE['REDIS'] or REDIS_URL environment variable."
)
50 changes: 50 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import pytest

from judoscale.core.config import Config, RuntimeContainer
from judoscale.django.redis import RedisHelper


class TestConfig:
Expand Down Expand Up @@ -142,3 +145,50 @@ def test_is_not_redundant_instance(self):
def test_string_representation(self):
container = RuntimeContainer("web.1")
assert str(container) == "web.1"


class TestRedisHelper:
def test_without_redis_url_and_judoscale_redis_config(self):
rh = RedisHelper()
with pytest.raises(RuntimeError):
rh.redis_connection(None)

def test_with_redis_url(self):
rh = RedisHelper()
redis = rh.redis_connection({"URL": "redis://localhost:6379/0"})
assert redis.connection_pool.connection_kwargs == {
"host": "localhost",
"port": 6379,
"db": 0,
}

def test_with_rediss_url(self):
rh = RedisHelper()
redis = rh.redis_connection({"URL": "rediss://localhost:6379/0"})
assert redis.connection_pool.connection_kwargs == {
"host": "localhost",
"port": 6379,
"db": 0,
}

def test_with_rediss_config_on_heroku(self):
rh = RedisHelper()
redis = rh.redis_connection(
{"URL": "rediss://localhost:6379/0", "SSL_CERT_REQS": None}
)
assert redis.connection_pool.connection_kwargs == {
"ssl_cert_reqs": None,
"host": "localhost",
"port": 6379,
"db": 0,
}

def test_with_env_var_redis_url(self, monkeypatch):
monkeypatch.setenv("REDIS_URL", "redis://localhost:1234/5")
rh = RedisHelper()
redis = rh.redis_connection(None)
assert redis.connection_pool.connection_kwargs == {
"host": "localhost",
"port": 1234,
"db": 5,
}

0 comments on commit d61b5e5

Please sign in to comment.