103 lines
3.6 KiB
Python
103 lines
3.6 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
|
||
from typing import Callable
|
||
|
||
from app import create_app
|
||
from worker import _configure_logging, _resolve_config_name, main
|
||
|
||
|
||
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
|
||
|
||
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()
|