优化(登录任务): 移除批量实时日志,增加账号独立日志
- 登录任务移除底部实时日志面板和 WebSocket 订阅 - 登录执行器按任务收集日志并在结束时持久化 - 新增登录任务日志接口和单账号日志弹窗 - 迁移 0134 增加 login_tasks.logs - 修复无 WebSocket 时开始按钮状态未复位
This commit is contained in:
@@ -368,6 +368,8 @@ class LoginBatchRunner:
|
||||
self._stop = threading.Event()
|
||||
self._counter_lock = threading.Lock()
|
||||
self._completed = 0
|
||||
# 每个登录线程独立收集当前账号日志;批次级日志不写入账号任务。
|
||||
self._log_context = threading.local()
|
||||
|
||||
# 共享代理获取器(无池,每次取新代理)
|
||||
self._proxy_runtime = build_proxy_runtime(
|
||||
@@ -432,6 +434,20 @@ class LoginBatchRunner:
|
||||
worker_db.close()
|
||||
|
||||
def _push_log(self, level: str, message: str):
|
||||
if message:
|
||||
task_id = int(getattr(self._log_context, "task_id", 0) or 0)
|
||||
if task_id:
|
||||
logs = getattr(self._log_context, "logs", None)
|
||||
if logs is None:
|
||||
logs = []
|
||||
self._log_context.logs = logs
|
||||
logs.append(
|
||||
{
|
||||
"level": level,
|
||||
"message": message,
|
||||
"at": datetime.now(UTC).isoformat(),
|
||||
}
|
||||
)
|
||||
# 即使没有页面实时日志,也要保留批次进度到 app.log,便于排查卡点。
|
||||
if message:
|
||||
log_level = (
|
||||
@@ -475,10 +491,13 @@ class LoginBatchRunner:
|
||||
return
|
||||
|
||||
worker_db = WorkerSessionLocal()
|
||||
task = None
|
||||
try:
|
||||
task = worker_db.query(LoginTask).filter(LoginTask.id == task_id).first()
|
||||
if not task:
|
||||
return
|
||||
self._log_context.task_id = int(task.id)
|
||||
self._log_context.logs = []
|
||||
# CK、健康与重登状态都挂在账号上;登录记录只是一次尝试的历史。
|
||||
account = task.account
|
||||
|
||||
@@ -684,6 +703,16 @@ class LoginBatchRunner:
|
||||
worker_db.commit()
|
||||
|
||||
finally:
|
||||
logs = list(getattr(self._log_context, "logs", []) or [])
|
||||
if task is not None and logs:
|
||||
try:
|
||||
task.logs = logs[-300:]
|
||||
worker_db.commit()
|
||||
except Exception: # noqa: BLE001 - 日志落库失败不能掩盖登录结果
|
||||
worker_db.rollback()
|
||||
logger.exception("保存登录账号日志失败: task_id={}", task_id)
|
||||
self._log_context.task_id = 0
|
||||
self._log_context.logs = []
|
||||
worker_db.close()
|
||||
|
||||
def run(self):
|
||||
|
||||
Reference in New Issue
Block a user