🤖 自动格式化代码 [skip ci]

This commit is contained in:
github-actions[bot]
2025-06-14 17:28:38 +00:00
parent d0c07da977
commit 86be17405a
4 changed files with 19 additions and 22 deletions

15
bot.py
View File

@@ -111,29 +111,26 @@ async def graceful_shutdown():
# 获取所有剩余任务,排除当前任务 # 获取所有剩余任务,排除当前任务
remaining_tasks = [t for t in asyncio.all_tasks() if t is not asyncio.current_task()] remaining_tasks = [t for t in asyncio.all_tasks() if t is not asyncio.current_task()]
if remaining_tasks: if remaining_tasks:
logger.info(f"正在取消 {len(remaining_tasks)} 个剩余任务...") logger.info(f"正在取消 {len(remaining_tasks)} 个剩余任务...")
# 取消所有剩余任务 # 取消所有剩余任务
for task in remaining_tasks: for task in remaining_tasks:
if not task.done(): if not task.done():
task.cancel() task.cancel()
# 等待所有任务完成,设置超时 # 等待所有任务完成,设置超时
try: try:
await asyncio.wait_for( await asyncio.wait_for(asyncio.gather(*remaining_tasks, return_exceptions=True), timeout=15.0)
asyncio.gather(*remaining_tasks, return_exceptions=True),
timeout=15.0
)
logger.info("所有剩余任务已成功取消") logger.info("所有剩余任务已成功取消")
except asyncio.TimeoutError: except asyncio.TimeoutError:
logger.warning("等待任务取消超时,强制继续关闭") logger.warning("等待任务取消超时,强制继续关闭")
except Exception as e: except Exception as e:
logger.error(f"等待任务取消时发生异常: {e}") logger.error(f"等待任务取消时发生异常: {e}")
logger.info("麦麦优雅关闭完成") logger.info("麦麦优雅关闭完成")
except Exception as e: except Exception as e:
logger.error(f"麦麦关闭失败: {e}", exc_info=True) logger.error(f"麦麦关闭失败: {e}", exc_info=True)

View File

