added callbacks

This commit is contained in:
John Lancaster
2024-05-04 11:43:26 -05:00
parent afc29c9092
commit 462a3afaea

View File

@@ -3,7 +3,7 @@ import logging
from dataclasses import dataclass, field from dataclasses import dataclass, field
from datetime import datetime from datetime import datetime
from pathlib import Path from pathlib import Path
from typing import AsyncIterable, Dict, Iterable, List from typing import AsyncIterable, Awaitable, Callable, Dict, Iterable, List
import anyio import anyio
@@ -15,10 +15,10 @@ class DirectoryMonitor:
dir: Path dir: Path
glob: str = '*' glob: str = '*'
times: Dict[Path | anyio.Path, float] = field(default_factory=dict, repr=False) times: Dict[Path | anyio.Path, float] = field(default_factory=dict, repr=False)
changed: List[Path | anyio.Path] = field(init=False, repr=False)
new: List[Path | anyio.Path] = field(init=False, repr=False)
deleted: List[Path | anyio.Path] = field(init=False, repr=False)
last_checked: datetime = field(default_factory=datetime.now) last_checked: datetime = field(default_factory=datetime.now)
new: List[Path | anyio.Path] = field(init=False, repr=False)
changed: List[Path | anyio.Path] = field(init=False, repr=False)
deleted: List[Path | anyio.Path] = field(init=False, repr=False)
def _reset_counts(self): def _reset_counts(self):
self.last_checked = datetime.now() self.last_checked = datetime.now()
@@ -68,7 +68,7 @@ class DirectoryMonitor:
self._delete_file(file) self._delete_file(file)
async def update_async(self): async def update_async(self):
logger.debug('Async update') # logger.debug('Async update')
self._reset_counts() self._reset_counts()
async for file in self.async_rglob(): async for file in self.async_rglob():
@@ -79,20 +79,6 @@ class DirectoryMonitor:
if not (await file.exists()): if not (await file.exists()):
self._delete_file(file) self._delete_file(file)
async def monitor(self):
while True:
try:
await self.update_async()
except KeyboardInterrupt:
# if a KeyboardInterrupt happens during the update, pass it through
raise
except Exception as e:
# if anything unexpected happens, log it and break the loop cleanly
logger.exception(e)
break
else:
await asyncio.sleep(1.0)
@dataclass @dataclass
class AppConfigMonitor: class AppConfigMonitor:
@@ -107,7 +93,11 @@ class AppConfigMonitor:
self.config_monitor = DirectoryMonitor(self.dir, '*.yaml') self.config_monitor = DirectoryMonitor(self.dir, '*.yaml')
self.module_monitor = DirectoryMonitor(self.dir, '*.py') self.module_monitor = DirectoryMonitor(self.dir, '*.py')
async def monitor(self): async def monitor(
self,
config_update_callback: Callable | Awaitable = None,
module_update_callback: Callable | Awaitable = None,
):
while True: while True:
try: try:
await self.config_monitor.update_async() await self.config_monitor.update_async()
@@ -120,6 +110,16 @@ class AppConfigMonitor:
logger.exception(e) logger.exception(e)
break break
else: else:
if isinstance(config_update_callback, Callable):
config_update_callback(self.config_monitor)
elif isinstance(config_update_callback, Awaitable):
await config_update_callback(self.config_monitor)
if isinstance(module_update_callback, Callable):
module_update_callback(self.module_monitor)
elif isinstance(module_update_callback, Awaitable):
await module_update_callback(self.module_monitor)
await asyncio.sleep(self.poll_sleep_time) await asyncio.sleep(self.poll_sleep_time)