重试失败任务时复用旧记录而非创建新记录

- 重试时查找该账号最近一条失败任务,更新其状态为 pending
- 避免同一账号在列表中出现多条重复记录
- 仅复用 failed/error 状态的任务,首次登录仍新建记录

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
yml2213
2026-06-22 19:37:18 +08:00
co-authored by Claude Fable 5
parent accb76b9da
commit 2f9b1a8eb2
+23 -7
View File
@@ -156,7 +156,7 @@ class LoginBatchRunner:
concurrency = self.concurrency
self._push_log("info", f"批量登录任务 {batch_id} 开始,共 {len(self.account_ids)} 个账号,并发数: {concurrency}")
# 创建任务记录(顺序执行,线程安全)
# 创建或复用任务记录(顺序执行,线程安全)
task_infos: list[dict] = [] # {task_id, acc_info}
for aid in self.account_ids:
acc = self.db.query(AccountModel).filter(AccountModel.id == aid).first()
@@ -168,13 +168,29 @@ class LoginBatchRunner:
self._push_log("warning", f"跳过无权账号: {acc.username}")
continue
task = LoginTask(
batch_id=batch_id,
account_id=aid,
status="pending",
created_by=self.created_by,
# 复用该账号最近一条失败任务记录,避免重复产生多条
existing_task = (
self.db.query(LoginTask)
.filter(LoginTask.account_id == aid, LoginTask.status.in_(["failed", "error"]))
.order_by(LoginTask.id.desc())
.first()
)
self.db.add(task)
if existing_task:
existing_task.batch_id = batch_id
existing_task.status = "pending"
existing_task.cookie = ""
existing_task.message = ""
existing_task.finished_at = None
task = existing_task
else:
task = LoginTask(
batch_id=batch_id,
account_id=aid,
status="pending",
created_by=self.created_by,
)
self.db.add(task)
self.db.flush() # 获取 task.id
task_infos.append({