Hot reloader for config structure
This commit is contained in:
parent
c2f4765c09
commit
f95f8c7d19
1 changed files with 41 additions and 0 deletions
41
src/config/hotreloader.py
Normal file
41
src/config/hotreloader.py
Normal 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.")
|
Loading…
Reference in a new issue