feat(equipment-acceptance): 预警扫描发送改异步并错开合同付款排程
手动扫描/发送入队由 schedule worker 执行,避免 Nginx 504;默认扫描 06:30、周五发送 18:30。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
2e8405e2f0
commit
537e3e18f0
16 files changed
+334
-73
No files matched your search
@@ -2,8 +2,9 @@
|
||||
backend/app/api/equipment_acceptance/__init__.py
|
||||
|
||||
用途:设备验收预警的 Chat 作业面接口。
|
||||
依赖:JWT 当前用户、Agent 授权、sync_service、统一成功响应。
|
||||
逻辑:持有 equipment_acceptance Agent 的用户可看停留列表、发送记录、已归档 Excel 和配置;同步、扫描、发送和保存配置仅平台管理员。
|
||||
依赖:JWT 当前用户、Agent 授权、sync_service、统一成功响应、jobs.queue。
|
||||
逻辑:持有 equipment_acceptance Agent 的用户可看停留列表、发送记录、已归档 Excel 和配置;
|
||||
同步、扫描、发送和保存配置仅平台管理员。扫描与发送改为入队,由 schedule worker 执行,避免 HTTP 504。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -12,15 +13,17 @@ from io import BytesIO
|
||||
|
||||
from flask import Blueprint, g, request, send_file
|
||||
|
||||
from app import db
|
||||
from app.core.auth.jwt import get_current_user
|
||||
from app.core.errors import ForbiddenError, UnauthorizedError
|
||||
from app.core.errors import ForbiddenError, NotFoundError, UnauthorizedError
|
||||
from app.core.response import ok
|
||||
from app.models import Agent
|
||||
from app.jobs.queue import enqueue
|
||||
from app.jobs.types import JobType
|
||||
from app.models import Agent, Job
|
||||
from app.services.equipment_acceptance.config_service import (
|
||||
get_acceptance_config,
|
||||
save_acceptance_config,
|
||||
)
|
||||
from app.services.equipment_acceptance.alert_service import generate_alerts, send_pending_alerts
|
||||
from app.services.equipment_acceptance.send_record_service import get_send_file, list_send_files, list_send_records
|
||||
from app.services.equipment_acceptance.sync_service import (
|
||||
get_acceptance_order,
|
||||
@@ -28,6 +31,11 @@ from app.services.equipment_acceptance.sync_service import (
|
||||
sync_acceptance_snapshots,
|
||||
)
|
||||
|
||||
_EQUIPMENT_ACCEPTANCE_JOB_TYPES = {
|
||||
JobType.EQUIPMENT_ACCEPTANCE_ALERT_SCAN.value,
|
||||
JobType.EQUIPMENT_ACCEPTANCE_ALERT_WEEKLY.value,
|
||||
}
|
||||
|
||||
equipment_acceptance_bp = Blueprint("equipment_acceptance", __name__)
|
||||
EQUIPMENT_ACCEPTANCE_AGENT_CODE = "equipment_acceptance"
|
||||
|
||||
@@ -149,16 +157,79 @@ def save_equipment_acceptance_config():
|
||||
|
||||
@equipment_acceptance_bp.route("/alerts/scan", methods=["POST"])
|
||||
def scan_equipment_acceptance_alerts():
|
||||
"""管理员立即同步 E10 并生成待发送记录。不发企业微信。"""
|
||||
_require_equipment_acceptance_admin()
|
||||
return ok(generate_alerts(), request_id=getattr(g, "request_id", None))
|
||||
"""
|
||||
将设备验收扫描任务入队。
|
||||
|
||||
核心逻辑:仅平台管理员。Worker 同步后生成待发送,不发企业微信。
|
||||
同业务键已有 queued/running 时复用,避免连点重复扫。
|
||||
Returns:
|
||||
HTTP 202 与 job_id。
|
||||
"""
|
||||
user = _require_equipment_acceptance_admin()
|
||||
job_id = enqueue(
|
||||
JobType.EQUIPMENT_ACCEPTANCE_ALERT_SCAN,
|
||||
{},
|
||||
domain="equipment_acceptance",
|
||||
business_ref="daily-alert",
|
||||
created_by_id=user.id,
|
||||
message="等待扫描设备验收预警",
|
||||
)
|
||||
return ok({"job_id": job_id}, request_id=getattr(g, "request_id", None), status=202)
|
||||
|
||||
|
||||
@equipment_acceptance_bp.route("/alerts/send", methods=["POST"])
|
||||
def send_equipment_acceptance_alerts():
|
||||
"""管理员立即把待发送记录按收件人发到企业微信。"""
|
||||
_require_equipment_acceptance_admin()
|
||||
return ok(send_pending_alerts(), request_id=getattr(g, "request_id", None))
|
||||
"""
|
||||
将设备验收发送任务入队。
|
||||
|
||||
核心逻辑:仅平台管理员。Worker 按收件人发企微后改发送记录状态。
|
||||
同业务键已有 queued/running 时复用,避免连点重复发。
|
||||
Returns:
|
||||
HTTP 202 与 job_id。
|
||||
"""
|
||||
user = _require_equipment_acceptance_admin()
|
||||
job_id = enqueue(
|
||||
JobType.EQUIPMENT_ACCEPTANCE_ALERT_WEEKLY,
|
||||
{},
|
||||
domain="equipment_acceptance",
|
||||
business_ref="weekly-alert",
|
||||
created_by_id=user.id,
|
||||
message="等待发送设备验收预警",
|
||||
)
|
||||
return ok({"job_id": job_id}, request_id=getattr(g, "request_id", None), status=202)
|
||||
|
||||
|
||||
@equipment_acceptance_bp.route("/jobs/<int:job_id>", methods=["GET"])
|
||||
def get_equipment_acceptance_job(job_id: int):
|
||||
"""
|
||||
查询当前管理员提交的设备验收后台任务是否跑完。
|
||||
|
||||
核心逻辑:只返回本人创建的设备验收 Job。发送任务的 result 含 sent/failed。
|
||||
Args:
|
||||
job_id: jobs 表主键。
|
||||
Returns:
|
||||
status、message、result。
|
||||
Raises:
|
||||
NotFoundError: 任务不存在或不属于当前管理员。
|
||||
"""
|
||||
user = _require_equipment_acceptance_admin()
|
||||
row = db.session.get(Job, job_id)
|
||||
if (
|
||||
row is None
|
||||
or row.domain != "equipment_acceptance"
|
||||
or row.job_type not in _EQUIPMENT_ACCEPTANCE_JOB_TYPES
|
||||
or row.created_by_id != user.id
|
||||
):
|
||||
raise NotFoundError("任务不存在")
|
||||
return ok(
|
||||
{
|
||||
"id": row.id,
|
||||
"status": row.status,
|
||||
"message": row.message,
|
||||
"result": row.result_json or {},
|
||||
},
|
||||
request_id=getattr(g, "request_id", None),
|
||||
)
|
||||
|
||||
|
||||
@equipment_acceptance_bp.route("/send-files", methods=["GET"])
|
||||
|
||||
@@ -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="设备验收预警发送完成")
|
||||
@@ -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
|
||||
|
||||
@@ -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',
|
||||
}),
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"""
|
||||
backend/app/services/equipment_acceptance/schedule_seed.py
|
||||
|
||||
用途:设备验收预警的默认排程:每日扫描和周五 18:00 发送。
|
||||
用途:设备验收预警的默认排程:每日 06:30 扫描和周五 18:30 发送。
|
||||
依赖:ScheduleTask、permission_roles.service.first_schedule_owner_user、timezone_util。
|
||||
逻辑:应用启动时幂等创建。已有同编号或同任务键时不覆盖管理员改过的 Cron。
|
||||
"""
|
||||
@@ -17,8 +17,10 @@ from app.services.schedule.timezone_util import compute_next_run_at
|
||||
|
||||
|
||||
_DEFAULTS = (
|
||||
("SCH-EQUIPMENT-ACCEPTANCE-SCAN", "equipment-acceptance-alert-scan", "设备验收预警扫描(日批)", "0 6 * * *"),
|
||||
("SCH-EQUIPMENT-ACCEPTANCE-WEEKLY", "equipment-acceptance-alert-weekly", "设备验收预警发送(周五 18:00)", "0 18 * * 5"),
|
||||
# 每天 06:30,错开合同付款预警扫描的 06:00
|
||||
("SCH-EQUIPMENT-ACCEPTANCE-SCAN", "equipment-acceptance-alert-scan", "设备验收预警扫描(日批 06:30)", "30 6 * * *"),
|
||||
# 周五 18:30,错开合同付款预警的周五 18:00
|
||||
("SCH-EQUIPMENT-ACCEPTANCE-WEEKLY", "equipment-acceptance-alert-weekly", "设备验收预警发送(周五 18:30)", "30 18 * * 5"),
|
||||
)
|
||||
|
||||
|
||||
@@ -26,7 +28,7 @@ def ensure_default_equipment_acceptance_schedules(*, commit: bool = True):
|
||||
"""
|
||||
幂等创建设备验收每日扫描和周五发送两个排程。
|
||||
|
||||
核心逻辑:发送时刻不在预警配置表。默认与合同付款一样,周五 18:00,时区 Asia/Shanghai。
|
||||
核心逻辑:发送时刻不在预警配置表。默认每天 06:30、周五 18:30(错开合同付款 06:00 / 18:00),时区 Asia/Shanghai。
|
||||
Args:
|
||||
commit: 为 False 时只 flush,交给外层事务提交。
|
||||
Returns:
|
||||
|
||||
Reference in new issue
Block a user