mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-17 04:50:51 +00:00
refactor(core): extract shared dedup helpers
This commit is contained in:
@@ -1,6 +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";
|
||||
import type { GatewayRpcOpts } from "../gateway-rpc.js";
|
||||
@@ -8,9 +7,11 @@ import { addGatewayClientOptions, callGatewayFromCli } from "../gateway-rpc.js";
|
||||
import { parsePositiveIntOrUndefined } from "../program/helpers.js";
|
||||
import {
|
||||
getCronChannelOptions,
|
||||
handleCronCliError,
|
||||
parseAt,
|
||||
parseCronStaggerMs,
|
||||
parseDurationMs,
|
||||
printCronJson,
|
||||
printCronList,
|
||||
warnIfCronSchedulerDisabled,
|
||||
} from "./shared.js";
|
||||
@@ -24,10 +25,9 @@ export function registerCronStatusCommand(cron: Command) {
|
||||
.action(async (opts) => {
|
||||
try {
|
||||
const res = await callGatewayFromCli("cron.status", opts, {});
|
||||
defaultRuntime.log(JSON.stringify(res, null, 2));
|
||||
printCronJson(res);
|
||||
} catch (err) {
|
||||
defaultRuntime.error(danger(String(err)));
|
||||
defaultRuntime.exit(1);
|
||||
handleCronCliError(err);
|
||||
}
|
||||
}),
|
||||
);
|
||||
@@ -46,14 +46,13 @@ export function registerCronListCommand(cron: Command) {
|
||||
includeDisabled: Boolean(opts.all),
|
||||
});
|
||||
if (opts.json) {
|
||||
defaultRuntime.log(JSON.stringify(res, null, 2));
|
||||
printCronJson(res);
|
||||
return;
|
||||
}
|
||||
const jobs = (res as { jobs?: CronJob[] } | null)?.jobs ?? [];
|
||||
printCronList(jobs, defaultRuntime);
|
||||
} catch (err) {
|
||||
defaultRuntime.error(danger(String(err)));
|
||||
defaultRuntime.exit(1);
|
||||
handleCronCliError(err);
|
||||
}
|
||||
}),
|
||||
);
|
||||
@@ -273,11 +272,10 @@ export function registerCronAddCommand(cron: Command) {
|
||||
};
|
||||
|
||||
const res = await callGatewayFromCli("cron.add", opts, params);
|
||||
defaultRuntime.log(JSON.stringify(res, null, 2));
|
||||
printCronJson(res);
|
||||
await warnIfCronSchedulerDisabled(opts);
|
||||
} catch (err) {
|
||||
defaultRuntime.error(danger(String(err)));
|
||||
defaultRuntime.exit(1);
|
||||
handleCronCliError(err);
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import type { Command } from "commander";
|
||||
import { danger } from "../../globals.js";
|
||||
import { defaultRuntime } from "../../runtime.js";
|
||||
import { addGatewayClientOptions, callGatewayFromCli } from "../gateway-rpc.js";
|
||||
import { warnIfCronSchedulerDisabled } from "./shared.js";
|
||||
import { handleCronCliError, printCronJson, warnIfCronSchedulerDisabled } from "./shared.js";
|
||||
|
||||
function registerCronToggleCommand(params: {
|
||||
cron: Command;
|
||||
@@ -21,11 +20,10 @@ function registerCronToggleCommand(params: {
|
||||
id,
|
||||
patch: { enabled: params.enabled },
|
||||
});
|
||||
defaultRuntime.log(JSON.stringify(res, null, 2));
|
||||
printCronJson(res);
|
||||
await warnIfCronSchedulerDisabled(opts);
|
||||
} catch (err) {
|
||||
defaultRuntime.error(danger(String(err)));
|
||||
defaultRuntime.exit(1);
|
||||
handleCronCliError(err);
|
||||
}
|
||||
}),
|
||||
);
|
||||
@@ -43,10 +41,9 @@ export function registerCronSimpleCommands(cron: Command) {
|
||||
.action(async (id, opts) => {
|
||||
try {
|
||||
const res = await callGatewayFromCli("cron.remove", opts, { id });
|
||||
defaultRuntime.log(JSON.stringify(res, null, 2));
|
||||
printCronJson(res);
|
||||
} catch (err) {
|
||||
defaultRuntime.error(danger(String(err)));
|
||||
defaultRuntime.exit(1);
|
||||
handleCronCliError(err);
|
||||
}
|
||||
}),
|
||||
);
|
||||
@@ -79,10 +76,9 @@ export function registerCronSimpleCommands(cron: Command) {
|
||||
id,
|
||||
limit,
|
||||
});
|
||||
defaultRuntime.log(JSON.stringify(res, null, 2));
|
||||
printCronJson(res);
|
||||
} catch (err) {
|
||||
defaultRuntime.error(danger(String(err)));
|
||||
defaultRuntime.exit(1);
|
||||
handleCronCliError(err);
|
||||
}
|
||||
}),
|
||||
);
|
||||
@@ -102,12 +98,11 @@ export function registerCronSimpleCommands(cron: Command) {
|
||||
id,
|
||||
mode: opts.due ? "due" : "force",
|
||||
});
|
||||
defaultRuntime.log(JSON.stringify(res, null, 2));
|
||||
printCronJson(res);
|
||||
const result = res as { ok?: boolean; ran?: boolean } | undefined;
|
||||
defaultRuntime.exit(result?.ok && result?.ran ? 0 : 1);
|
||||
} catch (err) {
|
||||
defaultRuntime.error(danger(String(err)));
|
||||
defaultRuntime.exit(1);
|
||||
handleCronCliError(err);
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -2,6 +2,7 @@ import { listChannelPlugins } from "../../channels/plugins/index.js";
|
||||
import { parseAbsoluteTimeMs } from "../../cron/parse.js";
|
||||
import { resolveCronStaggerMs } from "../../cron/stagger.js";
|
||||
import type { CronJob, CronSchedule } from "../../cron/types.js";
|
||||
import { danger } from "../../globals.js";
|
||||
import { formatDurationHuman } from "../../infra/format-time/format-duration.ts";
|
||||
import { defaultRuntime } from "../../runtime.js";
|
||||
import { colorize, isRich, theme } from "../../terminal/theme.js";
|
||||
@@ -11,6 +12,15 @@ import { callGatewayFromCli } from "../gateway-rpc.js";
|
||||
export const getCronChannelOptions = () =>
|
||||
["last", ...listChannelPlugins().map((plugin) => plugin.id)].join("|");
|
||||
|
||||
export function printCronJson(value: unknown) {
|
||||
defaultRuntime.log(JSON.stringify(value, null, 2));
|
||||
}
|
||||
|
||||
export function handleCronCliError(err: unknown) {
|
||||
defaultRuntime.error(danger(String(err)));
|
||||
defaultRuntime.exit(1);
|
||||
}
|
||||
|
||||
export async function warnIfCronSchedulerDisabled(opts: GatewayRpcOpts) {
|
||||
try {
|
||||
const res = (await callGatewayFromCli("cron.status", opts, {})) as {
|
||||
|
||||
Reference in New Issue
Block a user