This commit is contained in:
John Lancaster
2024-10-21 03:15:59 +00:00
parent bc2224a919
commit de496509a9
3 changed files with 26 additions and 14 deletions

View File

@@ -34,40 +34,46 @@ class ADSubsystem:
self.logger.debug(f'Starting {self.__class__.__name__}')
def __exit__(self, exc_type, exc_value, traceback):
self.logger.debug(f'Exiting {self.__class__.__name__}')
pass
# self.logger.debug(f'Exiting {self.__class__.__name__}')
@property
def stopping(self) -> bool:
return self.stop.is_set()
async def sleep(self, delay: float):
"""Wrapper function for asyncio.sleep that suppresses and logs a task cancellation"""
try:
if not self.stopping:
await asyncio.sleep(delay)
else:
self.logger.debug('Skipping sleep due to stop event')
except asyncio.CancelledError:
self.logger.debug('Cancelled during sleep')
@dataclass
class Utility(ADSubsystem):
loop_rate: float = 0.25
loop_rate: float = 1.0
def __enter__(self):
super().__enter__()
self.create_task(self.loop(), 'Utility loop', critical=True)
self.create_task(self.loop(), 'utility loop', critical=True)
return self
async def loop(self):
while not self.stopping:
self.logger.debug('Looping...')
await self.sleep(self.loop_rate)
self.logger.debug('Stopped utility loop gracefully')
task_name = asyncio.current_task().get_name()
self.logger.debug(f'Gracefully stopped {task_name} task')
@dataclass
class Plugin(ADSubsystem):
state: dict[str, int] = field(default_factory=dict)
update_rate: float = 2.0
update_rate: float = 5.0
def __post_init__(self) -> None:
super().__post_init__()
@@ -100,7 +106,9 @@ class Plugin(ADSubsystem):
# raise ValueError('fake error')
await self.sleep(self.update_rate)
self.logger.debug('Stopped plugin updates gracefully')
task_name = asyncio.current_task().get_name()
self.logger.debug(f'Gracefully stopped {task_name} task')
@dataclass