fix(login): 稳定登录失败事件幂等键

This commit is contained in:
yml
2026-11-19 16:03:13 +08:00
parent d611c0ed14
commit acd27bde67
+11 -3
View File
@@ -138,9 +138,15 @@ def _mark_attempt_failed(
失败重试会新建记录或新 attempt,因此是新的一条 —— 这是对的,那是新的事实。 失败重试会新建记录或新 attempt,因此是新的一条 —— 这是对的,那是新的事实。
""" """
completed_at = datetime.now(UTC) completed_at = datetime.now(UTC)
attempt_at = task.attempt_at or completed_at
attempt_utc = (
attempt_at.replace(tzinfo=UTC)
if attempt_at.tzinfo is None
else attempt_at.astimezone(UTC)
)
task.attempt_status = status if status in {"failed", "error"} else "failed" task.attempt_status = status if status in {"failed", "error"} else "failed"
task.attempt_message = message task.attempt_message = message
task.attempt_at = completed_at task.attempt_at = attempt_at
account = task.account account = task.account
if not (account and account.cookie): if not (account and account.cookie):
# 账号还没有任何 CK 需要保护:保持旧的失败语义,便于复用记录与页面统计。 # 账号还没有任何 CK 需要保护:保持旧的失败语义,便于复用记录与页面统计。
@@ -163,7 +169,7 @@ def _mark_attempt_failed(
account_id=account.id, account_id=account.id,
event_type=EVENT_LOGIN_FAILED, event_type=EVENT_LOGIN_FAILED,
title=f"登录失败:{message}", title=f"登录失败:{message}",
occurred_at=completed_at, occurred_at=attempt_at,
detail={ detail={
"message": message, "message": message,
"kind": kind, "kind": kind,
@@ -173,7 +179,9 @@ def _mark_attempt_failed(
source="login", source="login",
subject_type="login", subject_type="login",
subject_key=kind, subject_key=kind,
source_ref=f"login_task:{task.id}:failed:{int(completed_at.timestamp())}", source_ref=(
f"login_task:{task.id}:failed:{attempt_utc.strftime('%Y%m%dT%H%M%S')}"
),
) )