flows: fix flow cache not being cleared correctly when stages are saved

This commit is contained in:
Jens Langhammer 2020-06-21 12:40:01 +02:00
parent 246d00bdde
commit 6122dcacc7
4 changed files with 39 additions and 1 deletions

View File

@ -1,4 +1,6 @@
"""passbook flows app config"""
from importlib import import_module
from django.apps import AppConfig
@ -9,3 +11,7 @@ class PassbookFlowsConfig(AppConfig):
label = "passbook_flows"
mountpoint = "flows/"
verbose_name = "passbook Flows"
def ready(self):
"""Load policy cache clearing signals"""
import_module("passbook.flows.signals")

31
passbook/flows/signals.py Normal file
View File

@ -0,0 +1,31 @@
"""passbook flow signals"""
from django.core.cache import cache
from django.db.models.signals import post_save
from django.dispatch import receiver
from structlog import get_logger
LOGGER = get_logger()
@receiver(post_save)
# pylint: disable=unused-argument
def invalidate_flow_cache(sender, instance, **_):
"""Invalidate flow cache when flow is updated"""
from passbook.flows.models import Flow, FlowStageBinding, Stage
from passbook.flows.planner import cache_key
if isinstance(instance, Flow):
LOGGER.debug("Invalidating Flow cache", flow=instance)
cache.delete(f"{cache_key(instance)}*")
if isinstance(instance, FlowStageBinding):
LOGGER.debug("Invalidating Flow cache from FlowStageBinding", binding=instance)
cache.delete(f"{cache_key(instance.flow)}*")
if isinstance(instance, Stage):
LOGGER.debug("Invalidating Flow cache from Stage", stage=instance)
total = 0
for binding in FlowStageBinding.objects.filter(stage=instance):
prefix = cache_key(binding.flow)
keys = cache.keys(f"{prefix}*")
total += len(keys)
cache.delete_many(keys)
LOGGER.debug("Deleted keys", len=total)

View File

@ -12,5 +12,5 @@ class PassbookPoliciesConfig(AppConfig):
verbose_name = "passbook Policies"
def ready(self):
"""Load source_types from config file"""
"""Load policy cache clearing signals"""
import_module("passbook.policies.signals")

View File

@ -347,6 +347,7 @@ _LOGGING_HANDLER_MAP = {
"passbook": LOG_LEVEL,
"django": "WARNING",
"celery": "WARNING",
"selenium": "WARNING",
"grpc": LOG_LEVEL,
"oauthlib": LOG_LEVEL,
"oauth2_provider": LOG_LEVEL,