Files
jianli-boss/server/scheduler.py
T
2026-09-03 15:47:50 +08:00

143 lines
5.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.
"""
定时任务调度器:后台循环扫描 rpa_schedule 表,每日到点自动下发 RpaTask,
客户端通过现有 /api/rpa/tasks/pending 轮询认领执行(认领机制零改动)。
- 每 SCAN_INTERVAL 秒扫描一轮;排程时间均为北京时间 HH:MM
- 每个排程每天只下发一次开始动作、一次结束动作
(last_start_date / last_stop_date 记录北京日期防重复)
- 下发前复用 RpaTask 去重规则:同类型任务未达终态则跳过本轮,
不消耗当日次数,下轮重试
- 客户端离线时照常下发:任务 pending 超过 10 分钟无人认领,
由 fetch_pending_task 的现有过期机制自动 expired
"""
import asyncio
import json
from datetime import datetime, timedelta
from loguru import logger
from sqlalchemy.orm import Session
from .database import SessionLocal
from .models import RpaSchedule, RpaTask
SCAN_INTERVAL = 30 # 秒
# pending 超过该时长未客户端认领则过期(与 routers/rpa.py 共用同一常量)
PENDING_EXPIRE_MINUTES = 10
# kind → (开始任务类型, 结束任务类型)
KIND_TASK_TYPES = {
"apply": ("start_apply", "stop_apply"),
"activate": ("start_activate", "stop_activate"),
}
def _beijing_now() -> datetime:
return datetime.utcnow() + timedelta(hours=8)
def _due(last_date: str | None, hhmm: str, now_cn: datetime) -> bool:
"""到点判断:当日未触发过 且 当前 HH:MM 已到(零填充字符串可按字典序比较)"""
return last_date != now_cn.strftime("%Y-%m-%d") and now_cn.strftime("%H:%M") >= hhmm
def _dispatch(db: Session, sched: RpaSchedule, start: bool, now_cn: datetime) -> None:
"""下发一次开始/结束任务,并更新防重复日期"""
task_type = KIND_TASK_TYPES[sched.kind][0 if start else 1]
action = "开始" if start else "结束"
dup = db.query(RpaTask).filter(
RpaTask.user_id == sched.user_id,
RpaTask.task_type == task_type,
RpaTask.status.in_(("pending", "running")),
).first()
if dup:
# 同类型任务未终态:不消耗当日次数,下一轮重试
sched.last_result = f"定时{action}跳过:同类型任务 #{dup.id} 未结束"
db.commit()
return
task = RpaTask(
user_id=sched.user_id,
task_type=task_type,
# 只有开始任务携带参数;stop 任务无需 params
params=json.dumps(json.loads(sched.params) if sched.params else {},
ensure_ascii=False) if start else None,
)
db.add(task)
db.flush() # 先取 task.id 再写排程状态
today = now_cn.strftime("%Y-%m-%d")
if start:
sched.last_start_date = today
else:
sched.last_stop_date = today
sched.last_result = f"定时{action}已下发:任务 #{task.id}({task_type})"
db.commit()
logger.info(f"定时任务排程 #{sched.id}({sched.kind}) {action}已下发 -> RpaTask #{task.id} {task_type}")
def _expire_stale(db: Session) -> None:
"""pending 过期清理兜底:客户端长期离线不轮询时,fetch_pending_task 的清理
永远不会被触发,残留 pending 会永久阻塞后续调度——由调度器每轮代做"""
cutoff = datetime.utcnow() - timedelta(minutes=PENDING_EXPIRE_MINUTES)
stale = db.query(RpaTask).filter(
RpaTask.status == "pending",
RpaTask.created_at < cutoff,
).all()
for t in stale:
t.status = "expired"
t.result = f"超过 {PENDING_EXPIRE_MINUTES} 分钟未执行,自动过期"
if stale:
db.commit()
logger.info(f"调度器清理超时未认领任务 {len(stale)} 条")
def tick(db: Session | None = None) -> None:
"""单轮扫描。db 可注入(测试用),缺省自建会话;异常只记日志不中断循环"""
own = db is None
if own:
db = SessionLocal()
try:
_expire_stale(db)
now_cn = _beijing_now()
rows = db.query(RpaSchedule).filter(RpaSchedule.enabled.is_(True)).all()
for s in rows:
if s.kind not in KIND_TASK_TYPES:
logger.warning(f"定时任务排程 #{s.id} 类型未知({s.kind}),跳过")
continue
if _due(s.last_start_date, s.start_time, now_cn):
_dispatch(db, s, start=True, now_cn=now_cn)
if _due(s.last_stop_date, s.end_time, now_cn):
_dispatch(db, s, start=False, now_cn=now_cn)
except Exception as e:
db.rollback()
logger.error(f"定时任务扫描异常:{e}")
finally:
if own:
db.close()
async def _loop() -> None:
while True:
try:
# 同步 DB 操作丢线程池,避免阻塞事件循环
await asyncio.to_thread(tick)
except Exception as e:
logger.error(f"定时任务调度循环异常:{e}")
await asyncio.sleep(SCAN_INTERVAL)
def start_scheduler() -> asyncio.Task:
"""lifespan 内启动后台调度循环,返回 Task 供关闭时取消"""
logger.info(f"定时任务调度器已启动(每 {SCAN_INTERVAL} 秒扫描)")
return asyncio.create_task(_loop())
async def stop_scheduler(task: asyncio.Task | None) -> None:
if task is None:
return
task.cancel()
try:
await task
except asyncio.CancelledError:
pass