78 lines
2.9 KiB
Python
78 lines
2.9 KiB
Python
"""
|
|
tests/test_shipping_process_gate_requeue.py
|
|
|
|
用途:验证门禁 Job 技术重试 / 外部等待轮询不会复用仍 running 的旧 Job。
|
|
依赖:process_gate_job_service.enqueue_gate_attempt_job、jobs.queue、Flask testing app。
|
|
逻辑:先创建 running Job 绑定 Attempt,再 force_new 入队,必须得到不同 job_id 且新 Job 为 queued。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
|
|
from app import db
|
|
from app.jobs.types import JobType
|
|
from app.models import Job
|
|
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_force_new_enqueue_does_not_reuse_running_job(app):
|
|
"""
|
|
force_new=True 时即使存在同 attempt 的 running Job,也必须新建可领取 queued Job。
|
|
"""
|
|
with app.app_context():
|
|
_user, review_case_id, task_id = _seed_workflow_version(suffix='requeue1')
|
|
execution = ensure_execution_for_workflow_version(
|
|
review_case_id=review_case_id,
|
|
task_id=task_id,
|
|
)
|
|
attempt = queue_gate_attempt(
|
|
execution_no=execution.execution_no,
|
|
gate_code='inventory_preparation',
|
|
trigger_type='user_retry',
|
|
actor_type='system',
|
|
actor_id='requeue-worker',
|
|
idempotency_key='requeue-force-new-1',
|
|
input_snapshot={'action_code': 'poll_inventory_preparation', 'payload': {}},
|
|
)
|
|
# [STEP] 模拟旧 Job 仍为 running(重试前未结束)。
|
|
running = Job(
|
|
domain=SHIPPING_PROCESS_GATE_JOB_DOMAIN,
|
|
job_type=JobType.SHIPPING_PROCESS_GATE.value,
|
|
status='running',
|
|
business_ref=f'attempt:{int(attempt.id)}',
|
|
payload_json={'attempt_id': int(attempt.id)},
|
|
progress=10,
|
|
message='running',
|
|
attempts=1,
|
|
max_attempts=3,
|
|
created_at=datetime.utcnow(),
|
|
updated_at=datetime.utcnow(),
|
|
)
|
|
db.session.add(running)
|
|
db.session.flush()
|
|
attempt.job_id = int(running.id)
|
|
db.session.flush()
|
|
|
|
# 若无 force_new,默认去重可能复用 running;此处必须新建。
|
|
new_job_id = enqueue_gate_attempt_job(
|
|
attempt_id=int(attempt.id),
|
|
delay_seconds=5,
|
|
force_new=True,
|
|
)
|
|
assert int(new_job_id) != int(running.id)
|
|
new_job = db.session.get(Job, int(new_job_id))
|
|
assert new_job is not None
|
|
assert new_job.status == 'queued'
|
|
assert str(new_job.business_ref or '').startswith(f'attempt:{int(attempt.id)}:r')
|
|
db.session.refresh(attempt)
|
|
assert int(attempt.job_id) == int(new_job_id)
|