blueprints: only watch for fs events we're interested in

Signed-off-by: Marc 'risson' Schmitt <marc.schmitt@risson.space>
This commit is contained in:
Marc 'risson' Schmitt 2023-12-07 08:31:25 +01:00
parent 875a9dbc26
commit c62843b19b
No known key found for this signature in database
GPG Key ID: 9C3FA22FABF1AA8D
1 changed files with 28 additions and 15 deletions

View File

@ -62,7 +62,12 @@ def start_blueprint_watcher():
if _file_watcher_started: if _file_watcher_started:
return return
observer = Observer() 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() observer.start()
_file_watcher_started = True _file_watcher_started = True
@ -70,21 +75,29 @@ def start_blueprint_watcher():
class BlueprintEventHandler(FileSystemEventHandler): class BlueprintEventHandler(FileSystemEventHandler):
"""Event handler for blueprint events""" """Event handler for blueprint events"""
def on_any_event(self, event: FileSystemEvent): # We only ever get creation and modification events.
if not isinstance(event, (FileCreatedEvent, FileModifiedEvent)): # See the creation of the Observer instance above for the event filtering.
return
# 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: if event.is_directory:
return return None
if isinstance(event, FileCreatedEvent): return super().dispatch(event)
LOGGER.debug("new blueprint file created, starting discovery")
blueprints_discovery.delay() def on_created(self, event: FileSystemEvent):
if isinstance(event, FileModifiedEvent): LOGGER.debug("new blueprint file created, starting discovery")
path = Path(event.src_path) blueprints_discovery.delay()
root = Path(CONFIG.get("blueprints_dir")).absolute()
rel_path = str(path.relative_to(root)) def on_modified(self, event: FileSystemEvent):
for instance in BlueprintInstance.objects.filter(path=rel_path, enabled=True): path = Path(event.src_path)
LOGGER.debug("modified blueprint file, starting apply", instance=instance) root = Path(CONFIG.get("blueprints_dir")).absolute()
apply_blueprint.delay(instance.pk.hex) 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( @CELERY_APP.task(