feat(freight-rpa): 批次R解耦编排层并切换 onhand/inbound 契约

将 RPA 编排收编至 freight_rpa,回调迁 /api/freight-rpa/*。
共享目录与 Worker 契约一次切换至 onhand/inbound/upload,
并补 outbound 骨架与 tt16 排程 job key 迁移。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
hugoandCursor committed 2026-10-17 23:18:48 +08:00
1 parent bf3154033b
commit ffc0d4a430
147 files changed
+4221 -2678

No files matched your search

@@ -9,7 +9,7 @@ def test_receipt_schedule_one_unit_per_source_including_empty_query_params(app,
"""一源一单元:有/无 Warehouse 条件均产出 unit;多值 Warehouse 不拆分。"""
from app import db
from app.models import ScheduleRun, ScheduleTask, ShippingInventorySource, ShippingReceiptReportConfig, ShippingRpaProfile
from app.services.schedule.freight_receipt_rpa_job import run_freight_receipt_rpa_schedule
from app.services.schedule.freight_inbound_rpa_job import run_freight_receipt_rpa_schedule
with app.app_context():
dbs = ShippingInventorySource.query.filter_by(source_code='DSV360_AWSDU').one()
@@ -53,7 +53,7 @@ def test_receipt_schedule_one_unit_per_source_including_empty_query_params(app,
])
task = ScheduleTask(
schedule_no='SCH-RECEIPT-1', name='Receipt', schedule_type='platform_job',
platform_api_path='freight-receipt-rpa', owner_user_id=1, cron_expr='0 8 * * *',
platform_api_path='freight-rpa-inbound', owner_user_id=1, cron_expr='0 8 * * *',
job_params={'source_codes': [dbs.source_code, crane.source_code, empty_dbs.source_code]},
)
db.session.add(task); db.session.flush()
@@ -62,7 +62,7 @@ def test_receipt_schedule_one_unit_per_source_including_empty_query_params(app,
captured = {}
monkeypatch.setattr(
'app.services.schedule.freight_receipt_rpa_job.write_receipt_export_batch_trigger',
'app.services.schedule.freight_inbound_rpa_job.write_inbound_export_batch_trigger',
lambda run_no, units, payload: captured.update(run_no=run_no, units=units, payload=payload) or 'receipt-trigger.json',
)
total, success, failed, skipped, details, errors, logs = run_freight_receipt_rpa_schedule(task, run)
@@ -71,7 +71,7 @@ def test_receipt_schedule_one_unit_per_source_including_empty_query_params(app,
assert (total, success, failed, skipped, errors) == (3, 0, 0, 0, [])
assert {item['source_code'] for item in details} == {dbs.source_code, crane.source_code, empty_dbs.source_code}
assert all(item['warehouse_id'] is None for item in details)
assert captured['payload']['schema_version'] == 'shipping_receipt_rpa.v1'
assert captured['payload']['schema_version'] == 'freight_rpa.inbound.v1'
assert len(captured['units']) == 3
dbs_unit = next(item for item in captured['units'] if item['source_code'] == dbs.source_code)
assert dbs_unit['rpa_profile_code'] == 'DBS_DUB_TEST'
@@ -95,8 +95,8 @@ def test_receipt_schedule_fails_fast_when_worker_offline(app, tmp_path, monkeypa
from app import db
from app.core.errors import ServiceUnavailableError
from app.models import ScheduleRun, ScheduleTask, ShippingInventorySource, ShippingReceiptReportConfig, ShippingReceiptRpaUnitRun
from app.services.schedule.freight_receipt_rpa_job import run_freight_receipt_rpa_schedule
from app.services.shipping_notice.rpa_worker_heartbeat import RPA_WORKER_OFFLINE_MESSAGE
from app.services.schedule.freight_inbound_rpa_job import run_freight_receipt_rpa_schedule
from app.services.freight_rpa.common.worker_heartbeat import RPA_WORKER_OFFLINE_MESSAGE
with app.app_context():
source = ShippingInventorySource.query.filter_by(source_code='DSV360_AWSDU').one()
@@ -110,7 +110,7 @@ def test_receipt_schedule_fails_fast_when_worker_offline(app, tmp_path, monkeypa
))
task = ScheduleTask(
schedule_no='SCH-RECEIPT-OFFLINE', name='Receipt offline', schedule_type='platform_job',
platform_api_path='freight-receipt-rpa', owner_user_id=1, cron_expr='0 8 * * *',
platform_api_path='freight-rpa-inbound', owner_user_id=1, cron_expr='0 8 * * *',
job_params={'source_codes': [source.source_code]},
)
db.session.add(task)
@@ -121,7 +121,7 @@ def test_receipt_schedule_fails_fast_when_worker_offline(app, tmp_path, monkeypa
trigger_calls = []
monkeypatch.setattr(
'app.services.schedule.freight_receipt_rpa_job.assert_receipt_rpa_worker_ready',
'app.services.schedule.freight_inbound_rpa_job.assert_receipt_rpa_worker_ready',
lambda: (_ for _ in ()).throw(ServiceUnavailableError(
RPA_WORKER_OFFLINE_MESSAGE,
code='RPA.WORKER_OFFLINE',
@@ -129,7 +129,7 @@ def test_receipt_schedule_fails_fast_when_worker_offline(app, tmp_path, monkeypa
)),
)
monkeypatch.setattr(
'app.services.schedule.freight_receipt_rpa_job.write_receipt_export_batch_trigger',
'app.services.schedule.freight_inbound_rpa_job.write_inbound_export_batch_trigger',
lambda *args, **kwargs: trigger_calls.append(args) or 'should-not-write.json',
)
@@ -173,12 +173,12 @@ def test_stale_receipt_callback_run_is_failed_without_automatic_retry(app):
"""无回调超时只关闭本次 run/单元并写审计,绝不创建重试单元。"""
from app import db
from app.models import ScheduleRun, ScheduleTask, ShippingAuditEvent, ShippingInventorySource, ShippingReceiptRpaUnitRun
from app.services.schedule.freight_receipt_rpa_finalize import recover_stale_receipt_rpa_schedule_runs
from app.services.schedule.freight_inbound_rpa_finalize import recover_stale_receipt_rpa_schedule_runs
with app.app_context():
app.config['FREIGHT_RPA_RECEIPT_TIMEOUT_SECONDS'] = 60
app.config['FREIGHT_RPA_INBOUND_TIMEOUT_SECONDS'] = 60
source = ShippingInventorySource.query.filter_by(source_code='DSV360_AWSDU').one()
task = ScheduleTask(schedule_no='SCH-RECEIPT-TIMEOUT', name='Receipt timeout', schedule_type='platform_job', platform_api_path='freight-receipt-rpa', owner_user_id=1, cron_expr='0 8 * * *')
task = ScheduleTask(schedule_no='SCH-RECEIPT-TIMEOUT', name='Receipt timeout', schedule_type='platform_job', platform_api_path='freight-rpa-inbound', owner_user_id=1, cron_expr='0 8 * * *')
db.session.add(task); db.session.flush()
run = ScheduleRun(run_no='RUN-RECEIPT-TIMEOUT', schedule_task_id=task.id, status='running', started_at=datetime.utcnow() - timedelta(minutes=2), response_summary_json={'awaiting_receipt_rpa': True})
db.session.add(run); db.session.flush()
@@ -197,13 +197,13 @@ def test_stale_receipt_callback_run_is_failed_without_automatic_retry(app):
def test_receipt_trigger_is_published_atomically(app, tmp_path):
"""Worker 只能观察到完整 JSON;临时写入文件不会匹配其 *.json 扫描。"""
from app.services.shipping_notice.receipt_rpa import write_receipt_export_batch_trigger
from app.services.freight_rpa.inbound.trigger_io import write_inbound_export_batch_trigger
with app.app_context():
app.config['FREIGHT_RPA_SHARED_RECEIPT_TRIGGER_DIR'] = str(tmp_path)
path = write_receipt_export_batch_trigger('RUN-ATOMIC', [{'callback_id': 'c1'}], {})
app.config['FREIGHT_RPA_SHARED_INBOUND_TRIGGER_DIR'] = str(tmp_path)
path = write_inbound_export_batch_trigger('RUN-ATOMIC', [{'callback_id': 'c1'}], {})
assert path
assert (tmp_path / 'receipt_export_batch_RUN-ATOMIC.json').exists()
assert (tmp_path / 'inbound_export_batch_RUN-ATOMIC.json').exists()
assert not list(tmp_path.glob('.*.tmp'))
@@ -211,12 +211,12 @@ def test_stale_receipt_full_scan_recovers_without_task_trigger_or_restart(app):
"""全量扫描(每轮 tick 路径)无需任务再次执行/进程重启即可收口超时收料 run。"""
from app import db
from app.models import ScheduleRun, ScheduleTask, ShippingInventorySource, ShippingReceiptRpaUnitRun
from app.services.schedule.freight_receipt_rpa_finalize import recover_all_stale_receipt_rpa_schedule_runs
from app.services.schedule.freight_inbound_rpa_finalize import recover_all_stale_receipt_rpa_schedule_runs
with app.app_context():
app.config['FREIGHT_RPA_RECEIPT_TIMEOUT_SECONDS'] = 60
app.config['FREIGHT_RPA_INBOUND_TIMEOUT_SECONDS'] = 60
source = ShippingInventorySource.query.filter_by(source_code='DSV360_AWSDU').one()
task = ScheduleTask(schedule_no='SCH-RECEIPT-ORPHAN', name='Receipt orphan', schedule_type='platform_job', platform_api_path='freight-receipt-rpa', owner_user_id=1, cron_expr='0 8 * * *')
task = ScheduleTask(schedule_no='SCH-RECEIPT-ORPHAN', name='Receipt orphan', schedule_type='platform_job', platform_api_path='freight-rpa-inbound', owner_user_id=1, cron_expr='0 8 * * *')
db.session.add(task); db.session.flush()
run = ScheduleRun(run_no='RUN-RECEIPT-ORPHAN', schedule_task_id=task.id, status='running', started_at=datetime.utcnow() - timedelta(minutes=5), response_summary_json={'awaiting_receipt_rpa': True})
db.session.add(run); db.session.flush()
@@ -235,12 +235,12 @@ def test_receipt_recovery_respects_fresh_activity_of_long_batch(app):
"""长批次健康在跑不得被误杀:超时锚取最后单元活动而非 run 开始时间。"""
from app import db
from app.models import ScheduleRun, ScheduleTask, ShippingInventorySource, ShippingReceiptRpaUnitRun
from app.services.schedule.freight_receipt_rpa_finalize import recover_all_stale_receipt_rpa_schedule_runs
from app.services.schedule.freight_inbound_rpa_finalize import recover_all_stale_receipt_rpa_schedule_runs
with app.app_context():
app.config['FREIGHT_RPA_RECEIPT_TIMEOUT_SECONDS'] = 60
app.config['FREIGHT_RPA_INBOUND_TIMEOUT_SECONDS'] = 60
source = ShippingInventorySource.query.filter_by(source_code='DSV360_AWSDU').one()
task = ScheduleTask(schedule_no='SCH-RECEIPT-LONG', name='Receipt long batch', schedule_type='platform_job', platform_api_path='freight-receipt-rpa', owner_user_id=1, cron_expr='0 8 * * *')
task = ScheduleTask(schedule_no='SCH-RECEIPT-LONG', name='Receipt long batch', schedule_type='platform_job', platform_api_path='freight-rpa-inbound', owner_user_id=1, cron_expr='0 8 * * *')
db.session.add(task); db.session.flush()
run = ScheduleRun(run_no='RUN-RECEIPT-LONG', schedule_task_id=task.id, status='running', started_at=datetime.utcnow() - timedelta(minutes=30), response_summary_json={'awaiting_receipt_rpa': True})
db.session.add(run); db.session.flush()
@@ -274,10 +274,10 @@ def test_stale_orphan_inbound_units_are_failed_by_full_scan(app):
"""按需触发(无排程)的入站单元超时由全量扫描收口为失败,健康在跑单元不受影响。"""
from app import db
from app.models import ShippingAuditEvent, ShippingInventorySource, ShippingReceiptRpaUnitRun
from app.services.schedule.freight_receipt_rpa_finalize import recover_all_stale_orphan_receipt_units
from app.services.schedule.freight_inbound_rpa_finalize import recover_all_stale_orphan_receipt_units
with app.app_context():
app.config['FREIGHT_RPA_RECEIPT_TIMEOUT_SECONDS'] = 60
app.config['FREIGHT_RPA_INBOUND_TIMEOUT_SECONDS'] = 60
source = ShippingInventorySource.query.filter_by(source_code='DSV360_AWSDU').one()
# 单元1:排队 5 分钟从未被领取(created_at 为锚,queued 分支);单元2:领取后
# 静默 5 分钟(started_at 为锚);单元3:30 秒前刚领取(健康在跑,不得误杀)。