feat(schedule): 任务支持「每天定时 / 每周定时」,用选择器而不是手写 cron
- schedule.py: 新增调度计算模块(纯函数,便于单测)。三种模式:interval(每 N 分钟)/ daily(选钟点)/ weekly(选星期 + 钟点) - 钟点模式是「固定时刻」而非「固定延迟」——从日历重算,所以某轮跑晚了不会把之后每一轮都拖晚。interval 保持原语义:从上一轮开始计时 - 抖动只加给 interval。给「每天 9:00」也加抖动就成了 9:00–9:01 随机触发,操作者选的时间被悄悄改掉,只会像 bug - 模型 / schema / service: 新增 schedule_mode / schedule_hours / schedule_days / schedule_minute。时钟字段存逗号分隔文本——几个小整数、永远整体读写,单开一张表只会换来 join。interval_minutes 保留且仍是默认值,已有任务不受影响 - service: 改动任何调度字段都按合并后的状态重算 next_run_at。重新启用也算改动,否则停用一个月再打开会带着一个月前的 next_run_at,一保存就立即触发 - 前端: 运行方式三选一 + 小时/星期胶囊多选 + 分钟下拉,并实时预览结果句子。任务卡片改显示后端拼好的 schedule_label,避免列表和编辑器对同一计划给出两种说法 - 校验: 钟点模式至少选一个时间,按周至少选一个星期 tests/test_schedule.py 新增 24 个用例,含「恰好等于当前时刻的档位归属下一天」这个会让调度器自循环的边界。
This commit is contained in:
@@ -23,8 +23,15 @@ is enough here: there is exactly one process, one global crawler subprocess, and
|
||||
therefore no concurrency to coordinate -- a cron-style library would add a
|
||||
dependency without adding a capability.
|
||||
|
||||
Scheduling is **fixed-delay**, not fixed-rate: ``next_run_at`` is set from the
|
||||
moment a run starts, so a slow run cannot make its task fire back-to-back.
|
||||
Two families of schedule, and the difference matters:
|
||||
|
||||
* ``interval`` is **fixed-delay**, not fixed-rate -- ``next_run_at`` is measured
|
||||
from the moment a run starts, so a slow run cannot make its task fire
|
||||
back-to-back.
|
||||
* the clock modes (``daily``/``weekly``) are **fixed-time** -- recomputed from the
|
||||
calendar, so a run that starts late does not drag every later run with it.
|
||||
|
||||
The arithmetic for both lives in schedule.py.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
@@ -37,7 +44,7 @@ from sqlalchemy import select
|
||||
from tools.time_util import get_current_timestamp
|
||||
|
||||
from ..services import crawler_manager
|
||||
from . import app_settings
|
||||
from . import app_settings, schedule
|
||||
from .db import get_session
|
||||
from .models import MonitorRun, MonitorTask, RUN_INTERRUPTED, RUN_RUNNING
|
||||
from .runner import execute_task
|
||||
@@ -45,10 +52,9 @@ from .settings import get_cookie
|
||||
|
||||
POLL_INTERVAL_SECONDS = 20
|
||||
# Spread tasks sharing an interval so they do not all come due on the same tick.
|
||||
# Applied to interval mode only -- see the advance step below.
|
||||
JITTER_SECONDS = 60
|
||||
|
||||
_MS_PER_MINUTE = 60_000
|
||||
|
||||
|
||||
class MonitorScheduler:
|
||||
"""Polls the task table and runs whatever is due."""
|
||||
@@ -168,11 +174,34 @@ class MonitorScheduler:
|
||||
# Advance before running so a crash mid-run cannot cause an immediate
|
||||
# re-fire, and so a long outage coalesces into a single run instead
|
||||
# of one run per missed interval.
|
||||
task.next_run_at = (
|
||||
get_current_timestamp()
|
||||
+ task.interval_minutes * _MS_PER_MINUTE
|
||||
+ random.randint(0, JITTER_SECONDS) * 1000
|
||||
now = get_current_timestamp()
|
||||
following = schedule.next_occurrence(
|
||||
mode=task.schedule_mode,
|
||||
interval_minutes=task.interval_minutes,
|
||||
hours=schedule.parse_hours(task.schedule_hours),
|
||||
days=schedule.parse_days(task.schedule_days),
|
||||
minute=task.schedule_minute,
|
||||
after_ms=now,
|
||||
)
|
||||
|
||||
if following is None:
|
||||
# A clock schedule with no times can never fire. The API rejects
|
||||
# that shape, so this guards against a hand-edited row: park the
|
||||
# task with no next run rather than leaving it permanently due and
|
||||
# re-running it on every tick.
|
||||
task.next_run_at = None
|
||||
print(
|
||||
f"[monitor.scheduler] task {task.id} has no usable schedule "
|
||||
f"and will not run until one is set"
|
||||
)
|
||||
elif task.schedule_mode == schedule.MODE_INTERVAL:
|
||||
# Jitter belongs to the interval mode only. Spreading identical
|
||||
# intervals apart is the point; nudging a time the operator
|
||||
# explicitly picked is not -- it just looks like a broken clock.
|
||||
task.next_run_at = following + random.randint(0, JITTER_SECONDS) * 1000
|
||||
else:
|
||||
task.next_run_at = following
|
||||
|
||||
task_id = task.id
|
||||
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user