feat(shipping-notice): 完善发货通知自动推进与多库存源复核闭环

This commit is contained in:
hugo committed 2026-08-09 11:43:53 +08:00
1 parent cfa19a0b7b
commit 8e0bbf7a81
180 files changed
+31210 -1864

No files matched your search

+50 -3
View File
@@ -7,7 +7,7 @@ backend/app/jobs/runner.py
from __future__ import annotations
import time
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Callable
from app.jobs import store
from app.jobs.registry import get_handler
@@ -24,21 +24,49 @@ def _resolve_poll_seconds(ctx: JobContext) -> float:
return float(ctx.config.get('KB_WORKER_POLL_SECONDS', 2))
def run_forever(ctx: JobContext, *, poll_interval_seconds: float | None = None) -> None:
def run_forever(
ctx: JobContext,
*,
poll_interval_seconds: float | None = None,
recovery_callback: Callable[[], int] | None = None,
recovery_interval_seconds: float = 60.0,
) -> None:
"""
持续轮询并执行任务。
核心逻辑:claim_next → get_handler → handler(ctx, payload);异常时 mark_failed。
支持 recovery_callback:启动立即执行一次,空闲睡眠和 claim 后检查 tick。
Args:
ctx: Worker 上下文(extras.queues 指定消费队列)。
poll_interval_seconds: 无任务时的休眠秒数;默认按队列读 config。
recovery_callback: 恢复回调函数,返回本轮处理计数;默认 None 关闭。
recovery_interval_seconds: 两次 recovery tick 的最小间隔秒数;默认 60。
"""
queues = ctx.extras.get('queues') or ['kb']
poll = poll_interval_seconds if poll_interval_seconds is not None else _resolve_poll_seconds(ctx)
ctx.logger.info('Job runner started queues=%s poll_seconds=%s', queues, poll)
ctx.logger.info('Job runner started queues=%s poll_seconds=%s recovery=%s',
queues, poll, recovery_callback is not None)
if recovery_callback is not None:
try:
recovery_callback()
except Exception:
ctx.logger.exception('Recovery callback failed on BOOT')
finally:
last_recovery_ts = time.monotonic()
else:
last_recovery_ts = time.monotonic()
while True:
job = store.claim_next(queues=queues)
if not job:
current_ts = time.monotonic()
if recovery_callback is not None and (current_ts - last_recovery_ts) >= recovery_interval_seconds:
try:
recovery_callback()
except Exception:
ctx.logger.exception('Recovery callback failed during idle poll')
finally:
last_recovery_ts = current_ts
time.sleep(poll)
continue
@@ -52,6 +80,15 @@ def run_forever(ctx: JobContext, *, poll_interval_seconds: float | None = None)
message=f'no handler for {job.get("job_type")}',
owned_attempt=owned_attempt_int,
)
if recovery_callback is not None:
current_ts = time.monotonic()
if (current_ts - last_recovery_ts) >= recovery_interval_seconds:
try:
recovery_callback()
except Exception:
ctx.logger.exception('Recovery callback failed after no-handler job')
finally:
last_recovery_ts = current_ts
continue
try:
@@ -63,3 +100,13 @@ def run_forever(ctx: JobContext, *, poll_interval_seconds: float | None = None)
message=str(exc),
owned_attempt=owned_attempt_int,
)
finally:
if recovery_callback is not None:
current_ts = time.monotonic()
if (current_ts - last_recovery_ts) >= recovery_interval_seconds:
try:
recovery_callback()
except Exception:
ctx.logger.exception('Recovery callback failed after job execution')
finally:
last_recovery_ts = current_ts