@@ -320,7 +320,7 @@ async def clear_temp_emoji() -> None:
logger.info("[清理] 完成") logger.info("[清理] 完成")
async def clean_unused_emojis(emoji_dir: str, emoji_objects: List["MaiEmoji"],removed_count:int) -> int: async def clean_unused_emojis(emoji_dir: str, emoji_objects: List["MaiEmoji"], removed_count: int) -> int:
"""清理指定目录中未被 emoji_objects 追踪的表情包文件""" """清理指定目录中未被 emoji_objects 追踪的表情包文件"""
if not os.path.exists(emoji_dir): if not os.path.exists(emoji_dir):
logger.warning(f"[清理] 目标目录不存在,跳过清理: {emoji_dir}") logger.warning(f"[清理] 目标目录不存在,跳过清理: {emoji_dir}")
@@ -352,7 +352,7 @@ async def clean_unused_emojis(emoji_dir: str, emoji_objects: List["MaiEmoji"],re
logger.info(f"[清理] 在目录 {emoji_dir} 中清理了 {cleaned_count} 个破损表情包。") logger.info(f"[清理] 在目录 {emoji_dir} 中清理了 {cleaned_count} 个破损表情包。")
else: else:
logger.info(f"[清理] 目录 {emoji_dir} 中没有需要清理的。") logger.info(f"[清理] 目录 {emoji_dir} 中没有需要清理的。")
return removed_count + cleaned_count return removed_count + cleaned_count
except Exception as e: except Exception as e:
@@ -566,7 +566,7 @@ class EmojiManager:
self.emoji_objects = [e for e in self.emoji_objects if e not in objects_to_remove] self.emoji_objects = [e for e in self.emoji_objects if e not in objects_to_remove]
# 清理 EMOJI_REGISTED_DIR 目录中未被追踪的文件 # 清理 EMOJI_REGISTED_DIR 目录中未被追踪的文件
removed_count = await clean_unused_emojis(EMOJI_REGISTED_DIR, self.emoji_objects,removed_count) removed_count = await clean_unused_emojis(EMOJI_REGISTED_DIR, self.emoji_objects, removed_count)
# 输出清理结果 # 输出清理结果
if removed_count > 0: if removed_count > 0:

View File

@@ -165,14 +165,14 @@ class NormalChat:
if self._chat_task is None or self._chat_task.cancelled(): if self._chat_task is None or self._chat_task.cancelled():
logger.info(f"[{self.stream_name}] 兴趣监控任务被取消或置空,退出") logger.info(f"[{self.stream_name}] 兴趣监控任务被取消或置空,退出")
break break
# 检查是否已停用 # 检查是否已停用
if self._disabled: if self._disabled:
logger.info(f"[{self.stream_name}] 已停用,退出兴趣监控") logger.info(f"[{self.stream_name}] 已停用,退出兴趣监控")
break break
await asyncio.sleep(0.5) # 每0.5秒检查一次 await asyncio.sleep(0.5) # 每0.5秒检查一次
# 再次检查取消状态 # 再次检查取消状态
if self._chat_task is None or self._chat_task.cancelled() or self._disabled: if self._chat_task is None or self._chat_task.cancelled() or self._disabled:
logger.info(f"[{self.stream_name}] 检测到停止信号,退出") logger.info(f"[{self.stream_name}] 检测到停止信号,退出")
@@ -233,7 +233,7 @@ class NormalChat:
limited_tasks = [limited_process(task, semaphore) for task in tasks] limited_tasks = [limited_process(task, semaphore) for task in tasks]
await asyncio.gather(*limited_tasks, return_exceptions=True) await asyncio.gather(*limited_tasks, return_exceptions=True)
except asyncio.CancelledError: except asyncio.CancelledError:
logger.info(f"[{self.stream_name}] 处理上下文时任务被取消") logger.info(f"[{self.stream_name}] 处理上下文时任务被取消")
break break

View File

@@ -92,7 +92,7 @@ class AsyncTaskManager:
logger.warning(f"已存在名称为 '{task.task_name}' 的任务,正在尝试取消并替换") logger.warning(f"已存在名称为 '{task.task_name}' 的任务,正在尝试取消并替换")
old_task = self.tasks[task.task_name] old_task = self.tasks[task.task_name]
old_task.cancel() # 取消已存在的任务 old_task.cancel() # 取消已存在的任务
# 添加超时保护,避免无限等待 # 添加超时保护,避免无限等待
try: try:
await asyncio.wait_for(old_task, timeout=5.0) await asyncio.wait_for(old_task, timeout=5.0)
@@ -102,7 +102,7 @@ class AsyncTaskManager:
logger.info(f"任务 '{task.task_name}' 已成功取消") logger.info(f"任务 '{task.task_name}' 已成功取消")
except Exception as e: except Exception as e:
logger.error(f"等待任务 '{task.task_name}' 完成时发生异常: {e}") logger.error(f"等待任务 '{task.task_name}' 完成时发生异常: {e}")
logger.info(f"成功结束任务 '{task.task_name}'") logger.info(f"成功结束任务 '{task.task_name}'")
# 创建新任务 # 创建新任务
@@ -134,10 +134,10 @@ class AsyncTaskManager:
async with self._lock: # 由于可能需要await等待任务完成所以需要加异步锁 async with self._lock: # 由于可能需要await等待任务完成所以需要加异步锁
# 设置中止标志 # 设置中止标志
self.abort_flag.set() self.abort_flag.set()
# 首先收集所有任务的引用,避免在迭代过程中字典被修改 # 首先收集所有任务的引用,避免在迭代过程中字典被修改
task_items = list(self.tasks.items()) task_items = list(self.tasks.items())
# 取消所有任务 # 取消所有任务
for name, inst in task_items: for name, inst in task_items:
if not inst.done(): if not inst.done():
@@ -172,7 +172,7 @@ class AsyncTaskManager:
logger.info("=== 异步任务状态调试信息 ===") logger.info("=== 异步任务状态调试信息 ===")
logger.info(f"当前管理的任务数量: {len(self.tasks)}") logger.info(f"当前管理的任务数量: {len(self.tasks)}")
logger.info(f"中止标志状态: {self.abort_flag.is_set()}") logger.info(f"中止标志状态: {self.abort_flag.is_set()}")
for task_name, task in self.tasks.items(): for task_name, task in self.tasks.items():
status = [] status = []
if task.done(): if task.done():
@@ -185,9 +185,9 @@ class AsyncTaskManager:
status.append("正常完成") status.append("正常完成")
else: else:
status.append("运行中") status.append("运行中")
logger.info(f"任务 '{task_name}': {', '.join(status)}") logger.info(f"任务 '{task_name}': {', '.join(status)}")
# 检查所有asyncio任务 # 检查所有asyncio任务
all_tasks = asyncio.all_tasks() all_tasks = asyncio.all_tasks()
logger.info(f"当前事件循环中的所有任务数量: {len(all_tasks)}") logger.info(f"当前事件循环中的所有任务数量: {len(all_tasks)}")