Skip to content

Commit 4a52069

Browse files
authored
fix(svc): properly use Redis sentinel (#3319)
1 parent 8844dff commit 4a52069

File tree

3 files changed

+30
-24
lines changed

3 files changed

+30
-24
lines changed

renku/ui/cli/service.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def read_logs(log_file, follow=True, output_all=False):
176176
@click.pass_context
177177
def service(ctx, env):
178178
"""Manage service components."""
179-
import redis # noqa: F401
179+
import redis
180180
import rq # noqa: F401
181181
from dotenv import load_dotenv
182182

@@ -196,10 +196,9 @@ def service(ctx, env):
196196

197197
ctx.exit(1)
198198

199-
except redis.exceptions.ConnectionError:
199+
except redis.exceptions.ConnectionError as e:
200200
# NOTE: Cannot connect to the service dependencies, ie. Redis.
201-
202-
click.echo(ERROR + "Cannot connect to Redis")
201+
click.echo(ERROR + f"Cannot connect to Redis: {e}")
203202

204203
ctx.exit(1)
205204

renku/ui/service/cache/base.py

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,43 @@
1818
"""Renku service cache management."""
1919
import json
2020
import os
21+
from typing import Any, Dict
2122

2223
import redis
2324
from redis import RedisError
2425
from walrus import Database
2526

26-
from renku.ui.service.cache.config import REDIS_DATABASE, REDIS_HOST, REDIS_NAMESPACE, REDIS_PASSWORD, REDIS_PORT
27+
from renku.ui.service.cache.config import (
28+
REDIS_DATABASE,
29+
REDIS_HOST,
30+
REDIS_IS_SENTINEL,
31+
REDIS_MASTER_SET,
32+
REDIS_NAMESPACE,
33+
REDIS_PASSWORD,
34+
REDIS_PORT,
35+
)
36+
37+
_config: Dict[str, Any] = {
38+
"db": REDIS_DATABASE,
39+
"password": REDIS_PASSWORD,
40+
"retry_on_timeout": True,
41+
"health_check_interval": int(os.getenv("CACHE_HEALTH_CHECK_INTERVAL", 60)),
42+
}
43+
44+
if REDIS_IS_SENTINEL:
45+
_sentinel = redis.Sentinel([(REDIS_HOST, REDIS_PORT)], sentinel_kwargs={"password": REDIS_PASSWORD})
46+
_cache = _sentinel.master_for(REDIS_MASTER_SET, **_config)
47+
_model_db = Database(connection_pool=_cache.connection_pool, **_config)
48+
else:
49+
_cache = redis.Redis(host=REDIS_HOST, port=REDIS_PORT, **_config) # type: ignore
50+
_model_db = Database(host=REDIS_HOST, port=REDIS_PORT, **_config)
2751

2852

2953
class BaseCache:
3054
"""Cache management."""
3155

32-
config_ = {
33-
"host": REDIS_HOST,
34-
"port": REDIS_PORT,
35-
"db": REDIS_DATABASE,
36-
"password": REDIS_PASSWORD,
37-
"retry_on_timeout": True,
38-
"health_check_interval": int(os.getenv("CACHE_HEALTH_CHECK_INTERVAL", 60)),
39-
}
40-
41-
cache = redis.Redis(**config_) # type: ignore
42-
model_db = Database(**config_)
56+
cache: redis.Redis = _cache
57+
model_db: Database = _model_db
4358
namespace = REDIS_NAMESPACE
4459

4560
def set_record(self, name, key, value):

renku/ui/service/cache/config.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,3 @@
2727

2828
REDIS_IS_SENTINEL = os.environ.get("REDIS_IS_SENTINEL", "") == "true"
2929
REDIS_MASTER_SET = os.environ.get("REDIS_MASTER_SET", "mymaster")
30-
if REDIS_IS_SENTINEL:
31-
from redis.sentinel import Sentinel
32-
33-
sentinel = Sentinel(
34-
[(REDIS_HOST, REDIS_PORT)],
35-
sentinel_kwargs={"password": REDIS_PASSWORD},
36-
)
37-
REDIS_HOST, REDIS_PORT = sentinel.discover_master(REDIS_MASTER_SET)

0 commit comments

Comments
 (0)