nonebot-plugin-shutdown-hook


Namenonebot-plugin-shutdown-hook JSON
Version 0.1.2 PyPI version JSON
download
home_pagehttps://github.com/Sclock/nonebot-plugin-shutdown-hook
Summary高优先级关闭信号钩子插件
upload_time2024-05-26 15:33:02
maintainerNone
docs_urlNone
authorSclock
requires_python>=3.9
licenseMIT
keywords nonebot plugin shutdown hook
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # nonebot-plugin-shutdown-hook

该插件提供一个高优先级关闭信号钩子,用于在关闭时,Bot断开之前执行一些操作
例如对所有未关闭的Matcher进行回复,修改Bot昵称或签名为已关机等

在 nonebot.run() 之前调用此函数, 用 on_shutdown_before 装饰需要执行的函数即可

通过修改 Server.handle_exit,在 handle_exit 函数接收到中止信号后并不立刻中止,而是将信号存储在 SIGNALS_LIST,并立刻执行注册的钩子函数。钩子函数执行完毕后,依次从 SIGNALS_LIST 取出信号,重新传入原 handle_exit 函数处理。

此钩子优先级非常高,如果在钩子时发生错误,可能会导致程序无法正常关闭。

## 安装

```shell
pip install nonebot-plugin-shutdown-hook
```

## 使用

### 导入

```python
from nonebot-plugin-shutdown-hook import override_uvicorn_shutdown_hooks, on_shutdown_before
```

### 创建


```python
# bot.py 文件
...
override_uvicorn_shutdown_hooks()
...
if __name__ == "__main__":
    nonebot.run()
...
```


