feat(cron): add default stagger controls for scheduled jobs
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import type { Command } from "commander";
|
||||
import type { CronJob } from "../../cron/types.js";
|
||||
import type { GatewayRpcOpts } from "../gateway-rpc.js";
|
||||
import { danger } from "../../globals.js";
|
||||
import { sanitizeAgentId } from "../../routing/session-key.js";
|
||||
import { defaultRuntime } from "../../runtime.js";
|
||||
import type { GatewayRpcOpts } from "../gateway-rpc.js";
|
||||
import { addGatewayClientOptions, callGatewayFromCli } from "../gateway-rpc.js";
|
||||
import { parsePositiveIntOrUndefined } from "../program/helpers.js";
|
||||
import {
|
||||
@@ -74,8 +74,10 @@ export function registerCronAddCommand(cron: Command) {
|
||||
.option("--wake <mode>", "Wake mode (now|next-heartbeat)", "now")
|
||||
.option("--at <when>", "Run once at time (ISO) or +duration (e.g. 20m)")
|
||||
.option("--every <duration>", "Run every duration (e.g. 10m, 1h)")
|
||||
.option("--cron <expr>", "Cron expression (5-field)")
|
||||
.option("--cron <expr>", "Cron expression (5-field or 6-field with seconds)")
|
||||
.option("--tz <iana>", "Timezone for cron expressions (IANA)", "")
|
||||
.option("--stagger <duration>", "Cron stagger window (e.g. 30s, 5m)")
|
||||
.option("--exact", "Disable cron staggering (set stagger to 0)", false)
|
||||
.option("--system-event <text>", "System event payload (main session)")
|
||||
.option("--message <text>", "Agent message payload")
|
||||
.option("--thinking <level>", "Thinking level for agent jobs (off|minimal|low|medium|high)")
|
||||
@@ -93,6 +95,12 @@ export function registerCronAddCommand(cron: Command) {
|
||||
.option("--json", "Output JSON", false)
|
||||
.action(async (opts: GatewayRpcOpts & Record<string, unknown>, cmd?: Command) => {
|
||||
try {
|
||||
const staggerRaw = typeof opts.stagger === "string" ? opts.stagger.trim() : "";
|
||||
const useExact = Boolean(opts.exact);
|
||||
if (staggerRaw && useExact) {
|
||||
throw new Error("Choose either --stagger or --exact, not both");
|
||||
}
|
||||
|
||||
const schedule = (() => {
|
||||
const at = typeof opts.at === "string" ? opts.at : "";
|
||||
const every = typeof opts.every === "string" ? opts.every : "";
|
||||
@@ -101,6 +109,9 @@ export function registerCronAddCommand(cron: Command) {
|
||||
if (chosen !== 1) {
|
||||
throw new Error("Choose exactly one schedule: --at, --every, or --cron");
|
||||
}
|
||||
if ((useExact || staggerRaw) && !cronExpr) {
|
||||
throw new Error("--stagger/--exact are only valid with --cron");
|
||||
}
|
||||
if (at) {
|
||||
const atIso = parseAt(at);
|
||||
if (!atIso) {
|
||||
@@ -115,10 +126,24 @@ export function registerCronAddCommand(cron: Command) {
|
||||
}
|
||||
return { kind: "every" as const, everyMs };
|
||||
}
|
||||
const staggerMs = (() => {
|
||||
if (useExact) {
|
||||
return 0;
|
||||
}
|
||||
if (!staggerRaw) {
|
||||
return undefined;
|
||||
}
|
||||
const parsed = parseDurationMs(staggerRaw);
|
||||
if (!parsed) {
|
||||
throw new Error("Invalid --stagger; use e.g. 30s, 1m, 5m");
|
||||
}
|
||||
return parsed;
|
||||
})();
|
||||
return {
|
||||
kind: "cron" as const,
|
||||
expr: cronExpr,
|
||||
tz: typeof opts.tz === "string" && opts.tz.trim() ? opts.tz.trim() : undefined,
|
||||
staggerMs,
|
||||
};
|
||||
})();
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import type { Command } from "commander";
|
||||
import type { CronJob } from "../../cron/types.js";
|
||||
import { danger } from "../../globals.js";
|
||||
import { sanitizeAgentId } from "../../routing/session-key.js";
|
||||
import { defaultRuntime } from "../../runtime.js";
|
||||
@@ -41,6 +42,8 @@ export function registerCronEditCommand(cron: Command) {
|
||||
.option("--every <duration>", "Set interval duration like 10m")
|
||||
.option("--cron <expr>", "Set cron expression")
|
||||
.option("--tz <iana>", "Timezone for cron expressions (IANA)")
|
||||
.option("--stagger <duration>", "Cron stagger window (e.g. 30s, 5m)")
|
||||
.option("--exact", "Disable cron staggering (set stagger to 0)")
|
||||
.option("--system-event <text>", "Set systemEvent payload")
|
||||
.option("--message <text>", "Set agentTurn payload message")
|
||||
.option("--thinking <level>", "Thinking level for agent jobs")
|
||||
@@ -71,6 +74,24 @@ export function registerCronEditCommand(cron: Command) {
|
||||
if (opts.announce && typeof opts.deliver === "boolean") {
|
||||
throw new Error("Choose --announce or --no-deliver (not multiple).");
|
||||
}
|
||||
const staggerRaw = typeof opts.stagger === "string" ? opts.stagger.trim() : "";
|
||||
const useExact = Boolean(opts.exact);
|
||||
if (staggerRaw && useExact) {
|
||||
throw new Error("Choose either --stagger or --exact, not both");
|
||||
}
|
||||
const requestedStaggerMs = (() => {
|
||||
if (useExact) {
|
||||
return 0;
|
||||
}
|
||||
if (!staggerRaw) {
|
||||
return undefined;
|
||||
}
|
||||
const parsed = parseDurationMs(staggerRaw);
|
||||
if (!parsed) {
|
||||
throw new Error("Invalid --stagger; use e.g. 30s, 1m, 5m");
|
||||
}
|
||||
return parsed;
|
||||
})();
|
||||
|
||||
const patch: Record<string, unknown> = {};
|
||||
if (typeof opts.name === "string") {
|
||||
@@ -117,6 +138,12 @@ export function registerCronEditCommand(cron: Command) {
|
||||
if (scheduleChosen > 1) {
|
||||
throw new Error("Choose at most one schedule change");
|
||||
}
|
||||
if (
|
||||
(requestedStaggerMs !== undefined || typeof opts.tz === "string") &&
|
||||
(opts.at || opts.every)
|
||||
) {
|
||||
throw new Error("--stagger/--exact/--tz are only valid for cron schedules");
|
||||
}
|
||||
if (opts.at) {
|
||||
const atIso = parseAt(String(opts.at));
|
||||
if (!atIso) {
|
||||
@@ -134,6 +161,27 @@ export function registerCronEditCommand(cron: Command) {
|
||||
kind: "cron",
|
||||
expr: String(opts.cron),
|
||||
tz: typeof opts.tz === "string" && opts.tz.trim() ? opts.tz.trim() : undefined,
|
||||
staggerMs: requestedStaggerMs,
|
||||
};
|
||||
} else if (requestedStaggerMs !== undefined || typeof opts.tz === "string") {
|
||||
const listed = (await callGatewayFromCli("cron.list", opts, {
|
||||
includeDisabled: true,
|
||||
})) as { jobs?: CronJob[] } | null;
|
||||
const existing = (listed?.jobs ?? []).find((job) => job.id === id);
|
||||
if (!existing) {
|
||||
throw new Error(`unknown cron job id: ${id}`);
|
||||
}
|
||||
if (existing.schedule.kind !== "cron") {
|
||||
throw new Error("Current job is not a cron schedule; use --cron to convert first");
|
||||
}
|
||||
const tz =
|
||||
typeof opts.tz === "string" ? opts.tz.trim() || undefined : existing.schedule.tz;
|
||||
patch.schedule = {
|
||||
kind: "cron",
|
||||
expr: existing.schedule.expr,
|
||||
tz,
|
||||
staggerMs:
|
||||
requestedStaggerMs !== undefined ? requestedStaggerMs : existing.schedule.staggerMs,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -60,4 +60,54 @@ describe("printCronList", () => {
|
||||
expect(() => printCronList([jobWithTarget], mockRuntime)).not.toThrow();
|
||||
expect(logs.some((line) => line.includes("isolated"))).toBe(true);
|
||||
});
|
||||
|
||||
it("shows stagger label for cron schedules", () => {
|
||||
const logs: string[] = [];
|
||||
const mockRuntime = {
|
||||
log: (msg: string) => logs.push(msg),
|
||||
error: () => {},
|
||||
exit: () => {},
|
||||
} as RuntimeEnv;
|
||||
|
||||
const job: CronJob = {
|
||||
id: "staggered-job",
|
||||
name: "Staggered",
|
||||
enabled: true,
|
||||
createdAtMs: Date.now(),
|
||||
updatedAtMs: Date.now(),
|
||||
schedule: { kind: "cron", expr: "0 * * * *", staggerMs: 5 * 60_000 },
|
||||
sessionTarget: "main",
|
||||
wakeMode: "next-heartbeat",
|
||||
payload: { kind: "systemEvent", text: "tick" },
|
||||
state: {},
|
||||
};
|
||||
|
||||
printCronList([job], mockRuntime);
|
||||
expect(logs.some((line) => line.includes("(stagger 5m)"))).toBe(true);
|
||||
});
|
||||
|
||||
it("shows exact label for cron schedules with stagger disabled", () => {
|
||||
const logs: string[] = [];
|
||||
const mockRuntime = {
|
||||
log: (msg: string) => logs.push(msg),
|
||||
error: () => {},
|
||||
exit: () => {},
|
||||
} as RuntimeEnv;
|
||||
|
||||
const job: CronJob = {
|
||||
id: "exact-job",
|
||||
name: "Exact",
|
||||
enabled: true,
|
||||
createdAtMs: Date.now(),
|
||||
updatedAtMs: Date.now(),
|
||||
schedule: { kind: "cron", expr: "0 7 * * *", staggerMs: 0 },
|
||||
sessionTarget: "main",
|
||||
wakeMode: "next-heartbeat",
|
||||
payload: { kind: "systemEvent", text: "tick" },
|
||||
state: {},
|
||||
};
|
||||
|
||||
printCronList([job], mockRuntime);
|
||||
expect(logs.some((line) => line.includes("(exact)"))).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import type { CronJob, CronSchedule } from "../../cron/types.js";
|
||||
import type { GatewayRpcOpts } from "../gateway-rpc.js";
|
||||
import { listChannelPlugins } from "../../channels/plugins/index.js";
|
||||
import { parseAbsoluteTimeMs } from "../../cron/parse.js";
|
||||
import type { CronJob, CronSchedule } from "../../cron/types.js";
|
||||
import { resolveCronStaggerMs } from "../../cron/stagger.js";
|
||||
import { formatDurationHuman } from "../../infra/format-time/format-duration.ts";
|
||||
import { defaultRuntime } from "../../runtime.js";
|
||||
import { colorize, isRich, theme } from "../../terminal/theme.js";
|
||||
import type { GatewayRpcOpts } from "../gateway-rpc.js";
|
||||
import { callGatewayFromCli } from "../gateway-rpc.js";
|
||||
|
||||
export const getCronChannelOptions = () =>
|
||||
@@ -137,7 +138,12 @@ const formatSchedule = (schedule: CronSchedule) => {
|
||||
if (schedule.kind === "every") {
|
||||
return `every ${formatDurationHuman(schedule.everyMs)}`;
|
||||
}
|
||||
return schedule.tz ? `cron ${schedule.expr} @ ${schedule.tz}` : `cron ${schedule.expr}`;
|
||||
const base = schedule.tz ? `cron ${schedule.expr} @ ${schedule.tz}` : `cron ${schedule.expr}`;
|
||||
const staggerMs = resolveCronStaggerMs(schedule);
|
||||
if (staggerMs <= 0) {
|
||||
return `${base} (exact)`;
|
||||
}
|
||||
return `${base} (stagger ${formatDurationHuman(staggerMs)})`;
|
||||
};
|
||||
|
||||
const formatStatus = (job: CronJob) => {
|
||||
|
||||
Reference in New Issue
Block a user