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:
1 parent
323928626b
commit
cef3d5b26a
15 files changed
+489
-150
No files matched your search
@@ -0,0 +1,108 @@
|
||||
"""
|
||||
tests/test_shipping_process_gate_retry_cap.py
|
||||
|
||||
用途:验证门禁技术失败跨 force_new Job 累计重试上限,并在耗尽后收口 Attempt/Gate。
|
||||
依赖:Flask testing app、run_shipping_process_gate handler、process_gate_service。
|
||||
逻辑:mock claim_and_run 持续抛错;断言最多 TECHNICAL_RETRIES 次失败后
|
||||
Attempt/Gate 进入 failed_terminal,且不再入队新的重试 Job。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from app import db
|
||||
from app.jobs.handlers.shipping_process_gate import run_shipping_process_gate
|
||||
from app.jobs.types import JobType
|
||||
from app.models import Job, ShippingProcessGate, ShippingProcessGateAttempt
|
||||
from app.services.shipping_notice.process_gate_job_service import (
|
||||
SHIPPING_PROCESS_GATE_JOB_DOMAIN,
|
||||
enqueue_gate_attempt_job,
|
||||
)
|
||||
from app.services.shipping_notice.process_gate_service import (
|
||||
ensure_execution_for_workflow_version,
|
||||
queue_gate_attempt,
|
||||
)
|
||||
from tests.test_shipping_process_gate_service import _seed_workflow_version
|
||||
|
||||
|
||||
def test_technical_retries_cap_across_force_new_jobs(app):
|
||||
"""跨 Job 累计失败达到上限后必须 failed_terminal,且不再新建 queued Job。"""
|
||||
with app.app_context():
|
||||
app.config['SHIPPING_PROCESS_GATE_TECHNICAL_RETRIES'] = 3
|
||||
_user, item, version = _seed_workflow_version(suffix='retrycap')
|
||||
execution = ensure_execution_for_workflow_version(
|
||||
workflow_item_id=int(item.id),
|
||||
workflow_item_version_id=int(version.id),
|
||||
)
|
||||
attempt = queue_gate_attempt(
|
||||
execution_no=execution.execution_no,
|
||||
gate_code='layout_parsing',
|
||||
trigger_type='system_requeue',
|
||||
actor_type='system',
|
||||
actor_id='retry-cap',
|
||||
idempotency_key='retry-cap-1',
|
||||
input_snapshot={'action_code': 'retry_layout_parsing', 'payload': {}},
|
||||
)
|
||||
first_job_id = enqueue_gate_attempt_job(attempt_id=int(attempt.id))
|
||||
db.session.commit()
|
||||
attempt_id = int(attempt.id)
|
||||
|
||||
with patch(
|
||||
'app.jobs.handlers.shipping_process_gate.claim_and_run_gate_attempt',
|
||||
side_effect=RuntimeError('boom https://erp.example.com/x?token=abc'),
|
||||
):
|
||||
for round_idx in range(5):
|
||||
attempt_row = db.session.get(ShippingProcessGateAttempt, attempt_id)
|
||||
assert attempt_row is not None
|
||||
if attempt_row.status in {'failed', 'succeeded', 'cancelled'}:
|
||||
break
|
||||
job_id = int(attempt_row.job_id or 0)
|
||||
if job_id <= 0:
|
||||
job_id = enqueue_gate_attempt_job(attempt_id=attempt_id, force_new=True)
|
||||
db.session.commit()
|
||||
job = db.session.get(Job, job_id)
|
||||
assert job is not None
|
||||
job.status = 'running'
|
||||
job.attempts = 1
|
||||
attempt_row.status = 'running'
|
||||
gate = db.session.get(ShippingProcessGate, int(attempt_row.gate_id))
|
||||
if gate is not None:
|
||||
gate.status = 'running'
|
||||
db.session.commit()
|
||||
|
||||
ctx = MagicMock()
|
||||
ctx.extras = {'worker_label': f'retry-cap-worker-{round_idx}'}
|
||||
run_shipping_process_gate(ctx, {'attempt_id': attempt_id, 'job_id': job_id})
|
||||
|
||||
attempt_final = db.session.get(ShippingProcessGateAttempt, attempt_id)
|
||||
gate_final = db.session.get(ShippingProcessGate, int(attempt_final.gate_id))
|
||||
assert attempt_final is not None
|
||||
assert attempt_final.status == 'failed'
|
||||
assert gate_final is not None
|
||||
assert gate_final.status == 'failed_terminal'
|
||||
assert attempt_final.error_code == 'TECHNICAL_RETRY_EXHAUSTED'
|
||||
# 敏感 URL 不得原样落库
|
||||
assert 'erp.example.com' not in str(attempt_final.error_summary or '')
|
||||
|
||||
failed_jobs = (
|
||||
Job.query
|
||||
.filter_by(
|
||||
domain=SHIPPING_PROCESS_GATE_JOB_DOMAIN,
|
||||
job_type=JobType.SHIPPING_PROCESS_GATE.value,
|
||||
status='failed',
|
||||
)
|
||||
.count()
|
||||
)
|
||||
assert failed_jobs == 3
|
||||
queued_jobs = (
|
||||
Job.query
|
||||
.filter_by(
|
||||
domain=SHIPPING_PROCESS_GATE_JOB_DOMAIN,
|
||||
job_type=JobType.SHIPPING_PROCESS_GATE.value,
|
||||
status='queued',
|
||||
)
|
||||
.count()
|
||||
)
|
||||
assert queued_jobs == 0
|
||||
assert int(first_job_id) > 0
|
||||
Reference in new issue
Block a user