Skip to content

Commit

Permalink
feat: Use logging and suppress errors
Browse files Browse the repository at this point in the history
Also, use different names for the two receivers. (Had accidentally called
both `..._change`.)
  • Loading branch information
timmc-edx committed Oct 23, 2023
1 parent fe6e73e commit be2d92b
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions edx_arch_experiments/config_watcher/signals/receivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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():
Expand Down

0 comments on commit be2d92b

Please sign in to comment.