fix: 修复重启时 aiocqhttp 没有正常退出导致端口占用的问题

This commit is contained in:
Soulter
2024-08-11 02:30:49 -04:00
parent 121c40f273
commit 266da0a9d8
6 changed files with 13 additions and 7 deletions
+3
View File
@@ -100,6 +100,9 @@ class AstrBotBootstrap():
try:
result = await task
return result
except asyncio.CancelledError:
logger.info(f"{task.get_name()} 任务已取消。")
return
except Exception as e:
logger.error(traceback.format_exc())
logger.error(f"{task.get_name()} 任务发生错误,将在 5 秒后重试。")
+2 -2
View File
@@ -311,7 +311,7 @@ class AstrBotDashBoard():
latest = False
try:
self.astrbot_updator.update(latest=latest, version=version)
threading.Thread(target=self.astrbot_updator._reboot, args=(3, )).start()
threading.Thread(target=self.astrbot_updator._reboot, args=(2, self.context)).start()
return Response(
status="success",
message="更新成功,机器人将在 3 秒内重启。",
@@ -374,7 +374,7 @@ class AstrBotDashBoard():
self.dashboard_data, self.context.config_helper.get_all())
# 重启
threading.Thread(target=self.astrbot_updator._reboot,
args=(2, ), daemon=True).start()
args=(2, self.context), daemon=True).start()
except Exception as e:
raise e
+2 -2
View File
@@ -117,11 +117,11 @@ class InternalCommandHandler:
success=False,
message_chain="你没有权限使用该指令",
)
context.updator._reboot(5)
context.updator._reboot(3, context)
return CommandResult(
hit=True,
success=True,
message_chain="AstrBot 将在 5s 后重启。",
message_chain="AstrBot 将在 3s 后重启。",
)
def plugin(self, message: AstrMessageEvent, context: Context):
+1 -1
View File
@@ -99,7 +99,7 @@ class AIOCQHTTP(Platform):
return bot
async def shutdown_trigger_placeholder(self):
while True:
while self.context.running:
await asyncio.sleep(1)
def pre_check(self, message: AstrBotMessage) -> bool:
+1
View File
@@ -44,6 +44,7 @@ class Context:
self.ext_tasks: List[Task] = []
self.command_manager = None
self.running = True
# useless
self.reply_prefix = ""
+4 -2
View File
@@ -30,9 +30,11 @@ class AstrBotUpdator(RepoZipUpdator):
except psutil.NoSuchProcess:
pass
def _reboot(self, delay: int = None):
if delay: time.sleep(delay)
def _reboot(self, delay: int = None, context = None):
# if delay: time.sleep(delay)
py = sys.executable
context.running = False
time.sleep(3)
self.terminate_child_processes()
py = py.replace(" ", "\\ ")
try: