fix(shipping-notice): 同事务完成订单抽取续抽交接
避免 running 仍占唯一索引时 INSERT 后继 Job 触发 UniqueViolation,导致批次卡在接收解析。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
44c3bd0e80
commit
b4c793145e
4 files changed
+179
-32
No files matched your search
@@ -2,11 +2,16 @@
|
||||
订单抽取 Worker 单元测试。
|
||||
|
||||
覆盖:物化顺序、partial waiting_layout、layout_ready_continue 自交接、
|
||||
runtime_budget_continue、layout 调度失败收口、以及 continue_from_running 去重。
|
||||
runtime_budget_continue、layout 调度失败收口、continue_from_running 去重、
|
||||
以及同事务 finish→enqueue 避免 UniqueViolation。
|
||||
"""
|
||||
|
||||
from datetime import datetime
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
from app import db
|
||||
from app.core.errors import ServiceUnavailableError, ValidationError
|
||||
from app.jobs.handlers.shipping_order_extraction import (
|
||||
_expansion_trigger_reason_codes,
|
||||
@@ -354,6 +359,7 @@ def test_worker_runtime_budget_reenqueues_instead_of_partially_completed(monkeyp
|
||||
def fake_finish(*_args, **kwargs):
|
||||
events.append('finished')
|
||||
finished.update(kwargs)
|
||||
return True
|
||||
|
||||
monkeypatch.setattr(handler, 'finish_job', fake_finish)
|
||||
monkeypatch.setattr(handler.db.session, 'commit', lambda: None)
|
||||
@@ -374,7 +380,12 @@ def test_worker_runtime_budget_reenqueues_instead_of_partially_completed(monkeyp
|
||||
assert finished['result']['budget_stopped'] is True
|
||||
assert finished['result']['waiting_continue'] is True
|
||||
assert finished['result']['handoff_status'] == 'runtime_budget_continue'
|
||||
assert finished['result']['continue_job_id'] == 999
|
||||
assert pending_unit.status == 'pending'
|
||||
# 方案 A:须先 finish(commit=False) 再 enqueue,避免 running 占唯一槽
|
||||
assert events.index('finished') < next(
|
||||
i for i, item in enumerate(events) if item.startswith('enqueue:')
|
||||
)
|
||||
|
||||
|
||||
def test_worker_layout_ready_continue_when_stale_snapshot_skipped_pending_unit(monkeypatch):
|
||||
@@ -477,6 +488,7 @@ def test_worker_layout_ready_continue_when_stale_snapshot_skipped_pending_unit(m
|
||||
def fake_finish(*_args, **kwargs):
|
||||
events.append('finished')
|
||||
finished.update(kwargs)
|
||||
return True
|
||||
|
||||
monkeypatch.setattr(handler, 'finish_job', fake_finish)
|
||||
monkeypatch.setattr(handler.db.session, 'commit', lambda: None)
|
||||
@@ -502,6 +514,7 @@ def test_worker_layout_ready_continue_when_stale_snapshot_skipped_pending_unit(m
|
||||
assert finished['result']['waiting_layout'] is False
|
||||
assert finished['result']['continue_job_id'] == 1006
|
||||
assert stale_pending_unit.status == 'pending'
|
||||
assert events.index('finished') < events.index('enqueue:continue=True')
|
||||
|
||||
|
||||
def test_worker_layout_ready_continue_keeps_waiting_layout_when_siblings_still_pending(monkeypatch):
|
||||
@@ -595,7 +608,10 @@ def test_worker_layout_ready_continue_keeps_waiting_layout_when_siblings_still_p
|
||||
handler, '_enqueue_ready_order_extraction_job',
|
||||
lambda _run, *, message, continue_from_running=False: 1007,
|
||||
)
|
||||
monkeypatch.setattr(handler, 'finish_job', lambda *_a, **kwargs: finished.update(kwargs))
|
||||
monkeypatch.setattr(
|
||||
handler, 'finish_job',
|
||||
lambda *_a, **kwargs: finished.update(kwargs) or True,
|
||||
)
|
||||
monkeypatch.setattr(handler.db.session, 'commit', lambda: None)
|
||||
monkeypatch.setattr(handler.db.session, 'refresh', lambda *_a, **_k: None)
|
||||
monkeypatch.setattr(handler.db.session, 'expire_all', lambda: None)
|
||||
@@ -638,6 +654,75 @@ def test_enqueue_ready_order_extraction_job_continue_from_running_dedupes_queued
|
||||
assert captured.get('business_ref') == 'SNER-DEDUP'
|
||||
|
||||
|
||||
def test_order_extraction_handoff_finish_then_enqueue_same_txn(app):
|
||||
"""
|
||||
方案 A:同事务先 finish(commit=False) 再 enqueue,不得撞
|
||||
uq_jobs_shipping_order_extraction_active_ref。
|
||||
|
||||
断言:running 时直接 enqueue 失败;释放槽后同事务 INSERT 成功,旧 Job success。
|
||||
"""
|
||||
with app.app_context():
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
|
||||
from app.jobs.queue import enqueue, finish_job
|
||||
from app.jobs.types import JobType
|
||||
from app.models import Job
|
||||
|
||||
run_no = 'SNER-HANDOFF-ATOMIC'
|
||||
running = Job(
|
||||
domain='shipping_notice',
|
||||
job_type=JobType.SHIPPING_ORDER_EXTRACTION.value,
|
||||
status='running',
|
||||
business_ref=run_no,
|
||||
payload_json={'run_no': run_no},
|
||||
attempts=1,
|
||||
max_attempts=3,
|
||||
started_at=datetime.utcnow(),
|
||||
)
|
||||
db.session.add(running)
|
||||
db.session.commit()
|
||||
running_id = int(running.id)
|
||||
|
||||
# 对照:仍 running 时直接 INSERT 必须撞唯一索引
|
||||
with pytest.raises(IntegrityError):
|
||||
enqueue(
|
||||
JobType.SHIPPING_ORDER_EXTRACTION,
|
||||
{'run_no': run_no},
|
||||
domain='shipping_notice',
|
||||
business_ref=run_no,
|
||||
dedupe_statuses=('queued',),
|
||||
dedupe_active=True,
|
||||
commit=True,
|
||||
)
|
||||
db.session.rollback()
|
||||
|
||||
assert finish_job(
|
||||
running_id,
|
||||
result={'handoff_status': 'runtime_budget_continue'},
|
||||
message='handoff',
|
||||
owned_attempt=1,
|
||||
commit=False,
|
||||
) is True
|
||||
continue_id = enqueue(
|
||||
JobType.SHIPPING_ORDER_EXTRACTION,
|
||||
{'run_no': run_no},
|
||||
domain='shipping_notice',
|
||||
business_ref=run_no,
|
||||
dedupe_statuses=('queued',),
|
||||
dedupe_active=True,
|
||||
commit=False,
|
||||
)
|
||||
db.session.commit()
|
||||
db.session.expire_all()
|
||||
|
||||
old = db.session.get(Job, running_id)
|
||||
nxt = db.session.get(Job, int(continue_id))
|
||||
assert old is not None and old.status == 'success'
|
||||
assert nxt is not None and nxt.status == 'queued'
|
||||
assert nxt.id != running_id
|
||||
assert nxt.business_ref == run_no
|
||||
|
||||
|
||||
def test_worker_marks_run_failed_when_layout_scheduling_fails(monkeypatch):
|
||||
"""版面 Job 创建失败(如 MinerU 关闭)时必须同步收口 Run,禁止仅失败 Job 而留下 queued。"""
|
||||
import app.jobs.handlers.shipping_order_extraction as handler
|
||||
|
||||
Reference in new issue
Block a user