feat: 改为同步

This commit is contained in:
RockChinQ
2023-12-21 16:48:50 +08:00
parent b8776fba65
commit 7c6526d1ea
7 changed files with 34 additions and 39 deletions

View File

@@ -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:
@@ -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警告
@@ -486,5 +486,5 @@ async def main():
if __name__ == '__main__':
asyncio.run(main())
main()

View File

@@ -1,5 +1,4 @@
import threading
import asyncio
import traceback
from .. import aamgr
@@ -21,9 +20,9 @@ class UpdateCommand(aamgr.AbstractCommandNode):
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("无新版本")
@@ -32,10 +31,7 @@ class UpdateCommand(aamgr.AbstractCommandNode):
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]正在更新,请耐心等待,请勿重复发起更新..."]

View File

@@ -2,7 +2,7 @@ import abc
import uuid
import json
import aiohttp
import requests
class APIGroup(metaclass=abc.ABCMeta):
@@ -28,16 +28,15 @@ class APIGroup(metaclass=abc.ABCMeta):
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

View File

@@ -9,7 +9,7 @@ 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,
@@ -17,7 +17,7 @@ class V2MainDataAPI(apigroup.APIGroup):
new_version: str,
):
"""提交更新记录"""
return await self.do(
return self.do(
"POST",
"/update",
data={
@@ -31,12 +31,12 @@ class V2MainDataAPI(apigroup.APIGroup):
}
)
async def post_announcement_showed(
def post_announcement_showed(
self,
ids: list[int],
):
"""提交公告已阅"""
return await self.do(
return self.do(
"POST",
"/announcement",
data={

View File

@@ -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={
@@ -23,12 +23,12 @@ class V2PluginDataAPI(apigroup.APIGroup):
}
)
async def post_remove_record(
def post_remove_record(
self,
plugin: dict
):
"""提交插件卸载记录"""
return await self.do(
return self.do(
"POST",
"/remove",
data={
@@ -37,14 +37,14 @@ class V2PluginDataAPI(apigroup.APIGroup):
}
)
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={

View File

@@ -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,
@@ -20,7 +20,7 @@ class V2UsageDataAPI(apigroup.APIGroup):
retry_times: int,
):
"""提交请求记录"""
return await self.do(
return self.do(
"POST",
"/query",
data={
@@ -40,13 +40,13 @@ class V2UsageDataAPI(apigroup.APIGroup):
}
)
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={
@@ -59,14 +59,14 @@ class V2UsageDataAPI(apigroup.APIGroup):
}
)
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={

View File

@@ -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()
@@ -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,