From c62843b19b88ad4feb988450fdf1905d4c6005e5 Mon Sep 17 00:00:00 2001 From: Marc 'risson' Schmitt Date: Thu, 7 Dec 2023 08:31:25 +0100 Subject: [PATCH] blueprints: only watch for fs events we're interested in Signed-off-by: Marc 'risson' Schmitt --- authentik/blueprints/v1/tasks.py | 43 +++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/authentik/blueprints/v1/tasks.py b/authentik/blueprints/v1/tasks.py index 8ff86c996..766124ef2 100644 --- a/authentik/blueprints/v1/tasks.py +++ b/authentik/blueprints/v1/tasks.py @@ -62,7 +62,12 @@ def start_blueprint_watcher(): if _file_watcher_started: return observer = Observer() - observer.schedule(BlueprintEventHandler(), CONFIG.get("blueprints_dir"), recursive=True) + observer.schedule( + BlueprintEventHandler(), + CONFIG.get("blueprints_dir"), + recursive=True, + event_filter=(FileCreatedEvent, FileModifiedEvent), + ) observer.start() _file_watcher_started = True @@ -70,21 +75,29 @@ def start_blueprint_watcher(): class BlueprintEventHandler(FileSystemEventHandler): """Event handler for blueprint events""" - def on_any_event(self, event: FileSystemEvent): - if not isinstance(event, (FileCreatedEvent, FileModifiedEvent)): - return + # We only ever get creation and modification events. + # See the creation of the Observer instance above for the event filtering. + + # Even though we filter to only get file events, we might still get + # directory events as some implementations such as inotify do not support + # filtering on file/directory. + + def dispatch(self, event: FileSystemEvent) -> None: if event.is_directory: - return - if isinstance(event, FileCreatedEvent): - LOGGER.debug("new blueprint file created, starting discovery") - blueprints_discovery.delay() - if isinstance(event, FileModifiedEvent): - path = Path(event.src_path) - root = Path(CONFIG.get("blueprints_dir")).absolute() - rel_path = str(path.relative_to(root)) - for instance in BlueprintInstance.objects.filter(path=rel_path, enabled=True): - LOGGER.debug("modified blueprint file, starting apply", instance=instance) - apply_blueprint.delay(instance.pk.hex) + return None + return super().dispatch(event) + + def on_created(self, event: FileSystemEvent): + LOGGER.debug("new blueprint file created, starting discovery") + blueprints_discovery.delay() + + def on_modified(self, event: FileSystemEvent): + path = Path(event.src_path) + root = Path(CONFIG.get("blueprints_dir")).absolute() + rel_path = str(path.relative_to(root)) + for instance in BlueprintInstance.objects.filter(path=rel_path, enabled=True): + LOGGER.debug("modified blueprint file, starting apply", instance=instance) + apply_blueprint.delay(instance.pk.hex) @CELERY_APP.task(