fix(shipping-process-gates): 收口技术重试并补齐运维 UI

跨 Job 累计技术失败次数,耗尽后将 Attempt/Gate 置为 failed_terminal;
收缩旧 action_buttons;healthcheck 增加 Worker 领取探针;恢复卡展示责任方与审计,Admin 诊断改主从布局。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
hugoandCursor committed 2026-08-01 13:54:14 +08:00
1 parent 323928626b
commit cef3d5b26a
15 files changed
+489 -150

No files matched your search

@@ -2,10 +2,12 @@
backend/app/jobs/handlers/shipping_process_gate.py
用途:消费门禁恢复 Attempt Job,领取租约并执行 process_gate_executor。
依赖:JobContext、process_gate_job_service、jobs.queue finish/fail。
逻辑:由 shipping worker 领取;技术失败按 5s/30s/120s 退避重新入队;
waiting_external 先结束当前 Job 再延迟入队新 Job(禁止复用仍 running 的 Job);
成功进入 waiting_external 时调度下一次 poll Attempt。
依赖:JobContext、process_gate_job_service、process_gate_service、jobs.queue finish/fail、
process_gate_error_sanitizer。
逻辑:由 shipping worker 领取;技术失败按 Attempt 累计次数 + 5s/30s/120s 退避重新入队;
次数耗尽时 fail_job 并 complete_gate_attempt(failed_terminal);
waiting_external 先结束当前 Job 再延迟入队新 Job;
healthcheck_probe 载荷用于部署探针,不依赖真实 Attempt。
"""
from __future__ import annotations
@@ -18,25 +20,85 @@ from app.jobs.queue import fail_job, finish_job
from app.jobs.registry import register_handler
from app.jobs.types import JobType
from app.models import Job, ShippingProcessGate, ShippingProcessGateAttempt
from app.services.shipping_notice.process_gate_error_sanitizer import sanitize_gate_error_text
from app.services.shipping_notice.process_gate_job_service import (
claim_and_run_gate_attempt,
enqueue_gate_attempt_job,
schedule_waiting_external_poll,
)
from app.services.shipping_notice.process_gate_service import complete_gate_attempt
# 方案规定的技术重试退避(秒):第 1/2/3 次失败后延迟。
_TECHNICAL_RETRY_BACKOFF_SECONDS: tuple[int, ...] = (5, 30, 120)
_TECH_RETRY_KEY = 'technical_retry_count'
def _technical_retry_count(attempt: ShippingProcessGateAttempt) -> int:
"""读取 Attempt 上累计的技术失败次数(跨 force_new Job 保留)。"""
snap = attempt.input_snapshot_json if isinstance(attempt.input_snapshot_json, dict) else {}
try:
return max(int(snap.get(_TECH_RETRY_KEY) or 0), 0)
except (TypeError, ValueError):
return 0
def _bump_technical_retry_count(attempt: ShippingProcessGateAttempt) -> int:
"""递增并回写技术失败次数,返回递增后的值。"""
snap = dict(attempt.input_snapshot_json) if isinstance(attempt.input_snapshot_json, dict) else {}
next_count = _technical_retry_count(attempt) + 1
snap[_TECH_RETRY_KEY] = next_count
attempt.input_snapshot_json = snap
return next_count
def _finalize_attempt_after_retry_exhausted(
*,
attempt: ShippingProcessGateAttempt,
exc: BaseException,
) -> None:
"""
技术重试耗尽:将 Attempt/Gate 收口为 failed_terminal。
核心逻辑:调用 complete_gate_attempt,避免 Job=failed 而 Gate 仍 running。
"""
safe = sanitize_gate_error_text(exc, max_len=500) or 'technical_retry_exhausted'
if attempt.status == 'queued':
attempt.status = 'running'
db.session.flush()
complete_gate_attempt(
attempt_id=int(attempt.id),
outcome='failed_terminal',
blocking_reason_code='technical_failure',
blocking_reason_json={'message': safe, 'technical_retry_exhausted': True},
effective_result_json={'findings': [], 'error': 'technical_retry_exhausted'},
output_snapshot={'error': safe, 'technical_retry_exhausted': True},
error_code='TECHNICAL_RETRY_EXHAUSTED',
error_summary=safe,
)
@register_handler(JobType.SHIPPING_PROCESS_GATE)
def run_shipping_process_gate(ctx: JobContext, payload: dict) -> None:
"""
执行门禁 Attempt。
执行门禁 Attempt;支持部署健康检查探针载荷。
Args:
ctx: Worker 上下文。
payload: 须含 attempt_id 与 job_id。
payload: 须含 attempt_id 与 job_id;healthcheck_probe=True 时仅证明领取能力。
"""
# [STEP] 部署探针:Worker 领取后立即成功结束,不触碰业务 Attempt。
if bool(payload.get('healthcheck_probe')):
raw_job_id = payload.get('job_id')
job = db.session.get(Job, int(raw_job_id)) if raw_job_id else None
if job is None:
raise RuntimeError('missing_job_id_for_healthcheck_probe')
finish_job(
job.id,
result={'status': 'healthcheck_probe_ok'},
owned_attempt=int(job.attempts or 0),
)
return
attempt_id = int(payload.get('attempt_id') or 0)
raw_job_id = payload.get('job_id')
job = db.session.get(Job, int(raw_job_id)) if raw_job_id else None
@@ -70,17 +132,23 @@ def run_shipping_process_gate(ctx: JobContext, payload: dict) -> None:
db.session.commit()
return
# [STEP] 技术失败:先 fail 当前 Job,再按退避入队新 Job。
max_attempts = int(job.max_attempts or 3)
if attempt is not None and owned_attempt < max_attempts:
delay_idx = min(max(owned_attempt - 1, 0), len(_TECHNICAL_RETRY_BACKOFF_SECONDS) - 1)
# [STEP] 技术失败:按 Attempt 累计次数判定,禁止因 force_new Job.attempts=0 无限重试。
max_attempts = max(
int(current_app.config.get('SHIPPING_PROCESS_GATE_TECHNICAL_RETRIES', 3) or 3),
1,
)
retries_done = _technical_retry_count(attempt) if attempt is not None else max_attempts
safe_error = sanitize_gate_error_text(exc, max_len=1000) or 'technical_failure'
if attempt is not None and retries_done + 1 < max_attempts:
delay_idx = min(retries_done, len(_TECHNICAL_RETRY_BACKOFF_SECONDS) - 1)
delay = _TECHNICAL_RETRY_BACKOFF_SECONDS[delay_idx]
fail_job(
job.id,
error=str(exc)[:1000],
error=safe_error,
retryable=False,
owned_attempt=owned_attempt,
)
_bump_technical_retry_count(attempt)
attempt.status = 'queued'
attempt.job_id = None
attempt.worker_id = None
@@ -97,7 +165,10 @@ def run_shipping_process_gate(ctx: JobContext, payload: dict) -> None:
db.session.commit()
return
fail_job(job.id, error=str(exc)[:1000], retryable=False, owned_attempt=owned_attempt)
fail_job(job.id, error=safe_error, retryable=False, owned_attempt=owned_attempt)
if attempt is not None:
_finalize_attempt_after_retry_exhausted(attempt=attempt, exc=exc)
db.session.commit()
return
finish_job(job.id, result=result, owned_attempt=owned_attempt)