From b9dd42171461bc421378d3f294b6f2796c79e8dc Mon Sep 17 00:00:00 2001 From: kyx236 Date: Mon, 9 Feb 2026 18:02:50 +0800 Subject: [PATCH] feat: automatically clean team and tracker files after 'run all' or 'resume' tasks and notify administrators. --- telegram_bot.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/telegram_bot.py b/telegram_bot.py index b50db01..cdb25ab 100644 --- a/telegram_bot.py +++ b/telegram_bot.py @@ -1293,6 +1293,10 @@ class ProvisionerBot: failed_accounts = [r.get("email") for r in (result or []) if r.get("status") != "success"] log.info(f"任务完成: {team_name}, 成功: {len(success_accounts)}, 失败: {len(failed_accounts)}") await self.notifier.notify_task_completed(team_name, success_accounts, failed_accounts) + + # run_all / resume 完成后自动清理 team.json + if team_name in ("全部", "继续处理"): + await self._auto_clean_after_run_all() except Exception as e: log.error(f"任务异常: {team_name}, 错误: {e}") await self.notifier.notify_error(f"任务失败: {team_name}", str(e)) @@ -1304,6 +1308,44 @@ class ProvisionerBot: # 让标志保持 True,直到下次任务启动时再重置 # 这样可以确保线程池中的任务能够正确检测到停止信号 + async def _auto_clean_after_run_all(self): + """run_all 完成后自动清理 team.json 和 team_tracker.json""" + from config import reload_config as _reload + try: + cleaned = [] + + # 清理 team.json + if TEAM_JSON_FILE.exists(): + with open(TEAM_JSON_FILE, "w", encoding="utf-8") as f: + f.write("[]") + cleaned.append("team.json") + + # 清理 team_tracker.json + tracker_file = Path(TEAM_TRACKER_FILE) + if tracker_file.exists(): + with open(tracker_file, "w", encoding="utf-8") as f: + f.write('{"teams": {}}') + cleaned.append("team_tracker.json") + + # 重载配置 + _reload() + + if cleaned: + log.info(f"自动清理完成: {', '.join(cleaned)}") + for chat_id in TELEGRAM_ADMIN_CHAT_IDS: + try: + await self.app.bot.send_message( + chat_id, + f"🧹 自动清理完成\n\n" + f"已清空: {', '.join(cleaned)}\n\n" + f"现在可以导入新的 team.json 了", + parse_mode="HTML" + ) + except Exception: + pass + except Exception as e: + log.error(f"自动清理失败: {e}") + def _run_team_task(self, team_idx: int): """执行单个 Team 任务 (在线程池中运行)""" # 延迟导入避免循环依赖