2023-01-01 15:08:00 +08:00
|
|
|
"""None 驱动适配
|
|
|
|
|
|
|
|
:::tip 提示
|
|
|
|
本驱动不支持任何服务器或客户端连接
|
|
|
|
:::
|
|
|
|
|
|
|
|
FrontMatter:
|
2024-10-22 10:33:48 +08:00
|
|
|
mdx:
|
|
|
|
format: md
|
2023-01-01 15:08:00 +08:00
|
|
|
sidebar_position: 6
|
|
|
|
description: nonebot.drivers.none 模块
|
|
|
|
"""
|
|
|
|
|
2021-12-22 16:53:55 +08:00
|
|
|
import signal
|
2024-10-26 15:36:01 +08:00
|
|
|
from typing import Optional
|
2023-07-17 15:56:27 +08:00
|
|
|
from typing_extensions import override
|
2021-12-22 16:53:55 +08:00
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
import anyio
|
|
|
|
from anyio.abc import TaskGroup
|
|
|
|
from exceptiongroup import BaseExceptionGroup, catch
|
|
|
|
|
2021-12-22 16:53:55 +08:00
|
|
|
from nonebot.log import logger
|
2023-03-24 11:47:02 +08:00
|
|
|
from nonebot.consts import WINDOWS
|
2021-12-22 16:53:55 +08:00
|
|
|
from nonebot.config import Env, Config
|
2023-01-01 15:08:00 +08:00
|
|
|
from nonebot.drivers import Driver as BaseDriver
|
2024-10-26 15:36:01 +08:00
|
|
|
from nonebot.utils import flatten_exception_group
|
2021-12-22 16:53:55 +08:00
|
|
|
|
|
|
|
HANDLED_SIGNALS = (
|
|
|
|
signal.SIGINT, # Unix signal 2. Sent by Ctrl+C.
|
|
|
|
signal.SIGTERM, # Unix signal 15. Sent by `kill <pid>`.
|
|
|
|
)
|
2023-03-29 11:57:33 +08:00
|
|
|
if WINDOWS: # pragma: py-win32
|
2023-03-24 11:47:02 +08:00
|
|
|
HANDLED_SIGNALS += (signal.SIGBREAK,) # Windows signal 21. Sent by Ctrl+Break.
|
2021-12-22 16:53:55 +08:00
|
|
|
|
|
|
|
|
2023-01-01 15:08:00 +08:00
|
|
|
class Driver(BaseDriver):
|
|
|
|
"""None 驱动框架"""
|
|
|
|
|
2021-12-22 16:53:55 +08:00
|
|
|
def __init__(self, env: Env, config: Config):
|
|
|
|
super().__init__(env, config)
|
2023-03-29 15:59:54 +08:00
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
self.should_exit: anyio.Event = anyio.Event()
|
|
|
|
self.force_exit: anyio.Event = anyio.Event()
|
2021-12-22 16:53:55 +08:00
|
|
|
|
|
|
|
@property
|
2023-07-17 15:56:27 +08:00
|
|
|
@override
|
2021-12-22 16:53:55 +08:00
|
|
|
def type(self) -> str:
|
2023-01-01 15:08:00 +08:00
|
|
|
"""驱动名称: `none`"""
|
|
|
|
return "none"
|
2021-12-22 16:53:55 +08:00
|
|
|
|
|
|
|
@property
|
2023-07-17 15:56:27 +08:00
|
|
|
@override
|
2021-12-22 16:53:55 +08:00
|
|
|
def logger(self):
|
2023-01-01 15:08:00 +08:00
|
|
|
"""none driver 使用的 logger"""
|
2021-12-22 16:53:55 +08:00
|
|
|
return logger
|
|
|
|
|
2023-07-17 15:56:27 +08:00
|
|
|
@override
|
2021-12-22 16:53:55 +08:00
|
|
|
def run(self, *args, **kwargs):
|
2023-01-01 15:08:00 +08:00
|
|
|
"""启动 none driver"""
|
2021-12-22 16:53:55 +08:00
|
|
|
super().run(*args, **kwargs)
|
2024-10-26 15:36:01 +08:00
|
|
|
anyio.run(self._serve)
|
2021-12-22 16:53:55 +08:00
|
|
|
|
2023-01-01 15:08:00 +08:00
|
|
|
async def _serve(self):
|
2024-10-26 15:36:01 +08:00
|
|
|
async with anyio.create_task_group() as driver_tg:
|
|
|
|
driver_tg.start_soon(self._handle_signals)
|
|
|
|
driver_tg.start_soon(self._listen_force_exit, driver_tg)
|
|
|
|
driver_tg.start_soon(self._handle_lifespan, driver_tg)
|
2021-12-22 16:53:55 +08:00
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
async def _handle_signals(self):
|
2023-03-29 15:59:54 +08:00
|
|
|
try:
|
2024-10-26 15:36:01 +08:00
|
|
|
with anyio.open_signal_receiver(*HANDLED_SIGNALS) as signal_receiver:
|
|
|
|
async for sig in signal_receiver:
|
|
|
|
self.exit(force=self.should_exit.is_set())
|
|
|
|
except NotImplementedError:
|
|
|
|
# Windows
|
|
|
|
for sig in HANDLED_SIGNALS:
|
|
|
|
signal.signal(sig, self._handle_legacy_signal)
|
|
|
|
|
|
|
|
# backport for Windows signal handling
|
|
|
|
def _handle_legacy_signal(self, sig, frame):
|
|
|
|
self.exit(force=self.should_exit.is_set())
|
|
|
|
|
|
|
|
async def _handle_lifespan(self, tg: TaskGroup):
|
|
|
|
try:
|
|
|
|
await self._startup()
|
|
|
|
|
|
|
|
if self.should_exit.is_set():
|
|
|
|
return
|
|
|
|
|
|
|
|
await self._listen_exit()
|
|
|
|
|
|
|
|
await self._shutdown()
|
|
|
|
finally:
|
|
|
|
tg.cancel_scope.cancel()
|
|
|
|
|
|
|
|
async def _startup(self):
|
|
|
|
def handle_exception(exc_group: BaseExceptionGroup[Exception]) -> None:
|
|
|
|
self.should_exit.set()
|
|
|
|
|
|
|
|
for exc in flatten_exception_group(exc_group):
|
|
|
|
logger.opt(colors=True, exception=exc).error(
|
|
|
|
"<r><bg #f8bbd0>Error occurred while running startup hook."
|
|
|
|
"</bg #f8bbd0></r>"
|
|
|
|
)
|
|
|
|
logger.error(
|
2024-05-01 13:38:47 +08:00
|
|
|
"<r><bg #f8bbd0>Application startup failed. "
|
|
|
|
"Exiting.</bg #f8bbd0></r>"
|
2023-03-29 15:59:54 +08:00
|
|
|
)
|
2021-12-22 16:53:55 +08:00
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
with catch({Exception: handle_exception}):
|
|
|
|
await self._lifespan.startup()
|
|
|
|
|
|
|
|
if not self.should_exit.is_set():
|
|
|
|
logger.info("Application startup completed.")
|
2021-12-22 16:53:55 +08:00
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
async def _listen_exit(self, tg: Optional[TaskGroup] = None):
|
2021-12-22 16:53:55 +08:00
|
|
|
await self.should_exit.wait()
|
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
if tg is not None:
|
|
|
|
tg.cancel_scope.cancel()
|
|
|
|
|
2023-01-01 15:08:00 +08:00
|
|
|
async def _shutdown(self):
|
2021-12-22 16:53:55 +08:00
|
|
|
logger.info("Shutting down")
|
2024-10-26 15:36:01 +08:00
|
|
|
logger.info("Waiting for application shutdown. (CTRL+C to force quit)")
|
2021-12-22 16:53:55 +08:00
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
error_occurred: bool = False
|
2021-12-22 16:53:55 +08:00
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
def handle_exception(exc_group: BaseExceptionGroup[Exception]) -> None:
|
|
|
|
nonlocal error_occurred
|
2021-12-22 16:53:55 +08:00
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
error_occurred = True
|
2021-12-22 16:53:55 +08:00
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
for exc in flatten_exception_group(exc_group):
|
|
|
|
logger.opt(colors=True, exception=exc).error(
|
|
|
|
"<r><bg #f8bbd0>Error occurred while running shutdown hook."
|
|
|
|
"</bg #f8bbd0></r>"
|
|
|
|
)
|
|
|
|
logger.error(
|
|
|
|
"<r><bg #f8bbd0>Application shutdown failed. "
|
|
|
|
"Exiting.</bg #f8bbd0></r>"
|
|
|
|
)
|
2021-12-22 16:53:55 +08:00
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
with catch({Exception: handle_exception}):
|
|
|
|
await self._lifespan.shutdown()
|
2021-12-22 16:53:55 +08:00
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
if not error_occurred:
|
|
|
|
logger.info("Application shutdown complete.")
|
2021-12-22 16:53:55 +08:00
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
async def _listen_force_exit(self, tg: TaskGroup):
|
|
|
|
await self.force_exit.wait()
|
|
|
|
tg.cancel_scope.cancel()
|
2023-04-27 17:26:59 +08:00
|
|
|
|
|
|
|
def exit(self, force: bool = False):
|
|
|
|
"""退出 none driver
|
|
|
|
|
|
|
|
参数:
|
|
|
|
force: 强制退出
|
|
|
|
"""
|
|
|
|
if not self.should_exit.is_set():
|
2021-12-22 16:53:55 +08:00
|
|
|
self.should_exit.set()
|
2023-04-27 17:26:59 +08:00
|
|
|
if force:
|
2024-10-26 15:36:01 +08:00
|
|
|
self.force_exit.set()
|