async def shutdown(loop: AbstractEventLoop): tasks = [ task for task in asyncio.all_tasks(loop) if task is not asyncio.tasks.current_task(loop) ] list(map(lambda task: task.cancel(), tasks)) await asyncio.gather(*tasks, return_exceptions=True) loop.stop()
def error_handler(loop: AbstractEventLoop, context: Dict[str, Any]) -> None: if isinstance(context["exception"], AdapterException): try: loop.stop() loop.close() except Exception as exception: logging.debug(exception) else: logging.debug(context)
def test_locallogstreamer(capsys, event_loop: AbstractEventLoop) -> None: logger = StreamingLogger() logger.reset("xxx", "yyy") streamer = log_streamer.LocalLogStreamer( [ToyHandler()], logger, ) async def _toy_logging(logger: StreamingLogger, streamer: log_streamer.LocalLogStreamer) -> None: await asyncio.sleep(0.1) # logging by logger logger.log("test\n") # log will be sent into streamer, and handler, then printted out await asyncio.sleep(0.3) streamer.handle_exit(SIGINT, None) event_loop.create_task(streamer.entry_point()) event_loop.run_until_complete(_toy_logging(logger, streamer)) event_loop.stop() captured = capsys.readouterr() assert captured.out == "toy-test\n"
async def shutdown( # type: ignore[no-untyped-def] loop: AbstractEventLoop, logger: logging.Logger, teardown: AsyncFunction, signal=None # a named enum of ints ) -> None: '''Cancel active tasks for shutdown''' if signal: logger.info(f'Received exit signal {signal.name}') else: logger.info('Unexpeced shutdown initiated') await asyncio.sleep(5) # stall error loops if teardown: try: await teardown() except Exception: logger.exception('Error during teardown function') logger.error('Exiting uncleanly') sys.exit(1) tasks = [ t for t in asyncio.Task.all_tasks() if t is not asyncio.current_task() ] logger.info(f'Cancelling {len(tasks)} tasks') [task.cancel() for task in tasks] try: await asyncio.gather(*tasks, return_exceptions=True) except Exception: logger.exception('Error during loop task cancellation') logger.error('Exiting uncleanly') sys.exit(1) loop.stop()
def sigterm_handler(logger: Logger, event_loop: AbstractEventLoop) -> None: if event_loop.is_running(): logger.info('Received SIGTERM') event_loop.stop()
def error_handler(loop: AbstractEventLoop, context: Dict[str, Any]) -> None: if isinstance(context["exception"], AdapterException): loop.stop() loop.close()
def sigterm_handler(logger: Logger, event_loop: AbstractEventLoop) -> None: if event_loop.is_running(): logger.info('Received SIGTERM') event_loop.stop()