Files
syops/backend/app/jobs/runner.py
T

39 lines
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
backend/app/jobs/runner.py
用途:Worker 主循环 poll → dispatch → ack/fail。
逻辑:worker.py 调用 run_forever(ctx);Wave 2 接入 store 与 registry。
"""
from __future__ import annotations
import time
from app.jobs.context import JobContext
from app.jobs.registry import get_handler
from app.jobs import store
def run_forever(ctx: JobContext, *, poll_interval_seconds: float = 2.0) -> None:
"""
持续轮询并执行任务。
核心逻辑:claim_next → get_handler → handler(ctx, payload) → 更新状态。
Args:
ctx: Worker 上下文。
poll_interval_seconds: 无任务时的休眠秒数。
"""
ctx.logger.info('Job runner started (skeleton mode — Wave 2 will enable claim loop)')
while True:
try:
job = store.claim_next()
except NotImplementedError:
time.sleep(poll_interval_seconds)
continue
if not job:
time.sleep(poll_interval_seconds)
continue
handler = get_handler(job['job_type'])
if not handler:
ctx.logger.error('No handler for job_type=%s', job.get('job_type'))
continue
handler(ctx, job.get('payload') or {})