From f95f8c7d1959bddda38db68f41446aaad5e759ec Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Fri, 28 Oct 2022 09:30:40 +0200 Subject: [PATCH] Hot reloader for config structure --- src/config/hotreloader.py | 41 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/config/hotreloader.py diff --git a/src/config/hotreloader.py b/src/config/hotreloader.py new file mode 100644 index 0000000..3971239 --- /dev/null +++ b/src/config/hotreloader.py @@ -0,0 +1,41 @@ +import logging + +DEPENDENCY_INSTALLED = None + +try: + from watchdog.observers import Observer + from watchdog.events import ( + FileSystemEventHandler, + FileCreatedEvent, + FileModifiedEvent, + ) + + DEPENDENCY_INSTALLED = True + logging.debug("Watchdog imported successfully. Hot reloading available.") +except ImportError: + DEPENDENCY_INSTALLED = False + logging.info("Watchdog is not installed. Hot reloading unavailable.") + + +class HotReloader(FileSystemEventHandler): + def __init__(self, path): + if not DEPENDENCY_INSTALLED: + return + + self.path = path + self.observer = Observer() + self.observer.schedule(self, path, recursive=True) + self.observer.start() + + def __del__(self): + if not DEPENDENCY_INSTALLED: + return + + self.observer.stop() + self.observer.join() + + def on_modified(self, event: FileModifiedEvent): + logging.info("Config file modified. Triggering hot reload.") + + def on_created(self, event: FileCreatedEvent): + logging.info("New config file created. Triggering hot reload.")