mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 17:11:36 +00:00
refactor(cron-cli): share enable-disable command wiring
This commit is contained in:
@@ -79,6 +79,18 @@ async function runCronAddAndGetParams(addArgs: string[]): Promise<CronAddParams>
|
|||||||
return (addCall?.[2] ?? {}) as CronAddParams;
|
return (addCall?.[2] ?? {}) as CronAddParams;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function runCronSimpleAndGetUpdatePatch(
|
||||||
|
command: "enable" | "disable",
|
||||||
|
): Promise<{ enabled?: boolean }> {
|
||||||
|
resetGatewayMock();
|
||||||
|
const program = buildProgram();
|
||||||
|
await program.parseAsync(["cron", command, "job-1"], { from: "user" });
|
||||||
|
const updateCall = callGatewayFromCli.mock.calls.find((call) => call[0] === "cron.update");
|
||||||
|
return ((updateCall?.[2] as { patch?: { enabled?: boolean } } | undefined)?.patch ?? {}) as {
|
||||||
|
enabled?: boolean;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
describe("cron cli", () => {
|
describe("cron cli", () => {
|
||||||
it("trims model and thinking on cron add", { timeout: 60_000 }, async () => {
|
it("trims model and thinking on cron add", { timeout: 60_000 }, async () => {
|
||||||
resetGatewayMock();
|
resetGatewayMock();
|
||||||
@@ -196,6 +208,16 @@ describe("cron cli", () => {
|
|||||||
expect(params?.deleteAfterRun).toBe(false);
|
expect(params?.deleteAfterRun).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("cron enable sets enabled=true patch", async () => {
|
||||||
|
const patch = await runCronSimpleAndGetUpdatePatch("enable");
|
||||||
|
expect(patch.enabled).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("cron disable sets enabled=false patch", async () => {
|
||||||
|
const patch = await runCronSimpleAndGetUpdatePatch("disable");
|
||||||
|
expect(patch.enabled).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
it("sends agent id on cron add", async () => {
|
it("sends agent id on cron add", async () => {
|
||||||
resetGatewayMock();
|
resetGatewayMock();
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,33 @@ import { defaultRuntime } from "../../runtime.js";
|
|||||||
import { addGatewayClientOptions, callGatewayFromCli } from "../gateway-rpc.js";
|
import { addGatewayClientOptions, callGatewayFromCli } from "../gateway-rpc.js";
|
||||||
import { warnIfCronSchedulerDisabled } from "./shared.js";
|
import { warnIfCronSchedulerDisabled } from "./shared.js";
|
||||||
|
|
||||||
|
function registerCronToggleCommand(params: {
|
||||||
|
cron: Command;
|
||||||
|
name: "enable" | "disable";
|
||||||
|
description: string;
|
||||||
|
enabled: boolean;
|
||||||
|
}) {
|
||||||
|
addGatewayClientOptions(
|
||||||
|
params.cron
|
||||||
|
.command(params.name)
|
||||||
|
.description(params.description)
|
||||||
|
.argument("<id>", "Job id")
|
||||||
|
.action(async (id, opts) => {
|
||||||
|
try {
|
||||||
|
const res = await callGatewayFromCli("cron.update", opts, {
|
||||||
|
id,
|
||||||
|
patch: { enabled: params.enabled },
|
||||||
|
});
|
||||||
|
defaultRuntime.log(JSON.stringify(res, null, 2));
|
||||||
|
await warnIfCronSchedulerDisabled(opts);
|
||||||
|
} catch (err) {
|
||||||
|
defaultRuntime.error(danger(String(err)));
|
||||||
|
defaultRuntime.exit(1);
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
export function registerCronSimpleCommands(cron: Command) {
|
export function registerCronSimpleCommands(cron: Command) {
|
||||||
addGatewayClientOptions(
|
addGatewayClientOptions(
|
||||||
cron
|
cron
|
||||||
@@ -24,45 +51,18 @@ export function registerCronSimpleCommands(cron: Command) {
|
|||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
addGatewayClientOptions(
|
registerCronToggleCommand({
|
||||||
cron
|
cron,
|
||||||
.command("enable")
|
name: "enable",
|
||||||
.description("Enable a cron job")
|
description: "Enable a cron job",
|
||||||
.argument("<id>", "Job id")
|
enabled: true,
|
||||||
.action(async (id, opts) => {
|
});
|
||||||
try {
|
registerCronToggleCommand({
|
||||||
const res = await callGatewayFromCli("cron.update", opts, {
|
cron,
|
||||||
id,
|
name: "disable",
|
||||||
patch: { enabled: true },
|
description: "Disable a cron job",
|
||||||
});
|
enabled: false,
|
||||||
defaultRuntime.log(JSON.stringify(res, null, 2));
|
});
|
||||||
await warnIfCronSchedulerDisabled(opts);
|
|
||||||
} catch (err) {
|
|
||||||
defaultRuntime.error(danger(String(err)));
|
|
||||||
defaultRuntime.exit(1);
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
||||||
addGatewayClientOptions(
|
|
||||||
cron
|
|
||||||
.command("disable")
|
|
||||||
.description("Disable a cron job")
|
|
||||||
.argument("<id>", "Job id")
|
|
||||||
.action(async (id, opts) => {
|
|
||||||
try {
|
|
||||||
const res = await callGatewayFromCli("cron.update", opts, {
|
|
||||||
id,
|
|
||||||
patch: { enabled: false },
|
|
||||||
});
|
|
||||||
defaultRuntime.log(JSON.stringify(res, null, 2));
|
|
||||||
await warnIfCronSchedulerDisabled(opts);
|
|
||||||
} catch (err) {
|
|
||||||
defaultRuntime.error(danger(String(err)));
|
|
||||||
defaultRuntime.exit(1);
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
||||||
addGatewayClientOptions(
|
addGatewayClientOptions(
|
||||||
cron
|
cron
|
||||||
|
|||||||
Reference in New Issue
Block a user