Skip to content

Commit

Permalink
fix(svc): properly use Redis sentinel (#3319)
Browse files Browse the repository at this point in the history
  • Loading branch information
Panaetius committed Feb 16, 2023
1 parent 8844dff commit 4a52069
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
7 changes: 3 additions & 4 deletions renku/ui/cli/service.py
Expand Up @@ -176,7 +176,7 @@ def read_logs(log_file, follow=True, output_all=False):
@click.pass_context
def service(ctx, env):
"""Manage service components."""
import redis # noqa: F401
import redis
import rq # noqa: F401
from dotenv import load_dotenv

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

ctx.exit(1)

except redis.exceptions.ConnectionError:
except redis.exceptions.ConnectionError as e:
# NOTE: Cannot connect to the service dependencies, ie. Redis.

click.echo(ERROR + "Cannot connect to Redis")
click.echo(ERROR + f"Cannot connect to Redis: {e}")

ctx.exit(1)

Expand Down
39 changes: 27 additions & 12 deletions renku/ui/service/cache/base.py
Expand Up @@ -18,28 +18,43 @@
"""Renku service cache management."""
import json
import os
from typing import Any, Dict

import redis
from redis import RedisError
from walrus import Database

from renku.ui.service.cache.config import REDIS_DATABASE, REDIS_HOST, REDIS_NAMESPACE, REDIS_PASSWORD, REDIS_PORT
from renku.ui.service.cache.config import (
REDIS_DATABASE,
REDIS_HOST,
REDIS_IS_SENTINEL,
REDIS_MASTER_SET,
REDIS_NAMESPACE,
REDIS_PASSWORD,
REDIS_PORT,
)

_config: Dict[str, Any] = {
"db": REDIS_DATABASE,
"password": REDIS_PASSWORD,
"retry_on_timeout": True,
"health_check_interval": int(os.getenv("CACHE_HEALTH_CHECK_INTERVAL", 60)),
}

if REDIS_IS_SENTINEL:
_sentinel = redis.Sentinel([(REDIS_HOST, REDIS_PORT)], sentinel_kwargs={"password": REDIS_PASSWORD})
_cache = _sentinel.master_for(REDIS_MASTER_SET, **_config)
_model_db = Database(connection_pool=_cache.connection_pool, **_config)
else:
_cache = redis.Redis(host=REDIS_HOST, port=REDIS_PORT, **_config) # type: ignore
_model_db = Database(host=REDIS_HOST, port=REDIS_PORT, **_config)


class BaseCache:
"""Cache management."""

config_ = {
"host": REDIS_HOST,
"port": REDIS_PORT,
"db": REDIS_DATABASE,
"password": REDIS_PASSWORD,
"retry_on_timeout": True,
"health_check_interval": int(os.getenv("CACHE_HEALTH_CHECK_INTERVAL", 60)),
}

cache = redis.Redis(**config_) # type: ignore
model_db = Database(**config_)
cache: redis.Redis = _cache
model_db: Database = _model_db
namespace = REDIS_NAMESPACE

def set_record(self, name, key, value):
Expand Down
8 changes: 0 additions & 8 deletions renku/ui/service/cache/config.py
Expand Up @@ -27,11 +27,3 @@

REDIS_IS_SENTINEL = os.environ.get("REDIS_IS_SENTINEL", "") == "true"
REDIS_MASTER_SET = os.environ.get("REDIS_MASTER_SET", "mymaster")
if REDIS_IS_SENTINEL:
from redis.sentinel import Sentinel

sentinel = Sentinel(
[(REDIS_HOST, REDIS_PORT)],
sentinel_kwargs={"password": REDIS_PASSWORD},
)
REDIS_HOST, REDIS_PORT = sentinel.discover_master(REDIS_MASTER_SET)

0 comments on commit 4a52069

Please sign in to comment.