From be2d92b272b056745ae2a34f177738a75c34bff7 Mon Sep 17 00:00:00 2001 From: Tim McCormack Date: Mon, 23 Oct 2023 16:48:29 +0000 Subject: [PATCH] feat: Use logging and suppress errors Also, use different names for the two receivers. (Had accidentally called both `..._change`.) --- .../config_watcher/signals/receivers.py | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/edx_arch_experiments/config_watcher/signals/receivers.py b/edx_arch_experiments/config_watcher/signals/receivers.py index ddc4763..033a70b 100644 --- a/edx_arch_experiments/config_watcher/signals/receivers.py +++ b/edx_arch_experiments/config_watcher/signals/receivers.py @@ -4,19 +4,23 @@ Call ``connect_receivers`` to initialize. """ +import logging + import waffle.models from django.db.models import signals from django.dispatch import receiver +log = logging.getLogger(__name__) + def _report_waffle_change(model_short_name, instance, created, fields): verb = "created" if created else "updated" state_desc = ", ".join(f"{field}={repr(getattr(instance, field))}" for field in fields) - print(f"⚡⚡⚡ Waffle {model_short_name} {instance.name!r} was {verb}. New config: {state_desc}") + log.info(f"Waffle {model_short_name} {instance.name!r} was {verb}. New config: {state_desc}") def _report_waffle_delete(model_short_name, instance): - print(f"💥💥💥 Waffle {model_short_name} {instance.name!r} was deleted") + log.info(f"Waffle {model_short_name} {instance.name!r} was deleted") _WAFFLE_MODELS_TO_OBSERVE = [ @@ -48,12 +52,19 @@ def _register_waffle_observation(*, model, short_name, fields): fields (list): Names of fields to report on in the Slack message """ @receiver(signals.post_save, sender=model) - def log_waffle_change(*args, instance, created, **kwargs): - _report_waffle_change(short_name, instance, created, fields) + def report_waffle_change(*args, instance, created, **kwargs): + try: + _report_waffle_change(short_name, instance, created, fields) + except: + # Log and suppress error so Waffle change can proceed + log.exception(f"Failed to report change to waffle {short_name}") @receiver(signals.post_delete, sender=model) - def log_waffle_change(*args, instance, **kwargs): - _report_waffle_delete(short_name, instance) + def report_waffle_delete(*args, instance, **kwargs): + try: + _report_waffle_delete(short_name, instance) + except: + log.exception(f"Failed to report deletion of waffle {short_name}") def connect_receivers():