feat(equipment-acceptance): 预警扫描发送改异步并错开合同付款排程

手动扫描/发送入队由 schedule worker 执行,避免 Nginx 504;默认扫描 06:30、周五发送 18:30。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
sophieandCursor committed 2026-10-30 15:09:16 +08:00
1 parent 2e8405e2f0
commit 537e3e18f0
16 files changed
+334 -73

No files matched your search

+1
View File
@@ -7,6 +7,7 @@ backend/app/jobs/handlers/__init__.py
from app.jobs.handlers import ( # noqa: F401
contract_payment,
equipment_acceptance,
file_check_run,
in_transit_transfer_item,
in_transit_transfer_match,
@@ -0,0 +1,55 @@
"""
backend/app/jobs/handlers/equipment_acceptance.py
用途:执行设备验收预警域后台任务(手动扫描、手动发送)。
依赖:jobs.registry、equipment_acceptance.alert_service、jobs.queue.finish_job。
逻辑:与周五排程共用 generate_alerts / send_pending_alerts。成功须显式 finish_job,
否则 Job 停在 running,超时回收后会重复发送。
"""
from __future__ import annotations
from app.jobs.context import JobContext
from app.jobs.queue import finish_job
from app.jobs.registry import register_handler
from app.jobs.types import JobType
from app.services.equipment_acceptance.alert_service import generate_alerts, send_pending_alerts
@register_handler(JobType.EQUIPMENT_ACCEPTANCE_ALERT_SCAN)
def run_equipment_acceptance_alert_scan(ctx: JobContext, payload: dict) -> None:
"""
执行一次设备验收同步并生成待发送记录。
核心逻辑:不发企业微信。供管理员「扫描预警」入队后由 Worker 执行。
Args:
ctx: 后台任务上下文。
payload: 含通用队列注入的 job_id。
Returns:
None;结果写入 Job.result_json。
"""
job_id = int(payload.get("job_id") or 0)
result = generate_alerts()
ctx.logger.info("equipment acceptance alert scan result=%s", result)
if job_id:
finish_job(job_id, result=result, message="设备验收预警扫描完成")
@register_handler(JobType.EQUIPMENT_ACCEPTANCE_ALERT_WEEKLY)
def run_equipment_acceptance_alert_weekly(ctx: JobContext, payload: dict) -> None:
"""
执行一次设备验收待发送记录的企业微信汇总发送。
核心逻辑:按收件人发卡片和 Excel,发完后才改发送记录状态。
供管理员「发送预警」入队;与周五排程共用同一发送函数。
Args:
ctx: 后台任务上下文。
payload: 含通用队列注入的 job_id。
Returns:
None;sent/failed 写入 Job.result_json,页面轮询后展示。
"""
job_id = int(payload.get("job_id") or 0)
result = send_pending_alerts()
ctx.logger.info("equipment acceptance weekly alert result=%s", result)
if job_id:
finish_job(job_id, result=result, message="设备验收预警发送完成")
+27 -1
View File
@@ -109,6 +109,23 @@ def claim_next(*, queues: list[str] | None = None) -> dict[str, Any] | None:
timeout_seconds=float(current_app.config.get("CONTRACT_PAYMENT_JOB_TIMEOUT_SECONDS", 1800)),
job_type=job_type,
)
equipment_acceptance_types = (
JobType.EQUIPMENT_ACCEPTANCE_ALERT_SCAN,
JobType.EQUIPMENT_ACCEPTANCE_ALERT_WEEKLY,
)
if {item.value for item in equipment_acceptance_types} & allowed_types:
from app.jobs.queue import recover_stale_jobs
from flask import current_app
# 设备验收发送可能要给很多收件人发企微,超时与合同付款周汇总同级。
for job_type in equipment_acceptance_types:
if job_type.value not in allowed_types:
continue
recover_stale_jobs(
domain="equipment_acceptance",
timeout_seconds=float(current_app.config.get("CONTRACT_PAYMENT_JOB_TIMEOUT_SECONDS", 1800)),
job_type=job_type,
)
# [STEP 2] 按队列优先级 claim
for name in queue_names:
@@ -149,13 +166,22 @@ def claim_next(*, queues: list[str] | None = None) -> dict[str, Any] | None:
)
if job:
return _normalize_generic_job(job)
# file-check / 企微通讯录同步与排程共用 schedule worker,但 domain 不同
# file-check / 企微通讯录同步 / 设备验收手动扫描发送与排程共用 schedule worker
job = claim_next_job(
domain=None,
job_types=[JobType.FILE_CHECK_RUN, JobType.WECOM_DIRECTORY_SYNC],
)
if job:
return _normalize_generic_job(job)
job = claim_next_job(
domain="equipment_acceptance",
job_types=[
JobType.EQUIPMENT_ACCEPTANCE_ALERT_SCAN,
JobType.EQUIPMENT_ACCEPTANCE_ALERT_WEEKLY,
],
)
if job:
return _normalize_generic_job(job)
elif name == 'transfer':
# 在途调拨域:预检/票级执行/行级任务共用 domain="in_transit_transfer"
# (与 execution_service 入队口径一致);缺此分支时 transfer worker
+10 -1
View File
@@ -42,6 +42,9 @@ class JobType(StrEnum):
CONTRACT_PAYMENT_SYNC = 'contract_payment_sync'
CONTRACT_PAYMENT_ALERT = 'contract_payment_alert'
CONTRACT_PAYMENT_ALERT_WEEKLY = 'contract_payment_alert_weekly'
# 设备验收预警:管理员手动扫描 / 发送入队,由 schedule worker 执行,避免 HTTP 504。
EQUIPMENT_ACCEPTANCE_ALERT_SCAN = 'equipment_acceptance_alert_scan'
EQUIPMENT_ACCEPTANCE_ALERT_WEEKLY = 'equipment_acceptance_alert_weekly'
# 在途仓自动调拨:文件准备 + 只读匹配(Phase 1);执行 Run/Item 在 Phase 2 注册。
TRANSFER_MATCH = 'in_transit_transfer_match'
TRANSFER_RUN = 'in_transit_transfer_run'
@@ -84,7 +87,13 @@ QUEUE_KB_JOB_TYPES: dict[str, frozenset[str]] = {
'shipping_post_ot_wave_progress', 'shipping_review_revalidate_progress',
}),
'contract': frozenset({'procurement_contract_review', 'contract_payment_sync', 'contract_payment_alert', 'contract_payment_alert_weekly'}),
'schedule': frozenset({'schedule_run', 'file_check_run', 'wecom_directory_sync'}),
'schedule': frozenset({
'schedule_run',
'file_check_run',
'wecom_directory_sync',
'equipment_acceptance_alert_scan',
'equipment_acceptance_alert_weekly',
}),
'transfer': frozenset({
'in_transit_transfer_match', 'in_transit_transfer_run', 'in_transit_transfer_item',
}),