Owner jobs and guests' own scheduled jobs are the cron-jobs live plugin's (forms move-ai-agent-cron-jobs-into-a-plugin_u0wa6b, cron-scheduled-in-the-cli-and-mcp_x4nk83). Gone: backend/cron/, schemas/cron.py, /api/cron*, /api/me/cron*, the cron scheduler thread, Settings → Cron and the guests' Tâches planifiées pages, `ai-agent cron` (now a pointer), MCP run_cron_job / set_cron_job / browse cron, the scheduled-jobs core skill, guestDefaults.cronLimits. The core keeps thin contracts: - runs.guest bridge (runs/guestjob.py): a guest's job run as that guest, confined by the core from their record; the plugin only names it. - runs.start's cron stamp → meta.cron. - delivery of a guest job's result (push, mail, mailHtml) off the stamp. - « Discuter »: POST /api/cron-runs/discuss. - runs.cron provider readers (plugins/cron_bridge.py): budget, auto-archive, agents catalog, the agents page's Schedule. - core/cronexpr.py, agents/runconfig.py; context.write create for a new agent definition; ui.pages[].unless; setRoute keeps a Settings page under /settings/p/. - carryover.cron_jobs: /data/cron-jobs.json → the plugin's jobs.json, cronLimits → limits.json, guests' qwen jobs → sonnet (PR #23's decision, superseded). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
94 lines
3.5 KiB
Python
94 lines
3.5 KiB
Python
"""5-field cron expressions, dependency-free — the plugin scheduler
|
|
(``plugins/scheduler.py``), the manifest validator and the week's budget use
|
|
it. Standard ``min hour dom mon dow`` with ``*``, lists, ranges and ``/step``
|
|
(no names, no @shortcuts), evaluated in the container's local time (``TZ``).
|
|
|
|
It was ``cron/scheduler.py``'s until the cron jobs became the ``cron-jobs``
|
|
live plugin (2026-11-28), which carries its own copy."""
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
# (low, high) bounds per field: minute, hour, day-of-month, month, day-of-week.
|
|
_BOUNDS = [(0, 59), (0, 23), (1, 31), (1, 12), (0, 6)]
|
|
|
|
|
|
def _parse_field(spec: str, lo: int, hi: int) -> set[int]:
|
|
vals: set[int] = set()
|
|
for part in spec.split(","):
|
|
part = part.strip()
|
|
step = 1
|
|
if "/" in part:
|
|
part, step_s = part.split("/", 1)
|
|
step = int(step_s)
|
|
if part in ("*", ""):
|
|
a, b = lo, hi
|
|
elif "-" in part:
|
|
a_s, b_s = part.split("-", 1)
|
|
a, b = int(a_s), int(b_s)
|
|
else:
|
|
a = b = int(part)
|
|
if not (lo <= a <= b <= hi) or step < 1:
|
|
raise ValueError(f"cron field out of range: {spec!r}")
|
|
vals.update(range(a, b + 1, step))
|
|
return vals
|
|
|
|
|
|
def parse_schedule(expr: str) -> list[set[int]]:
|
|
"""Parse a 5-field cron expression; raises ValueError on anything else."""
|
|
fields = (expr or "").split()
|
|
if len(fields) != 5:
|
|
raise ValueError("schedule must have 5 fields (min hour dom mon dow)")
|
|
return [_parse_field(f, lo, hi)
|
|
for f, (lo, hi) in zip(fields, _BOUNDS)]
|
|
|
|
|
|
def _day_matches(parsed: list[set[int]], dt: datetime) -> bool:
|
|
"""Standard cron semantics: dom and dow are OR'd when both are restricted,
|
|
AND'd (trivially) when either is ``*``."""
|
|
_, _, dom, mon, dow = parsed
|
|
if dt.month not in mon:
|
|
return False
|
|
cron_dow = (dt.weekday() + 1) % 7 # cron: 0 = Sunday
|
|
dom_any = dom == set(range(1, 32))
|
|
dow_any = dow == set(range(0, 7))
|
|
if dom_any and dow_any:
|
|
return True
|
|
if dom_any:
|
|
return cron_dow in dow
|
|
if dow_any:
|
|
return dt.day in dom
|
|
return dt.day in dom or cron_dow in dow
|
|
|
|
|
|
def due(parsed: list[set[int]], dt: datetime) -> bool:
|
|
"""Does this minute match the schedule?"""
|
|
return (dt.minute in parsed[0] and dt.hour in parsed[1]
|
|
and _day_matches(parsed, dt))
|
|
|
|
|
|
def next_run(expr: str, after: datetime | None = None) -> str | None:
|
|
"""The next fire time strictly after ``after`` (default: now), as an ISO
|
|
minute string **with the local UTC offset** (a viewer in any timezone
|
|
parses it to the right instant) — or None for an invalid/never-matching
|
|
schedule. Day-level skipping keeps this fast for any real schedule."""
|
|
try:
|
|
parsed = parse_schedule(expr)
|
|
except ValueError:
|
|
return None
|
|
base = (after or datetime.now()).replace(second=0, microsecond=0)
|
|
minutes, hours = sorted(parsed[0]), sorted(parsed[1])
|
|
day = base
|
|
for _ in range(366 * 2):
|
|
if not _day_matches(parsed, day):
|
|
day = (day + timedelta(days=1)).replace(hour=0, minute=0)
|
|
continue
|
|
for h in hours:
|
|
if h < day.hour:
|
|
continue
|
|
for m in minutes:
|
|
cand = day.replace(hour=h, minute=m)
|
|
if cand > base:
|
|
return cand.astimezone().isoformat(timespec="minutes")
|
|
day = (day + timedelta(days=1)).replace(hour=0, minute=0)
|
|
return None
|