authentik.flows.signals
authentik flow signals
1"""authentik flow signals""" 2 3from django.core.cache import cache 4from django.db.models.signals import post_save, pre_delete 5from django.dispatch import receiver 6from structlog.stdlib import get_logger 7 8from authentik.flows.apps import GAUGE_FLOWS_CACHED 9from authentik.flows.planner import CACHE_PREFIX 10from authentik.root.monitoring import monitoring_set 11 12LOGGER = get_logger() 13 14 15def delete_cache_prefix(prefix: str) -> int: 16 """Delete keys prefixed with `prefix` and return count of deleted keys.""" 17 keys = cache.keys(prefix) 18 cache.delete_many(keys) 19 return len(keys) 20 21 22@receiver(monitoring_set) 23def monitoring_set_flows(sender, **kwargs): 24 """set flow gauges""" 25 GAUGE_FLOWS_CACHED.set(cache.keys_count(f"{CACHE_PREFIX}*")) 26 27 28@receiver(post_save) 29@receiver(pre_delete) 30def invalidate_flow_cache(sender, instance, **_): 31 """Invalidate flow cache when flow is updated""" 32 from authentik.flows.models import Flow, FlowStageBinding, Stage 33 from authentik.flows.planner import cache_key 34 35 if isinstance(instance, Flow): 36 total = delete_cache_prefix(f"{cache_key(instance)}*") 37 LOGGER.debug("Invalidating Flow cache", flow=instance, len=total) 38 if isinstance(instance, FlowStageBinding) and instance.target_id: 39 total = delete_cache_prefix(f"{cache_key(instance.target)}*") 40 LOGGER.debug("Invalidating Flow cache from FlowStageBinding", binding=instance, len=total) 41 if isinstance(instance, Stage): 42 total = 0 43 for binding in FlowStageBinding.objects.filter(stage=instance): 44 prefix = cache_key(binding.target) 45 total += delete_cache_prefix(f"{prefix}*") 46 LOGGER.debug("Invalidating Flow cache from Stage", stage=instance, len=total)
LOGGER =
<BoundLoggerLazyProxy(logger=None, wrapper_class=None, processors=None, context_class=None, initial_values={}, logger_factory_args=())>
def
delete_cache_prefix(prefix: str) -> int:
16def delete_cache_prefix(prefix: str) -> int: 17 """Delete keys prefixed with `prefix` and return count of deleted keys.""" 18 keys = cache.keys(prefix) 19 cache.delete_many(keys) 20 return len(keys)
Delete keys prefixed with prefix and return count of deleted keys.
@receiver(monitoring_set)
def
monitoring_set_flows(sender, **kwargs):
23@receiver(monitoring_set) 24def monitoring_set_flows(sender, **kwargs): 25 """set flow gauges""" 26 GAUGE_FLOWS_CACHED.set(cache.keys_count(f"{CACHE_PREFIX}*"))
set flow gauges
@receiver(post_save)
@receiver(pre_delete)
def
invalidate_flow_cache(sender, instance, **_):
29@receiver(post_save) 30@receiver(pre_delete) 31def invalidate_flow_cache(sender, instance, **_): 32 """Invalidate flow cache when flow is updated""" 33 from authentik.flows.models import Flow, FlowStageBinding, Stage 34 from authentik.flows.planner import cache_key 35 36 if isinstance(instance, Flow): 37 total = delete_cache_prefix(f"{cache_key(instance)}*") 38 LOGGER.debug("Invalidating Flow cache", flow=instance, len=total) 39 if isinstance(instance, FlowStageBinding) and instance.target_id: 40 total = delete_cache_prefix(f"{cache_key(instance.target)}*") 41 LOGGER.debug("Invalidating Flow cache from FlowStageBinding", binding=instance, len=total) 42 if isinstance(instance, Stage): 43 total = 0 44 for binding in FlowStageBinding.objects.filter(stage=instance): 45 prefix = cache_key(binding.target) 46 total += delete_cache_prefix(f"{prefix}*") 47 LOGGER.debug("Invalidating Flow cache from Stage", stage=instance, len=total)
Invalidate flow cache when flow is updated