From b8776fba6579064bcc5a424a358b878b7c812a5f Mon Sep 17 00:00:00 2001 From: RockChinQ <1010553892@qq.com> Date: Thu, 21 Dec 2023 16:44:21 +0800 Subject: [PATCH] chore: stash --- main.py | 6 +++--- pkg/qqbot/cmds/system/update.py | 10 +++++++--- pkg/utils/updater.py | 16 +++++++++++++++- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/main.py b/main.py index 5fe42013..ebb98e92 100644 --- a/main.py +++ b/main.py @@ -422,7 +422,7 @@ def stop(): raise e -def main(): +async def main(): global use_override # 检查是否携带了 --override 或 -r 参数 if '--override' in sys.argv or '-r' in sys.argv: @@ -450,7 +450,7 @@ def main(): elif len(sys.argv) > 1 and sys.argv[1] == 'update': print("正在进行程序更新...") import pkg.utils.updater as updater - updater.update_all(cli=True) + await updater.update_all(cli=True) sys.exit(0) # 关闭urllib的http警告 @@ -486,5 +486,5 @@ def run_wrapper(): if __name__ == '__main__': - main() + asyncio.run(main()) diff --git a/pkg/qqbot/cmds/system/update.py b/pkg/qqbot/cmds/system/update.py index d4cca3f3..6d97cd9e 100644 --- a/pkg/qqbot/cmds/system/update.py +++ b/pkg/qqbot/cmds/system/update.py @@ -1,4 +1,5 @@ import threading +import asyncio import traceback from .. import aamgr @@ -20,9 +21,9 @@ def process(cls, ctx: aamgr.Context) -> tuple[bool, list]: import pkg.utils.reloader import pkg.utils.context - def update_task(): + async def update_task(): try: - if pkg.utils.updater.update_all(): + if await pkg.utils.updater.update_all(): pkg.utils.context.get_qqbot_manager().notify_admin("更新完成, 请手动重启程序。") else: pkg.utils.context.get_qqbot_manager().notify_admin("无新版本") @@ -31,7 +32,10 @@ def update_task(): pkg.utils.context.get_qqbot_manager().notify_admin("更新失败:{}".format(e0)) return - threading.Thread(target=update_task, daemon=True).start() + def wrapper(): + asyncio.run(update_task()) + + threading.Thread(target=wrapper).start() reply = ["[bot]正在更新,请耐心等待,请勿重复发起更新..."] diff --git a/pkg/utils/updater.py b/pkg/utils/updater.py index 767f3621..abbc696a 100644 --- a/pkg/utils/updater.py +++ b/pkg/utils/updater.py @@ -1,11 +1,15 @@ +from __future__ import annotations + import datetime import logging import os.path +import time import requests from . import constants from . import network +from . import context def check_dulwich_closure(): @@ -105,9 +109,12 @@ def compare_version_str(v0: str, v1: str) -> int: return 0 -def update_all(cli: bool = False) -> bool: +async def update_all(cli: bool = False) -> bool: """检查更新并下载源码""" + start_time = time.time() + current_tag = get_current_tag() + old_tag = current_tag rls_list = get_release_list() @@ -200,6 +207,13 @@ def update_all(cli: bool = False) -> bool: with open("current_tag", "w") as f: f.write(current_tag) + await context.get_center_v2_api().main.post_update_record( + spent_seconds=int(time.time()-start_time), + infer_reason="update", + old_version=old_tag, + new_version=current_tag, + ) + # 通知管理员 if not cli: import pkg.utils.context