refactor(scheduler): 排程迁移出web进程,迁入scheduler worker

This commit is contained in:
sophie committed 2026-07-30 19:52:41 +08:00
1 parent 970ee06537
commit 74745a9d10
21 files changed
+652 -223

No files matched your search

+18 -4
View File
@@ -2,17 +2,31 @@
backend/app/jobs/handlers/schedule_run.py
用途:排程任务执行 handler。
逻辑:Wave 4 从 scheduler_service in-process 执行体迁入。
依赖:schedule.queue_service、jobs registry/context。
逻辑:独立 schedule worker 领取 Job 后,委托 queue_service 执行并同步双层状态。
"""
from __future__ import annotations
from app.jobs.context import JobContext
from app.jobs.registry import register_handler
from app.jobs.types import JobType
from app.services.schedule.queue_service import execute_schedule_job
@register_handler(JobType.SCHEDULE_RUN)
def run_schedule(ctx: JobContext, payload: dict) -> None:
"""执行单条 ScheduleTask。"""
ctx.logger.info('schedule_run handler skeleton invoked payload=%s', payload)
raise NotImplementedError('schedule_run handler pending Wave 4')
"""
执行单条已入队排程。
Args:
ctx: Worker 上下文,用于日志与运行配置。
payload: 必须包含 job_id 与 schedule_run_id。
Raises:
RuntimeError: payload 缺失或执行失败时抛出,由通用 Job 层负责重试。
"""
job_id = int(payload.get("job_id") or 0)
run_id = int(payload.get("schedule_run_id") or 0)
if not job_id or not run_id:
raise RuntimeError("job_id and schedule_run_id are required")
execute_schedule_job(job_id=job_id, run_id=run_id, logger=ctx.logger)
+2
View File
@@ -19,6 +19,8 @@ if TYPE_CHECKING:
def _resolve_poll_seconds(ctx: JobContext) -> float:
"""按队列配置读取轮询间隔。"""
queues = ctx.extras.get('queues') or ['kb']
if 'schedule' in queues and len(queues) == 1:
return float(ctx.config.get('SCHEDULE_WORKER_POLL_SECONDS', 30))
if 'shipping' in queues and 'kb' not in queues:
return float(ctx.config.get('SHIPPING_NOTICE_WORKER_POLL_SECONDS', 2))
return float(ctx.config.get('KB_WORKER_POLL_SECONDS', 2))
+13 -2
View File
@@ -1,9 +1,9 @@
"""
backend/app/jobs/store.py
用途:Job 认领与状态读写(Wave 2:KbJob 双轨 adapter,无新表)。
用途:Job 认领与状态读写(KbJob 双轨 adapter + 通用 Job)。
依赖:KbJob、各域 queue_service 的 claim/recover 逻辑。
逻辑:runner 通过 claim_next 获取 kb / shipping / contract 待执行任务;返回归一化 dict 供 handler 分发。
逻辑:runner 通过 claim_next 获取 kb / shipping / contract / schedule 任务;返回归一化 dict 供 handler 分发。
"""
from __future__ import annotations
@@ -114,6 +114,17 @@ def claim_next(*, queues: list[str] | None = None) -> dict[str, Any] | None:
)
if job:
return _normalize_generic_job(job)
elif name == 'schedule':
from app.jobs.queue import claim_next_job
from app.services.schedule.queue_service import prepare_schedule_queue
prepare_schedule_queue()
job = claim_next_job(
domain="schedule",
job_types=[JobType.SCHEDULE_RUN],
)
if job:
return _normalize_generic_job(job)
return None
+1
View File
@@ -52,6 +52,7 @@ QUEUE_KB_JOB_TYPES: dict[str, frozenset[str]] = {
'kb': frozenset({'clean', 'path_upload', 'sync_dify'}),
'shipping': frozenset({'shipping_notice_run', 'shipping_notice_parse'}),
'contract': frozenset({'procurement_contract_review'}),
'schedule': frozenset({'schedule_run'}),
}