2024-08-21 17:59:21 +08:00
|
|
|
---
|
|
|
|
title: liteyuki.bot.lifespan
|
|
|
|
---
|
2024-08-29 14:19:39 +08:00
|
|
|
### **class** `Lifespan`
|
|
|
|
### *method* `__init__(self) -> None`
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
**Description**: 轻雪生命周期管理,启动、停止、重启
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
<details>
|
2024-08-29 14:19:39 +08:00
|
|
|
<summary> <b>Source code</b> </summary>
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
```python
|
|
|
|
def __init__(self) -> None:
|
|
|
|
"""
|
|
|
|
轻雪生命周期管理,启动、停止、重启
|
|
|
|
"""
|
|
|
|
self.life_flag: int = 0
|
|
|
|
self._before_start_funcs: list[LIFESPAN_FUNC] = []
|
|
|
|
self._after_start_funcs: list[LIFESPAN_FUNC] = []
|
|
|
|
self._before_process_shutdown_funcs: list[LIFESPAN_FUNC] = []
|
|
|
|
self._after_shutdown_funcs: list[LIFESPAN_FUNC] = []
|
|
|
|
self._before_process_restart_funcs: list[LIFESPAN_FUNC] = []
|
|
|
|
self._after_restart_funcs: list[LIFESPAN_FUNC] = []
|
|
|
|
self._after_nonebot_init_funcs: list[LIFESPAN_FUNC] = []
|
|
|
|
```
|
|
|
|
</details>
|
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
### `@staticmethod`
|
|
|
|
### *method* `run_funcs(funcs: list[LIFESPAN_FUNC | PROCESS_LIFESPAN_FUNC]) -> None`
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
**Description**: 运行函数
|
|
|
|
|
|
|
|
**Arguments**:
|
|
|
|
> - funcs:
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
<details>
|
2024-08-29 14:19:39 +08:00
|
|
|
<summary> <b>Source code</b> </summary>
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
```python
|
|
|
|
@staticmethod
|
|
|
|
def run_funcs(funcs: list[LIFESPAN_FUNC | PROCESS_LIFESPAN_FUNC], *args, **kwargs) -> None:
|
|
|
|
"""
|
|
|
|
运行函数
|
|
|
|
Args:
|
|
|
|
funcs:
|
|
|
|
Returns:
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
except RuntimeError:
|
|
|
|
loop = asyncio.new_event_loop()
|
|
|
|
asyncio.set_event_loop(loop)
|
|
|
|
tasks = []
|
|
|
|
for func in funcs:
|
|
|
|
if is_coroutine_callable(func):
|
|
|
|
tasks.append(func(*args, **kwargs))
|
|
|
|
else:
|
|
|
|
tasks.append(async_wrapper(func)(*args, **kwargs))
|
|
|
|
loop.run_until_complete(asyncio.gather(*tasks))
|
|
|
|
```
|
|
|
|
</details>
|
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
### *method* `on_before_start(self, func: LIFESPAN_FUNC) -> LIFESPAN_FUNC`
|
|
|
|
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
**Description**: 注册启动时的函数
|
2024-08-21 17:59:21 +08:00
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
**Arguments**:
|
|
|
|
> - func:
|
2024-08-21 17:59:21 +08:00
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
**Return**: LIFESPAN_FUNC:
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
<details>
|
2024-08-29 14:19:39 +08:00
|
|
|
<summary> <b>Source code</b> </summary>
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
```python
|
|
|
|
def on_before_start(self, func: LIFESPAN_FUNC) -> LIFESPAN_FUNC:
|
|
|
|
"""
|
|
|
|
注册启动时的函数
|
|
|
|
Args:
|
|
|
|
func:
|
|
|
|
Returns:
|
|
|
|
LIFESPAN_FUNC:
|
|
|
|
"""
|
|
|
|
self._before_start_funcs.append(func)
|
|
|
|
return func
|
|
|
|
```
|
|
|
|
</details>
|
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
### *method* `on_after_start(self, func: LIFESPAN_FUNC) -> LIFESPAN_FUNC`
|
|
|
|
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
**Description**: 注册启动时的函数
|
2024-08-21 17:59:21 +08:00
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
**Arguments**:
|
|
|
|
> - func:
|
2024-08-21 17:59:21 +08:00
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
**Return**: LIFESPAN_FUNC:
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
<details>
|
2024-08-29 14:19:39 +08:00
|
|
|
<summary> <b>Source code</b> </summary>
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
```python
|
|
|
|
def on_after_start(self, func: LIFESPAN_FUNC) -> LIFESPAN_FUNC:
|
|
|
|
"""
|
|
|
|
注册启动时的函数
|
|
|
|
Args:
|
|
|
|
func:
|
|
|
|
Returns:
|
|
|
|
LIFESPAN_FUNC:
|
|
|
|
"""
|
|
|
|
self._after_start_funcs.append(func)
|
|
|
|
return func
|
|
|
|
```
|
|
|
|
</details>
|
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
### *method* `on_before_process_shutdown(self, func: LIFESPAN_FUNC) -> LIFESPAN_FUNC`
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
**Description**: 注册停止前的函数
|
2024-08-21 17:59:21 +08:00
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
**Arguments**:
|
|
|
|
> - func:
|
|
|
|
|
|
|
|
**Return**: LIFESPAN_FUNC:
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
<details>
|
2024-08-29 14:19:39 +08:00
|
|
|
<summary> <b>Source code</b> </summary>
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
```python
|
|
|
|
def on_before_process_shutdown(self, func: LIFESPAN_FUNC) -> LIFESPAN_FUNC:
|
|
|
|
"""
|
|
|
|
注册停止前的函数
|
|
|
|
Args:
|
|
|
|
func:
|
|
|
|
Returns:
|
|
|
|
LIFESPAN_FUNC:
|
|
|
|
"""
|
|
|
|
self._before_process_shutdown_funcs.append(func)
|
|
|
|
return func
|
|
|
|
```
|
|
|
|
</details>
|
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
### *method* `on_after_shutdown(self, func: LIFESPAN_FUNC) -> LIFESPAN_FUNC`
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
**Description**: 注册停止后的函数
|
2024-08-21 17:59:21 +08:00
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
**Arguments**:
|
|
|
|
> - func:
|
2024-08-21 17:59:21 +08:00
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
**Return**: LIFESPAN_FUNC:
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
<details>
|
2024-08-29 14:19:39 +08:00
|
|
|
<summary> <b>Source code</b> </summary>
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
```python
|
|
|
|
def on_after_shutdown(self, func: LIFESPAN_FUNC) -> LIFESPAN_FUNC:
|
|
|
|
"""
|
|
|
|
注册停止后的函数
|
|
|
|
Args:
|
|
|
|
func:
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
LIFESPAN_FUNC:
|
|
|
|
|
|
|
|
"""
|
|
|
|
self._after_shutdown_funcs.append(func)
|
|
|
|
return func
|
|
|
|
```
|
|
|
|
</details>
|
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
### *method* `on_before_process_restart(self, func: LIFESPAN_FUNC) -> LIFESPAN_FUNC`
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
**Description**: 注册重启时的函数
|
2024-08-21 17:59:21 +08:00
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
**Arguments**:
|
|
|
|
> - func:
|
|
|
|
|
|
|
|
**Return**: LIFESPAN_FUNC:
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
<details>
|
2024-08-29 14:19:39 +08:00
|
|
|
<summary> <b>Source code</b> </summary>
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
```python
|
|
|
|
def on_before_process_restart(self, func: LIFESPAN_FUNC) -> LIFESPAN_FUNC:
|
|
|
|
"""
|
|
|
|
注册重启时的函数
|
|
|
|
Args:
|
|
|
|
func:
|
|
|
|
Returns:
|
|
|
|
LIFESPAN_FUNC:
|
|
|
|
"""
|
|
|
|
self._before_process_restart_funcs.append(func)
|
|
|
|
return func
|
|
|
|
```
|
|
|
|
</details>
|
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
### *method* `on_after_restart(self, func: LIFESPAN_FUNC) -> LIFESPAN_FUNC`
|
|
|
|
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
**Description**: 注册重启后的函数
|
2024-08-21 17:59:21 +08:00
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
**Arguments**:
|
|
|
|
> - func:
|
2024-08-21 17:59:21 +08:00
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
**Return**: LIFESPAN_FUNC:
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
<details>
|
2024-08-29 14:19:39 +08:00
|
|
|
<summary> <b>Source code</b> </summary>
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
```python
|
|
|
|
def on_after_restart(self, func: LIFESPAN_FUNC) -> LIFESPAN_FUNC:
|
|
|
|
"""
|
|
|
|
注册重启后的函数
|
|
|
|
Args:
|
|
|
|
func:
|
|
|
|
Returns:
|
|
|
|
LIFESPAN_FUNC:
|
|
|
|
"""
|
|
|
|
self._after_restart_funcs.append(func)
|
|
|
|
return func
|
|
|
|
```
|
|
|
|
</details>
|
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
### *method* `on_after_nonebot_init(self, func)`
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
**Description**: 注册 NoneBot 初始化后的函数
|
2024-08-21 17:59:21 +08:00
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
**Arguments**:
|
|
|
|
> - func:
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
<details>
|
2024-08-29 14:19:39 +08:00
|
|
|
<summary> <b>Source code</b> </summary>
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
```python
|
|
|
|
def on_after_nonebot_init(self, func):
|
|
|
|
"""
|
|
|
|
注册 NoneBot 初始化后的函数
|
|
|
|
Args:
|
|
|
|
func:
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
self._after_nonebot_init_funcs.append(func)
|
|
|
|
return func
|
|
|
|
```
|
|
|
|
</details>
|
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
### *method* `before_start(self) -> None`
|
|
|
|
|
|
|
|
|
2024-08-21 17:59:21 +08:00
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
**Description**: 启动前
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
<details>
|
2024-08-29 14:19:39 +08:00
|
|
|
<summary> <b>Source code</b> </summary>
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
```python
|
|
|
|
def before_start(self) -> None:
|
|
|
|
"""
|
|
|
|
启动前
|
|
|
|
Returns:
|
|
|
|
"""
|
|
|
|
logger.debug('Running before_start functions')
|
|
|
|
self.run_funcs(self._before_start_funcs)
|
|
|
|
```
|
|
|
|
</details>
|
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
### *method* `after_start(self) -> None`
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
|
|
|
|
**Description**: 启动后
|
|
|
|
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
<details>
|
2024-08-29 14:19:39 +08:00
|
|
|
<summary> <b>Source code</b> </summary>
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
```python
|
|
|
|
def after_start(self) -> None:
|
|
|
|
"""
|
|
|
|
启动后
|
|
|
|
Returns:
|
|
|
|
"""
|
|
|
|
logger.debug('Running after_start functions')
|
|
|
|
self.run_funcs(self._after_start_funcs)
|
|
|
|
```
|
|
|
|
</details>
|
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
### *method* `before_process_shutdown(self) -> None`
|
|
|
|
|
|
|
|
|
2024-08-21 17:59:21 +08:00
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
**Description**: 停止前
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
<details>
|
2024-08-29 14:19:39 +08:00
|
|
|
<summary> <b>Source code</b> </summary>
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
```python
|
|
|
|
def before_process_shutdown(self) -> None:
|
|
|
|
"""
|
|
|
|
停止前
|
|
|
|
Returns:
|
|
|
|
"""
|
|
|
|
logger.debug('Running before_shutdown functions')
|
|
|
|
self.run_funcs(self._before_process_shutdown_funcs)
|
|
|
|
```
|
|
|
|
</details>
|
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
### *method* `after_shutdown(self) -> None`
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
|
|
|
|
**Description**: 停止后
|
|
|
|
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
<details>
|
2024-08-29 14:19:39 +08:00
|
|
|
<summary> <b>Source code</b> </summary>
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
```python
|
|
|
|
def after_shutdown(self) -> None:
|
|
|
|
"""
|
|
|
|
停止后
|
|
|
|
Returns:
|
|
|
|
"""
|
|
|
|
logger.debug('Running after_shutdown functions')
|
|
|
|
self.run_funcs(self._after_shutdown_funcs)
|
|
|
|
```
|
|
|
|
</details>
|
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
### *method* `before_process_restart(self) -> None`
|
|
|
|
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
**Description**: 重启前
|
|
|
|
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
<details>
|
2024-08-29 14:19:39 +08:00
|
|
|
<summary> <b>Source code</b> </summary>
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
```python
|
|
|
|
def before_process_restart(self) -> None:
|
|
|
|
"""
|
|
|
|
重启前
|
|
|
|
Returns:
|
|
|
|
"""
|
|
|
|
logger.debug('Running before_restart functions')
|
|
|
|
self.run_funcs(self._before_process_restart_funcs)
|
|
|
|
```
|
|
|
|
</details>
|
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
### *method* `after_restart(self) -> None`
|
|
|
|
|
|
|
|
|
2024-08-21 17:59:21 +08:00
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
**Description**: 重启后
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
<details>
|
2024-08-29 14:19:39 +08:00
|
|
|
<summary> <b>Source code</b> </summary>
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
```python
|
|
|
|
def after_restart(self) -> None:
|
|
|
|
"""
|
|
|
|
重启后
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
logger.debug('Running after_restart functions')
|
|
|
|
self.run_funcs(self._after_restart_funcs)
|
|
|
|
```
|
|
|
|
</details>
|
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
### ***var*** `SYNC_LIFESPAN_FUNC = Callable[[], Any]`
|
|
|
|
|
|
|
|
- **Type**: `TypeAlias`
|
|
|
|
|
|
|
|
### ***var*** `ASYNC_LIFESPAN_FUNC = Callable[[], Awaitable[Any]]`
|
|
|
|
|
|
|
|
- **Type**: `TypeAlias`
|
2024-08-21 17:59:21 +08:00
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
### ***var*** `LIFESPAN_FUNC = SYNC_LIFESPAN_FUNC | ASYNC_LIFESPAN_FUNC`
|
2024-08-21 17:59:21 +08:00
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
- **Type**: `TypeAlias`
|
2024-08-21 17:59:21 +08:00
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
### ***var*** `SYNC_PROCESS_LIFESPAN_FUNC = Callable[[str], Any]`
|
2024-08-21 17:59:21 +08:00
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
- **Type**: `TypeAlias`
|
2024-08-21 17:59:21 +08:00
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
### ***var*** `ASYNC_PROCESS_LIFESPAN_FUNC = Callable[[str], Awaitable[Any]]`
|
2024-08-21 17:59:21 +08:00
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
- **Type**: `TypeAlias`
|
2024-08-21 17:59:21 +08:00
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
### ***var*** `PROCESS_LIFESPAN_FUNC = SYNC_PROCESS_LIFESPAN_FUNC | ASYNC_PROCESS_LIFESPAN_FUNC`
|
2024-08-21 17:59:21 +08:00
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
- **Type**: `TypeAlias`
|
2024-08-21 17:59:21 +08:00
|
|
|
|