Hot reloader for config structure

This commit is contained in:
Maximilian Giller 2022-10-28 09:30:40 +02:00
parent c2f4765c09
commit f95f8c7d19

41
src/config/hotreloader.py Normal file
View file

@ -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.")