From 5bad3e317c16651bad256ad57d2da808810ced96 Mon Sep 17 00:00:00 2001 From: Bruno Rocha Date: Thu, 21 Sep 2023 16:35:12 +0100 Subject: [PATCH] Log will emit only once when Redis is not available No-Issue --- .../app/management/commands/galaxy-settings.py | 2 +- .../{0042_setting.py => 0045_setting.py} | 2 +- galaxy_ng/app/tasks/settings_cache.py | 16 +++++++--------- 3 files changed, 9 insertions(+), 11 deletions(-) rename galaxy_ng/app/migrations/{0042_setting.py => 0045_setting.py} (96%) diff --git a/galaxy_ng/app/management/commands/galaxy-settings.py b/galaxy_ng/app/management/commands/galaxy-settings.py index 2485823723..c6a6384d40 100644 --- a/galaxy_ng/app/management/commands/galaxy-settings.py +++ b/galaxy_ng/app/management/commands/galaxy-settings.py @@ -71,7 +71,7 @@ def add_arguments(self, parser): subparsers.add_parser('allowed_keys', help='List allowed settings keys') def echo(self, message): - self.stdout.write(self.style.SUCCESS(message)) + self.stdout.write(self.style.SUCCESS(str(message))) def handle(self, *args, **options): subcommand = options['subcommand'] diff --git a/galaxy_ng/app/migrations/0042_setting.py b/galaxy_ng/app/migrations/0045_setting.py similarity index 96% rename from galaxy_ng/app/migrations/0042_setting.py rename to galaxy_ng/app/migrations/0045_setting.py index acc79859c5..1eb1b4ba4b 100644 --- a/galaxy_ng/app/migrations/0042_setting.py +++ b/galaxy_ng/app/migrations/0045_setting.py @@ -9,7 +9,7 @@ class Migration(migrations.Migration): dependencies = [ - ("galaxy", "0041_alter_containerregistryremote_remote_ptr"), + ("galaxy", "0044_legacyroleimport"), ] operations = [ diff --git a/galaxy_ng/app/tasks/settings_cache.py b/galaxy_ng/app/tasks/settings_cache.py index 16571edfda..9796f1bbab 100644 --- a/galaxy_ng/app/tasks/settings_cache.py +++ b/galaxy_ng/app/tasks/settings_cache.py @@ -12,7 +12,9 @@ from galaxy_ng.app.models.config import Setting from django.db.utils import OperationalError +logger = logging.getLogger(__name__) _conn = None +CACHE_KEY = "GALAXY_SETTINGS_DATA" def get_redis_connection(): @@ -32,13 +34,14 @@ def get_redis_connection(): ssl_ca_certs=settings.get("REDIS_SSL_CA_CERTS"), decode_responses=True, ) + else: + logger.warning( + "REDIS connection undefined, not caching dynamic settings" + ) return _conn conn: Optional[Redis] = get_redis_connection() -logger = logging.getLogger(__name__) -logger.setLevel(logging.DEBUG) -CACHE_KEY = "GALAXY_SETTINGS_DATA" def connection_error_wrapper( @@ -48,8 +51,7 @@ def connection_error_wrapper( def dispatch(func, *args, **kwargs): """Handle connection errors, specific to the sync context, raised by the Redis client.""" - if conn is None: - logger.warning(f"{func.__name__} skipped, no Redis connection available") + if conn is None: # No redis connection defined return default() try: return func(*args, **kwargs) @@ -84,7 +86,6 @@ def acquire_lock(lock_name: str, lock_timeout: int = 20): """Acquire a lock using Redis connection""" LOCK_KEY = f"GALAXY_SETTINGS_LOCK_{lock_name}" if conn is None: - logger.warning("Lock acquisition skipped, no Redis connection available") return "no-lock" # no Redis connection, assume lock is acquired token = str(uuid4()) @@ -97,7 +98,6 @@ def release_lock(lock_name: str, token: str): """Release a lock using Redis connection""" LOCK_KEY = f"GALAXY_SETTINGS_LOCK_{lock_name}" if conn is None: - logger.warning("Lock release skipped, no Redis connection available") return lock = conn.get(LOCK_KEY) if lock == token: @@ -109,7 +109,6 @@ def update_setting_cache(data: Dict[str, Any]) -> int: """Takes a python dictionary and write to Redis as a hashmap using Redis connection""" if conn is None: - logger.warning("Settings cache update skipped, no Redis connection available") return 0 conn.delete(CACHE_KEY) @@ -124,7 +123,6 @@ def update_setting_cache(data: Dict[str, Any]) -> int: def get_settings_from_cache() -> Dict[str, Any]: """Reads settings from Redis cache and returns a python dictionary""" if conn is None: - logger.warning("Settings cache read skipped, no Redis connection available") return {} return conn.hgetall(CACHE_KEY)