```python
@on_shutdown_before()
async def func():
    # 执行一些操作
    ...
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Sclock/nonebot-plugin-shutdown-hook",
    "name": "nonebot-plugin-shutdown-hook",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "nonebot, plugin, shutdown, hook",
    "author": "Sclock",
    "author_email": "1342810270@qq.com",
    "download_url": "https://files.pythonhosted.org/packages/5c/2b/6e48f54f0a2e9717b6956f77028e581134943f53bb2e91158b648bf92d25/nonebot_plugin_shutdown_hook-0.1.2.tar.gz",
    "platform": null,
    "description": "# nonebot-plugin-shutdown-hook\n\n\u8be5\u63d2\u4ef6\u63d0\u4f9b\u4e00\u4e2a\u9ad8\u4f18\u5148\u7ea7\u5173\u95ed\u4fe1\u53f7\u94a9\u5b50,\u7528\u4e8e\u5728\u5173\u95ed\u65f6,Bot\u65ad\u5f00\u4e4b\u524d\u6267\u884c\u4e00\u4e9b\u64cd\u4f5c\n\u4f8b\u5982\u5bf9\u6240\u6709\u672a\u5173\u95ed\u7684Matcher\u8fdb\u884c\u56de\u590d,\u4fee\u6539Bot\u6635\u79f0\u6216\u7b7e\u540d\u4e3a\u5df2\u5173\u673a\u7b49\n\n\u5728 nonebot.run() \u4e4b\u524d\u8c03\u7528\u6b64\u51fd\u6570, \u7528 on_shutdown_before \u88c5\u9970\u9700\u8981\u6267\u884c\u7684\u51fd\u6570\u5373\u53ef\n\n\u901a\u8fc7\u4fee\u6539 Server.handle_exit\uff0c\u5728 handle_exit \u51fd\u6570\u63a5\u6536\u5230\u4e2d\u6b62\u4fe1\u53f7\u540e\u5e76\u4e0d\u7acb\u523b\u4e2d\u6b62\uff0c\u800c\u662f\u5c06\u4fe1\u53f7\u5b58\u50a8\u5728 SIGNALS_LIST\uff0c\u5e76\u7acb\u523b\u6267\u884c\u6ce8\u518c\u7684\u94a9\u5b50\u51fd\u6570\u3002\u94a9\u5b50\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u4f9d\u6b21\u4ece SIGNALS_LIST \u53d6\u51fa\u4fe1\u53f7\uff0c\u91cd\u65b0\u4f20\u5165\u539f handle_exit \u51fd\u6570\u5904\u7406\u3002\n\n\u6b64\u94a9\u5b50\u4f18\u5148\u7ea7\u975e\u5e38\u9ad8\uff0c\u5982\u679c\u5728\u94a9\u5b50\u65f6\u53d1\u751f\u9519\u8bef\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u65e0\u6cd5\u6b63\u5e38\u5173\u95ed\u3002\n\n## \u5b89\u88c5\n\n```shell\npip install nonebot-plugin-shutdown-hook\n```\n\n## \u4f7f\u7528\n\n### \u5bfc\u5165\n\n```python\nfrom nonebot-plugin-shutdown-hook import override_uvicorn_shutdown_hooks, on_shutdown_before\n```\n\n### \u521b\u5efa\n\n\n```python\n# bot.py \u6587\u4ef6\n...\noverride_uvicorn_shutdown_hooks()\n...\nif __name__ == \"__main__\":\n    nonebot.run()\n...\n```\n\n\n```python\n@on_shutdown_before()\nasync def func():\n    # \u6267\u884c\u4e00\u4e9b\u64cd\u4f5c\n    ...\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "\u9ad8\u4f18\u5148\u7ea7\u5173\u95ed\u4fe1\u53f7\u94a9\u5b50\u63d2\u4ef6",
    "version": "0.1.2",
    "project_urls": {
        "Documentation": "https://github.com/Sclock/nonebot-plugin-shutdown-hook",
        "Homepage": "https://github.com/Sclock/nonebot-plugin-shutdown-hook",
        "Repository": "https://github.com/Sclock/nonebot-plugin-shutdown-hook"
    },
    "split_keywords": [
        "nonebot",
        " plugin",
        " shutdown",
        " hook"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "54cd5fbc9968d8d4c8681912d5b05fbb4d49e2f882eee68d4f883f3625a01e50",
                "md5": "6ae384e4e897f7854742e2e8da167db3",
                "sha256": "e36ce3c8bbbc5c841f9415104f420372385637097d6b1ffe89fd0203d44f46de"
            },
            "downloads": -1,
            "filename": "nonebot_plugin_shutdown_hook-0.1.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "6ae384e4e897f7854742e2e8da167db3",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 3624,
            "upload_time": "2024-05-26T15:33:00",
            "upload_time_iso_8601": "2024-05-26T15:33:00.679461Z",
            "url": "https://files.pythonhosted.org/packages/54/cd/5fbc9968d8d4c8681912d5b05fbb4d49e2f882eee68d4f883f3625a01e50/nonebot_plugin_shutdown_hook-0.1.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5c2b6e48f54f0a2e9717b6956f77028e581134943f53bb2e91158b648bf92d25",
                "md5": "e616b6be5f083e7369240bdca596f241",
                "sha256": "6c52088092c24e7a21a78ad043309f4c94aef5b6cbf77d6e372064e11ff1e605"
            },
            "downloads": -1,
            "filename": "nonebot_plugin_shutdown_hook-0.1.2.tar.gz",
            "has_sig": false,
            "md5_digest": "e616b6be5f083e7369240bdca596f241",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 3210,
            "upload_time": "2024-05-26T15:33:02",
            "upload_time_iso_8601": "2024-05-26T15:33:02.638427Z",
            "url": "https://files.pythonhosted.org/packages/5c/2b/6e48f54f0a2e9717b6956f77028e581134943f53bb2e91158b648bf92d25/nonebot_plugin_shutdown_hook-0.1.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-05-26 15:33:02",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Sclock",
    "github_project": "nonebot-plugin-shutdown-hook",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "nonebot-plugin-shutdown-hook"
}
        
Elapsed time: 0.22944s