Skip to content

Commit

Permalink
feat: 改为同步
Browse files Browse the repository at this point in the history
  • Loading branch information
RockChinQ committed Dec 21, 2023
1 parent b8776fb commit 7c6526d
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 39 deletions.
6 changes: 3 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ def stop():
raise e


async def main():
def main():
global use_override
# 检查是否携带了 --override 或 -r 参数
if '--override' in sys.argv or '-r' in sys.argv:
Expand Down Expand Up @@ -450,7 +450,7 @@ async def main():
elif len(sys.argv) > 1 and sys.argv[1] == 'update':
print("正在进行程序更新...")
import pkg.utils.updater as updater
await updater.update_all(cli=True)
updater.update_all(cli=True)
sys.exit(0)

# 关闭urllib的http警告
Expand Down Expand Up @@ -486,5 +486,5 @@ def run_wrapper():


if __name__ == '__main__':
asyncio.run(main())
main()

10 changes: 3 additions & 7 deletions pkg/qqbot/cmds/system/update.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import threading
import asyncio
import traceback

from .. import aamgr
Expand All @@ -21,9 +20,9 @@ def process(cls, ctx: aamgr.Context) -> tuple[bool, list]:
import pkg.utils.reloader
import pkg.utils.context

async def update_task():
def update_task():
try:
if await pkg.utils.updater.update_all():
if pkg.utils.updater.update_all():
pkg.utils.context.get_qqbot_manager().notify_admin("更新完成, 请手动重启程序。")
else:
pkg.utils.context.get_qqbot_manager().notify_admin("无新版本")
Expand All @@ -32,10 +31,7 @@ async def update_task():
pkg.utils.context.get_qqbot_manager().notify_admin("更新失败:{}".format(e0))
return

def wrapper():
asyncio.run(update_task())

threading.Thread(target=wrapper).start()
threading.Thread(target=update_task, daemon=True).start()

reply = ["[bot]正在更新,请耐心等待,请勿重复发起更新..."]

Expand Down
21 changes: 10 additions & 11 deletions pkg/utils/center/apigroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import uuid
import json

import aiohttp
import requests


class APIGroup(metaclass=abc.ABCMeta):
Expand All @@ -28,16 +28,15 @@ async def do(
url = self.prefix + path
data = json.dumps(data)
headers['Content-Type'] = 'application/json'
async with aiohttp.ClientSession() as session:
async with session.request(
method,
url,
data=data,
params=params,
headers=headers,
**kwargs
) as resp:
return await resp.json()

return requests.request(
method,
url,
data=data,
params=params,
headers=headers,
**kwargs
)

def gen_rid(
self
Expand Down
8 changes: 4 additions & 4 deletions pkg/utils/center/groups/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ class V2MainDataAPI(apigroup.APIGroup):
def __init__(self, prefix: str):
super().__init__(prefix+"/main")

async def post_update_record(
def post_update_record(
self,
spent_seconds: int,
infer_reason: str,
old_version: str,
new_version: str,
):
"""提交更新记录"""
return await self.do(
return self.do(
"POST",
"/update",
data={
Expand All @@ -31,12 +31,12 @@ async def post_update_record(
}
)

async def post_announcement_showed(
def post_announcement_showed(
self,
ids: list[int],
):
"""提交公告已阅"""
return await self.do(
return self.do(
"POST",
"/announcement",
data={
Expand Down
12 changes: 6 additions & 6 deletions pkg/utils/center/groups/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ class V2PluginDataAPI(apigroup.APIGroup):
def __init__(self, prefix: str):
super().__init__(prefix+"/plugin")

async def post_install_record(
def post_install_record(
self,
plugin: dict
):
"""提交插件安装记录"""
return await self.do(
return self.do(
"POST",
"/install",
data={
Expand All @@ -23,12 +23,12 @@ async def post_install_record(
}
)

async def post_remove_record(
def post_remove_record(
self,
plugin: dict
):
"""提交插件卸载记录"""
return await self.do(
return self.do(
"POST",
"/remove",
data={
Expand All @@ -37,14 +37,14 @@ async def post_remove_record(
}
)

async def post_update_record(
def post_update_record(
self,
plugin: dict,
old_version: str,
new_version: str,
):
"""提交插件更新记录"""
return await self.do(
return self.do(
"POST",
"/update",
data={
Expand Down
12 changes: 6 additions & 6 deletions pkg/utils/center/groups/usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class V2UsageDataAPI(apigroup.APIGroup):
def __init__(self, prefix: str):
super().__init__(prefix+"/usage")

async def post_query_record(
def post_query_record(
self,
session_type: str,
session_id: str,
Expand All @@ -20,7 +20,7 @@ async def post_query_record(
retry_times: int,
):
"""提交请求记录"""
return await self.do(
return self.do(
"POST",
"/query",
data={
Expand All @@ -40,13 +40,13 @@ async def post_query_record(
}
)

async def post_event_record(
def post_event_record(
self,
plugins: list[dict],
event_name: str,
):
"""提交事件触发记录"""
return await self.do(
return self.do(
"POST",
"/event",
data={
Expand All @@ -59,14 +59,14 @@ async def post_event_record(
}
)

async def post_function_record(
def post_function_record(
self,
plugin: dict,
function_name: str,
function_description: str,
):
"""提交内容函数使用记录"""
return await self.do(
return self.do(
"POST",
"/function",
data={
Expand Down
4 changes: 2 additions & 2 deletions pkg/utils/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def compare_version_str(v0: str, v1: str) -> int:
return 0


async def update_all(cli: bool = False) -> bool:
def update_all(cli: bool = False) -> bool:
"""检查更新并下载源码"""
start_time = time.time()

Expand Down Expand Up @@ -207,7 +207,7 @@ async 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(
context.get_center_v2_api().main.post_update_record(
spent_seconds=int(time.time()-start_time),
infer_reason="update",
old_version=old_tag,
Expand Down

0 comments on commit 7c6526d

Please sign in to comment.