130 lines
4.8 KiB
Python
130 lines
4.8 KiB
Python
"""
|
||
backend/worker_shipping.py
|
||
|
||
用途:发货通知 worker 部署兼容入口(Compose / Supervisor 沿用此路径)。
|
||
依赖:multiprocessing、worker.main、Flask Config.SHIPPING_NOTICE_WORKER_CONCURRENCY。
|
||
逻辑:默认 --queues shipping;按 SHIPPING_NOTICE_WORKER_CONCURRENCY 启动进程池,
|
||
数据库 SKIP LOCKED 保证同批版面 Job 并发领取安全;
|
||
每个子进程独立挂接 Shipping Recovery Sweeper(recovery_callback),
|
||
靠 FOR UPDATE SKIP LOCKED + pg_advisory_xact_lock 做天然互斥。
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import multiprocessing as mp
|
||
import sys
|
||
from typing import Callable
|
||
|
||
from app import create_app
|
||
from app.services.shipping_notice.xfa_ocr_adapter import is_ocr_engine_available
|
||
from worker import _configure_logging, _resolve_config_name, main
|
||
|
||
|
||
def configure_shipping_worker_process_executable() -> None:
|
||
"""固定 Windows spawn 子进程解释器,防止可选 OCR 依赖跨虚拟环境加载。"""
|
||
mp.set_executable(sys.executable)
|
||
|
||
|
||
def assert_shipping_worker_capabilities() -> None:
|
||
"""
|
||
在 shipping Worker 消费队列前验证 XFA 自动标记的强制 OCR 运行时。
|
||
|
||
XFA 自动标记需以 OCR 对 PDFium 页面、AcroForm Widget 和 datasets 进行视觉
|
||
交叉校验;不可把缺少依赖的问题延迟到已领取的 Job。失败即退出,供 Supervisor
|
||
或本地启动脚本明确暴露错误并阻止错误解释器参与同一 `shipping` 队列。
|
||
|
||
Raises:
|
||
RuntimeError: RapidOCR/OpenCV/ONNX Runtime 任一缺失。
|
||
"""
|
||
if not is_ocr_engine_available():
|
||
raise RuntimeError(
|
||
'XFA OCR dependency is unavailable in the shipping Worker interpreter; '
|
||
'install backend/requirements-ocr.txt in this virtual environment.'
|
||
)
|
||
|
||
|
||
def _worker_loop(worker_index: int) -> None:
|
||
"""启动一个 shipping 队列子进程,并使用独立日志标签区分并发执行。"""
|
||
from app.services.shipping_notice.shipping_recovery_sweeper_service import shipping_recovery_tick
|
||
from flask import current_app
|
||
|
||
log = _configure_logging(f'shipping-{worker_index}')
|
||
log.info('shipping-worker process started index=%s', worker_index)
|
||
|
||
config_name = _resolve_config_name()
|
||
child_app = create_app(config_name)
|
||
with child_app.app_context():
|
||
recovery_interval = float(getattr(
|
||
current_app.config,
|
||
'SHIPPING_NOTICE_WORKER_RECOVERY_TICK_SECONDS',
|
||
60,
|
||
) or 60)
|
||
recovery_cb: Callable[[], int] = shipping_recovery_tick
|
||
main(
|
||
queues=['shipping'],
|
||
config_name=config_name,
|
||
recovery_callback=recovery_cb,
|
||
recovery_interval_seconds=recovery_interval,
|
||
)
|
||
|
||
|
||
def main_shipping() -> None:
|
||
"""
|
||
启动 shipping worker;SHIPPING_NOTICE_WORKER_CONCURRENCY>1 时 fork 多进程。
|
||
|
||
核心逻辑:默认两个进程并行领取 Job;单进程配置时直接进入主循环,便于本地调试。
|
||
Returns:
|
||
None。主进程持续等待子进程;收到 KeyboardInterrupt 时终止并回收子进程。
|
||
"""
|
||
from app.services.shipping_notice.shipping_recovery_sweeper_service import shipping_recovery_tick
|
||
from flask import current_app
|
||
|
||
configure_shipping_worker_process_executable()
|
||
assert_shipping_worker_capabilities()
|
||
config_name = _resolve_config_name()
|
||
bootstrap_app = create_app(config_name)
|
||
concurrency = max(int(bootstrap_app.config.get('SHIPPING_NOTICE_WORKER_CONCURRENCY', 2) or 2), 1)
|
||
|
||
if concurrency == 1:
|
||
with bootstrap_app.app_context():
|
||
recovery_interval = float(getattr(
|
||
current_app.config,
|
||
'SHIPPING_NOTICE_WORKER_RECOVERY_TICK_SECONDS',
|
||
60,
|
||
) or 60)
|
||
recovery_cb: Callable[[], int] = shipping_recovery_tick
|
||
main(
|
||
queues=['shipping'],
|
||
config_name=config_name,
|
||
recovery_callback=recovery_cb,
|
||
recovery_interval_seconds=recovery_interval,
|
||
)
|
||
return
|
||
|
||
log = _configure_logging('shipping-master')
|
||
log.info('starting %s shipping worker processes', concurrency)
|
||
workers: list[mp.Process] = []
|
||
for index in range(concurrency):
|
||
proc = mp.Process(
|
||
target=_worker_loop,
|
||
args=(index + 1,),
|
||
name=f'shipping-worker-{index + 1}',
|
||
)
|
||
proc.start()
|
||
workers.append(proc)
|
||
|
||
try:
|
||
for proc in workers:
|
||
proc.join()
|
||
except KeyboardInterrupt:
|
||
log.info('stopping shipping worker pool')
|
||
for proc in workers:
|
||
if proc.is_alive():
|
||
proc.terminate()
|
||
for proc in workers:
|
||
proc.join(timeout=5)
|
||
|
||
|
||
if __name__ == '__main__':
|
||
mp.freeze_support()
|
||
main_shipping()
|