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

@@ -15,7 +15,7 @@ logger = logging.getLogger(__name__)
class AppDaemonRunContext:
_stack: ExitStack = field(default_factory=ExitStack)
stop_event: Event = field(default_factory=Event)
shutdown_grace_period: float = 0.75
shutdown_grace_period: float = 0.1
loop: asyncio.AbstractEventLoop = field(init=False)
executor: ThreadPoolExecutor = field(init=False)
@@ -37,10 +37,10 @@ class AppDaemonRunContext:
logger.debug(f'Closing context from {self.__class__.__name__}')
self._stack.close()
def get_running_tasks(self, exclude_current: bool = True) -> list[asyncio.Task]:
def get_running_tasks(self) -> list[asyncio.Task]:
return [
t for t in asyncio.all_tasks(self.loop)
if exclude_current and t is not asyncio.current_task()
if t is not asyncio.current_task()
]
async def shutdown(self, signal=signal.SIGTERM):
@@ -59,15 +59,17 @@ class AppDaemonRunContext:
asyncio.wait_for(t, timeout=self.shutdown_grace_period)
for t in tasks
)
logger.debug(f'Allowing graceful shutdown from stop event for {
self.shutdown_grace_period}s')
logger.debug(
'Allowing graceful shutdown from stop event for %ss',
self.shutdown_grace_period
)
await asyncio.gather(*graceful, return_exceptions=True)
for task in tasks:
if task.cancelled():
logger.warning(f'Cancelled {task.get_name()}')
logger.info("Stopping asyncio event loop")
logger.info("Calling stop() for asyncio event loop")
self.loop.stop()
else:
logger.warning('Already started shutdown')