added callbacks
This commit is contained in:
@@ -3,7 +3,7 @@ import logging
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import AsyncIterable, Dict, Iterable, List
|
||||
from typing import AsyncIterable, Awaitable, Callable, Dict, Iterable, List
|
||||
|
||||
import anyio
|
||||
|
||||
@@ -15,10 +15,10 @@ class DirectoryMonitor:
|
||||
dir: Path
|
||||
glob: str = '*'
|
||||
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)
|
||||
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):
|
||||
self.last_checked = datetime.now()
|
||||
@@ -68,7 +68,7 @@ class DirectoryMonitor:
|
||||
self._delete_file(file)
|
||||
|
||||
async def update_async(self):
|
||||
logger.debug('Async update')
|
||||
# logger.debug('Async update')
|
||||
self._reset_counts()
|
||||
|
||||
async for file in self.async_rglob():
|
||||
@@ -79,20 +79,6 @@ class DirectoryMonitor:
|
||||
if not (await file.exists()):
|
||||
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
|
||||
class AppConfigMonitor:
|
||||
@@ -107,7 +93,11 @@ class AppConfigMonitor:
|
||||
self.config_monitor = DirectoryMonitor(self.dir, '*.yaml')
|
||||
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:
|
||||
try:
|
||||
await self.config_monitor.update_async()
|
||||
@@ -120,6 +110,16 @@ class AppConfigMonitor:
|
||||
logger.exception(e)
|
||||
break
|
||||
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)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user