Skip to content

Commit

Permalink
manager: files: watchdog: individual files watchdog
Browse files Browse the repository at this point in the history
  • Loading branch information
alesmrazek committed Feb 5, 2025
1 parent 6db58b9 commit 2b709f3
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions python/knot_resolver/manager/files/watchdog.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ def tls_cert_files_config(config: KresConfig) -> List[Any]:
]


def files_config(config: KresConfig) -> List[Any]:
return [
config.local_data.rpz,
]


if WATCHDOG_LIB:
from watchdog.events import (
FileSystemEvent,
Expand Down Expand Up @@ -107,6 +113,60 @@ def stop(self) -> None:
self._observer.stop()
self._observer.join()

_files_watchdog: Optional["FilesWatchDog"] = None

class FilesEventHandler(FileSystemEventHandler):
def __init__(self, files: List[Path]) -> None:
self._files = files

def _reload(self) -> None:
pass

def on_created(self, event: FileSystemEvent) -> None:
src_path = Path(str(event.src_path))
if src_path in self._files:
logger.info(f"Watched file '{src_path}' has been created")
self._reload()

def on_deleted(self, event: FileSystemEvent) -> None:
src_path = Path(str(event.src_path))
if src_path in self._files:
logger.warning(f"Watched file '{src_path}' has been deleted")
for file in self._files:
if file.parent == src_path:
logger.warning(f"Watched directory '{src_path}' has been deleted")

def on_modified(self, event: FileSystemEvent) -> None:
src_path = Path(str(event.src_path))
if src_path in self._files:
logger.info(f"Watched file '{src_path}' has been modified")
self._reload()

class FilesWatchDog:
def __init__(self, files: List[Path]) -> None:
self._observer = Observer()

dirs_to_watch: List[Path] = []
for file in files:
if file.parent not in dirs_to_watch:
dirs_to_watch.append(file.parent)

event_handler = FilesEventHandler(files)
for d in dirs_to_watch:
self._observer.schedule(
event_handler,
str(d),
recursive=False,
)
logger.info(f"Directory '{d}' scheduled for watching")

def start(self) -> None:
self._observer.start()

def stop(self) -> None:
self._observer.stop()
self._observer.join()


@only_on_real_changes_update(tls_cert_files_config)
async def _init_tls_cert_watchdog(config: KresConfig) -> None:
Expand All @@ -125,6 +185,28 @@ async def _init_tls_cert_watchdog(config: KresConfig) -> None:
_tls_cert_watchdog.start()


@only_on_real_changes_update(files_config)
async def _init_files_watchdog(config: KresConfig) -> None:
if WATCHDOG_LIB:
global _files_watchdog

if _files_watchdog:
_files_watchdog.stop()

files_to_watch: List[Path] = []
if config.local_data.rpz:
for rpz in config.local_data.rpz:
files_to_watch.append(rpz.file.to_path())

if files_to_watch:
logger.info("Initializing files WatchDog")
_files_watchdog = FilesWatchDog(files_to_watch)
_files_watchdog.start()


async def init_files_watchdog(config_store: ConfigStore) -> None:
# watchdog for TLS certificate files
await config_store.register_on_change_callback(_init_tls_cert_watchdog)

# watchdog for other files
await config_store.register_on_change_callback(_init_files_watchdog)

0 comments on commit 2b709f3

Please sign in to comment.