优化(登录任务): 移除批量实时日志,增加账号独立日志
- 登录任务移除底部实时日志面板和 WebSocket 订阅 - 登录执行器按任务收集日志并在结束时持久化 - 新增登录任务日志接口和单账号日志弹窗 - 迁移 0134 增加 login_tasks.logs - 修复无 WebSocket 时开始按钮状态未复位
This commit is contained in:
@@ -533,6 +533,10 @@
|
||||
"20261125_0133_binding_failed_hold.py": {
|
||||
"revision": "20261125_0133",
|
||||
"sha256": "d282e42776fd9501b5293c2a116ad1467df0ddb1847d950b477086e3ae5440a7"
|
||||
},
|
||||
"20261125_0134_login_task_logs.py": {
|
||||
"revision": "20261125_0134",
|
||||
"sha256": "c21a4833874676b08a9156e20cfb89c2786aece628d998e8a1bf87a8d0b3cd4b"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
"""保存每个登录任务的逐账号日志。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision: str = "20261125_0134"
|
||||
down_revision: str | None = "20261125_0133"
|
||||
branch_labels: str | None = None
|
||||
depends_on: str | None = None
|
||||
|
||||
TABLE = "login_tasks"
|
||||
|
||||
|
||||
def _column_exists(name: str) -> bool:
|
||||
conn = op.get_bind()
|
||||
count = conn.execute(
|
||||
sa.text(
|
||||
"SELECT COUNT(*) FROM information_schema.columns "
|
||||
"WHERE table_schema = DATABASE() AND table_name = :table "
|
||||
"AND column_name = :name"
|
||||
),
|
||||
{"table": TABLE, "name": name},
|
||||
).scalar()
|
||||
return bool(count)
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
if not _column_exists("logs"):
|
||||
op.add_column(TABLE, sa.Column("logs", sa.JSON(), nullable=True))
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
if _column_exists("logs"):
|
||||
op.drop_column(TABLE, "logs")
|
||||
@@ -397,6 +397,8 @@ class LoginTask(Base):
|
||||
relogin_failure_kind: Mapped[str] = mapped_column(
|
||||
String(16), default="", server_default=""
|
||||
)
|
||||
# 本次登录尝试的逐账号日志;由 LoginBatchRunner 在任务结束时写入。
|
||||
logs: Mapped[list | None] = mapped_column(JSON, nullable=True)
|
||||
|
||||
account: Mapped[Account] = relationship("Account", back_populates="login_tasks")
|
||||
|
||||
|
||||
@@ -397,6 +397,27 @@ def list_tasks(
|
||||
}
|
||||
|
||||
|
||||
@router.get("/tasks/{task_id}/logs")
|
||||
def get_task_logs(
|
||||
task_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(get_current_user),
|
||||
):
|
||||
"""读取单个账号本次登录尝试的日志。"""
|
||||
task = (
|
||||
_visible_tasks_query(db, current, None)
|
||||
.filter(LoginTask.id == task_id)
|
||||
.first()
|
||||
)
|
||||
if task is None:
|
||||
raise HTTPException(status_code=404, detail="登录任务不存在")
|
||||
return {
|
||||
"task_id": int(task.id),
|
||||
"account_id": int(task.account_id),
|
||||
"items": task.logs or [],
|
||||
}
|
||||
|
||||
|
||||
@router.get("/tasks/filters")
|
||||
def task_filter_options(
|
||||
db: Session = Depends(get_db),
|
||||
|
||||
@@ -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