Skip to content

Commit

Permalink
Validator must get by key not base_key
Browse files Browse the repository at this point in the history
No-Issue
  • Loading branch information
rochacbruno committed Sep 20, 2023
1 parent 1e6b3ff commit 47cc64b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 29 deletions.
9 changes: 5 additions & 4 deletions galaxy_ng/app/dynaconf_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,14 +595,15 @@ def configure_dynamic_settings(settings: Dynaconf) -> Dict[str, Any]:
# Perform lazy imports here to avoid breaking when system runs with older
# dynaconf versions
try:
from dynaconf.hooking import Hook, Action, HookValue
from dynaconf import DynaconfFormatError, DynaconfParseError
from dynaconf.loaders.base import SourceMetadata
from dynaconf.hooking import Hook, Action, HookValue
from dynaconf.base import Settings
except ImportError as exc:
# Graceful degradation for dynaconf < 3.2.2 where method hooking is not available
# Graceful degradation for dynaconf < 3.2.3 where method hooking is not available
logger.error(
"Dynaconf method hooking requires Dynaconf >=3.2.2: %s",
"Galaxy Dynamic Settings requires Dynaconf >=3.2.3, "
"system will work normally but dynamic settings from database will be ignored: %s",
str(exc)
)
return {}
Expand Down Expand Up @@ -633,7 +634,7 @@ def read_settings_from_cache_or_db(
if data:
metadata = SourceMetadata(loader="hooking", identifier="db")

# This is the main part, it will update temp_settigns with data coming from settings db
# This is the main part, it will update temp_settings with data coming from settings db
# and by calling update it will process dynaconf parsing and merging.
try:
if data:
Expand Down
30 changes: 15 additions & 15 deletions galaxy_ng/app/management/commands/galaxy-settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ class Command(BaseCommand):
django-admin galaxy-settings set --key=foo --value=bar --is-secret
django-admin galaxy-settings get --key=foo
django-admin galaxy-settings get --key=foo --parsed
django-admin galaxy-settings get --key=foo --raw
django-admin galaxy-settings delete --key=foo --all-versions
django-admin galaxy-settings delete --all
django-admin galaxy-settings list
django-admin galaxy-settings list --parsed
django-admin galaxy-settings list --raw
django-admin galaxy-settings inspect --key=foo
Expand All @@ -42,7 +42,7 @@ def add_arguments(self, parser):
# Subcommand: get
get_parser = subparsers.add_parser('get', help='Get a Galaxy setting')
get_parser.add_argument('--key', required=True, help='Setting key')
get_parser.add_argument('--parsed', action='store_true', help='Parse value using Dynaconf')
get_parser.add_argument('--raw', action='store_true', help='Raw value from DB')
get_parser.add_argument('--default', help='Default value')

# Subcommand: delete
Expand All @@ -55,7 +55,7 @@ def add_arguments(self, parser):

# Subcommand: list
list_parser = subparsers.add_parser('list', help='List Galaxy settings')
list_parser.add_argument('--parsed', action='store_true', help='Parse values with Dynaconf')
list_parser.add_argument('--raw', action='store_true', help='Raw value from DB')

# Subcommand: inspect
inspect_parser = subparsers.add_parser('inspect', help='Inspect a Galaxy setting')
Expand Down Expand Up @@ -86,14 +86,14 @@ def handle_set(self, *args, **options):

def handle_get(self, *args, **options):
key = options['key']
parsed = options['parsed']
raw = options['raw']
default = options['default']
if parsed:
return Setting.get(upperfy(key), default=default)
try:
return Setting.get_value_from_db(upperfy(key))
except Setting.DoesNotExist:
return default
if raw:
try:
return Setting.get_value_from_db(upperfy(key))
except Setting.DoesNotExist:
return default
return Setting.get(upperfy(key), default=default)

def handle_delete(self, *args, **options):
key = options['key']
Expand All @@ -113,11 +113,11 @@ def handle_delete(self, *args, **options):
return "Nothing to delete"

def handle_list(self, *args, **options):
parsed = options['parsed']
raw = options['raw']
data = Setting.as_dict()
if parsed:
return {k: Setting.get(k) for k in data}
return data
if raw:
return data
return {k: Setting.get(k) for k in data}

def handle_inspect(self, *args, **options):
key = options['key']
Expand Down
2 changes: 1 addition & 1 deletion galaxy_ng/app/models/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def _hook_validate(self):
temp_settings = settings.dynaconf_clone()
temp_settings.validators.register(validator)
temp_settings.update(current_db_data, tomlfy=True)
temp_settings.set(self.base_key, self.value, tomlfy=True)
temp_settings.set(self.key, self.value, tomlfy=True)
temp_settings.validators.validate()

@classmethod
Expand Down
20 changes: 11 additions & 9 deletions galaxy_ng/app/tasks/settings_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@

def get_redis_connection():
global _conn
redis_host = settings.get("REDIS_HOST")
redis_url = settings.get("REDIS_URL")
if _conn is None:
if redis_url := settings.get("REDIS_URL"):
if redis_url is not None:
_conn = Redis.from_url(redis_url, decode_responses=True)
else:
elif redis_host is not None:
_conn = Redis(
host=settings.get("REDIS_HOST", "localhost"),
port=settings.get("REDIS_PORT", 6379),
host=redis_host,
port=settings.get("REDIS_PORT") or 6379,
db=settings.get("REDIS_DB", 0),
password=settings.get("REDIS_PASSWORD"),
ssl=settings.get("REDIS_SSL", False),
Expand All @@ -47,7 +49,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.debug(f"{func.__name__} skipped, no Redis connection available")
logger.warning(f"{func.__name__} skipped, no Redis connection available")
return default()
try:
return func(*args, **kwargs)
Expand Down Expand Up @@ -82,7 +84,7 @@ 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.debug("Lock acquisition skipped, no Redis connection available")
logger.warning("Lock acquisition skipped, no Redis connection available")
return "no-lock" # no Redis connection, assume lock is acquired

token = str(uuid4())
Expand All @@ -95,7 +97,7 @@ 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.debug("Lock release skipped, no Redis connection available")
logger.warning("Lock release skipped, no Redis connection available")
return
lock = conn.get(LOCK_KEY)
if lock == token:
Expand All @@ -107,7 +109,7 @@ 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.debug("Settings cache update skipped, no Redis connection available")
logger.warning("Settings cache update skipped, no Redis connection available")
return 0

conn.delete(CACHE_KEY)
Expand All @@ -122,7 +124,7 @@ 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.debug("Settings cache read skipped, no Redis connection available")
logger.warning("Settings cache read skipped, no Redis connection available")
return {}

return conn.hgetall(CACHE_KEY)
Expand Down

0 comments on commit 47cc64b

Please sign in to comment.