scheduler.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. """APScheduler 装配。
  2. 用 ``AsyncIOScheduler`` 与主事件循环共用同一个 loop——非 async 的调度器
  3. 会在自己的线程里跑任务,那样 ``agent.astream`` / aiosqlite 的 loop 绑定
  4. 全部失效。这是 1.5 明确记录过的约束。
  5. 启动时把 DB 里 enabled 的任务全量装载进调度器:DB 是任务定义的**事实源**,
  6. 调度器内存里的 job 只是它的一份投影。所以改任务必须「先写 DB,再同步
  7. job」,反之会在重启后丢失。
  8. """
  9. from __future__ import annotations
  10. import asyncio
  11. from typing import Any
  12. from apscheduler.schedulers.asyncio import AsyncIOScheduler
  13. from apscheduler.triggers.cron import CronTrigger
  14. from db import schedule_store
  15. from obs import get_logger
  16. from scheduler.runner import run_schedule
  17. logger = get_logger(__name__)
  18. _scheduler: AsyncIOScheduler | None = None
  19. _active: set[asyncio.Task] = set()
  20. def _job_id(schedule_id: str) -> str:
  21. return f"schedule:{schedule_id}"
  22. def _parse_cron(expr: str) -> CronTrigger:
  23. """解析 5 段 cron。非法表达式抛 ValueError,由调用方转成 400。"""
  24. return CronTrigger.from_crontab((expr or "").strip(), timezone="Asia/Shanghai")
  25. async def _invoke(schedule_id: str) -> None:
  26. """job 回调。吞掉异常——见 scheduler/runner.run_schedule 的说明。"""
  27. task = asyncio.current_task()
  28. _active.add(task)
  29. try:
  30. await run_schedule(schedule_id, trigger="cron")
  31. except Exception: # runner 已记录带 schedule_id / trace_id 的错误,避免重复堆栈
  32. pass
  33. finally:
  34. _active.discard(task)
  35. async def _invoke_batch(callback):
  36. """分日计划可能等待网关,关闭时与普通定时任务一起取消并等待。"""
  37. task = asyncio.current_task()
  38. _active.add(task)
  39. try:
  40. return await callback()
  41. finally:
  42. _active.discard(task)
  43. def get_scheduler() -> AsyncIOScheduler | None:
  44. """取调度器实例;未启动返回 None。"""
  45. return _scheduler
  46. def is_running() -> bool:
  47. return _scheduler is not None and _scheduler.running
  48. async def start() -> int:
  49. """启动调度器并装载已启用的任务。返回装载条数。"""
  50. global _scheduler
  51. if _scheduler is not None and _scheduler.running:
  52. return len(_scheduler.get_jobs())
  53. _scheduler = AsyncIOScheduler(timezone="Asia/Shanghai")
  54. _scheduler.start()
  55. schedules = await asyncio.to_thread(schedule_store.list_schedules, None, True, 500)
  56. loaded = 0
  57. for schedule in schedules:
  58. try:
  59. add_job(schedule)
  60. loaded += 1
  61. except Exception as exc:
  62. # 单条 cron 非法不该让整个调度器起不来,但必须留下痕迹
  63. logger.error(
  64. "装载定时任务失败 id=%s cron=%r:%s: %s",
  65. schedule.get("id"), schedule.get("cron"), type(exc).__name__, exc,
  66. )
  67. import config
  68. expression = config.get_str('MAIN_FAN_BATCH_CRON')
  69. if config.get_bool('MAIN_FAN_LOCAL_BATCH_ENABLED', False) and expression:
  70. from extensions.main_fan.batch_service import daily_callback, monthly_callback
  71. from extensions.main_fan.monthly_schedule import configured_spread_days
  72. _scheduler.add_job(_invoke_batch, args=[monthly_callback], trigger=_parse_cron(expression),
  73. id='main-fan:monthly-local', name='主通风机月度本地批次', replace_existing=True,
  74. max_instances=1, misfire_grace_time=60, coalesce=True)
  75. loaded += 1
  76. if configured_spread_days() > 1:
  77. daily_expression = ' '.join(expression.split()[:2] + ['*', '*', '*'])
  78. _scheduler.add_job(_invoke_batch, args=[daily_callback], trigger=_parse_cron(daily_expression),
  79. id='main-fan:daily-local', name='主通风机月度分日批次', replace_existing=True,
  80. max_instances=1, misfire_grace_time=60, coalesce=True)
  81. loaded += 1
  82. logger.info("调度器已启动,装载 %d 个任务(扫描到 %d 条)", loaded, len(schedules))
  83. return loaded
  84. async def shutdown() -> None:
  85. """停止调度器。"""
  86. global _scheduler
  87. if _scheduler is None:
  88. return
  89. try:
  90. _scheduler.pause()
  91. active = list(_active)
  92. for task in active:
  93. task.cancel()
  94. if active:
  95. await asyncio.gather(*active, return_exceptions=True)
  96. _scheduler.shutdown(wait=False)
  97. except Exception as exc: # pragma: no cover
  98. logger.warning("关闭调度器异常:%s: %s", type(exc).__name__, exc)
  99. _scheduler = None
  100. logger.info("调度器已停止")
  101. def add_job(schedule: dict[str, Any]) -> str:
  102. """把一个任务定义投影成调度器 job。"""
  103. if _scheduler is None:
  104. raise RuntimeError("调度器未启动")
  105. schedule_id = schedule["id"]
  106. trigger = _parse_cron(schedule["cron"])
  107. _scheduler.add_job(
  108. _invoke,
  109. trigger=trigger,
  110. args=[schedule_id],
  111. id=_job_id(schedule_id),
  112. name=schedule.get("title") or schedule_id,
  113. replace_existing=True,
  114. max_instances=1,
  115. # 错过触发时间(服务重启/长时间 GC)时不要补跑:定时任务多是
  116. # 「每日汇报」这类语义,补跑历史时点没有意义,还会突然烧一批额度
  117. misfire_grace_time=60,
  118. coalesce=True,
  119. )
  120. logger.info("已装载定时任务 id=%s cron=%s", schedule_id, schedule["cron"])
  121. return _job_id(schedule_id)
  122. def remove_job(schedule_id: str) -> bool:
  123. """摘掉 job。任务定义被删或停用时调用。"""
  124. if _scheduler is None:
  125. return False
  126. job = _scheduler.get_job(_job_id(schedule_id))
  127. if job is None:
  128. return False
  129. _scheduler.remove_job(_job_id(schedule_id))
  130. logger.info("已摘除定时任务 id=%s", schedule_id)
  131. return True
  132. def sync_job(schedule: dict[str, Any]) -> None:
  133. """按任务定义的当前状态同步 job:enabled 则装载,否则摘除。"""
  134. if schedule.get("enabled"):
  135. add_job(schedule)
  136. else:
  137. remove_job(schedule["id"])
  138. def jobs() -> list[dict[str, Any]]:
  139. """当前调度器里的 job 快照,供 /health 展示。"""
  140. if _scheduler is None:
  141. return []
  142. return [
  143. {
  144. "id": job.id,
  145. "name": job.name,
  146. "next_run_time": job.next_run_time.isoformat() if job.next_run_time else None,
  147. "trigger": str(job.trigger),
  148. }
  149. for job in _scheduler.get_jobs()
  150. ]