mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-16 08:00:43 +00:00
fix(cli): paginate cron show lookup
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import type { Command } from "commander";
|
||||
import type { CronJob } from "../../cron/types.js";
|
||||
import type { CronDeliveryPreview, CronJob } from "../../cron/types.js";
|
||||
import { defaultRuntime } from "../../runtime.js";
|
||||
import { normalizeLowercaseStringOrEmpty } from "../../shared/string-coerce.js";
|
||||
import type { GatewayRpcOpts } from "../gateway-rpc.js";
|
||||
import { addGatewayClientOptions, callGatewayFromCli } from "../gateway-rpc.js";
|
||||
import {
|
||||
coerceCronDeliveryPreviews,
|
||||
@@ -11,7 +12,9 @@ import {
|
||||
warnIfCronSchedulerDisabled,
|
||||
} from "./shared.js";
|
||||
|
||||
function findCronJobForShow(jobs: CronJob[], idOrName: string): CronJob | undefined {
|
||||
const CRON_SHOW_PAGE_SIZE = 200;
|
||||
|
||||
function findCronJobInPage(jobs: CronJob[], idOrName: string): CronJob | undefined {
|
||||
const needle = normalizeLowercaseStringOrEmpty(idOrName);
|
||||
return jobs.find(
|
||||
(job) =>
|
||||
@@ -20,6 +23,34 @@ function findCronJobForShow(jobs: CronJob[], idOrName: string): CronJob | undefi
|
||||
);
|
||||
}
|
||||
|
||||
async function loadCronJobForShow(
|
||||
opts: GatewayRpcOpts,
|
||||
idOrName: string,
|
||||
): Promise<{ job?: CronJob; deliveryPreview?: CronDeliveryPreview }> {
|
||||
let offset = 0;
|
||||
for (;;) {
|
||||
const res = await callGatewayFromCli("cron.list", opts, {
|
||||
includeDisabled: true,
|
||||
limit: CRON_SHOW_PAGE_SIZE,
|
||||
offset,
|
||||
});
|
||||
const page = res as {
|
||||
jobs?: CronJob[];
|
||||
hasMore?: boolean;
|
||||
nextOffset?: number | null;
|
||||
};
|
||||
const jobs = page.jobs ?? [];
|
||||
const job = findCronJobInPage(jobs, idOrName);
|
||||
if (job) {
|
||||
return { job, deliveryPreview: coerceCronDeliveryPreviews(res).get(job.id) };
|
||||
}
|
||||
if (!page.hasMore || typeof page.nextOffset !== "number") {
|
||||
return {};
|
||||
}
|
||||
offset = page.nextOffset;
|
||||
}
|
||||
}
|
||||
|
||||
function registerCronToggleCommand(params: {
|
||||
cron: Command;
|
||||
name: "enable" | "disable";
|
||||
@@ -86,9 +117,7 @@ export function registerCronSimpleCommands(cron: Command) {
|
||||
.option("--json", "Output JSON", false)
|
||||
.action(async (id, opts) => {
|
||||
try {
|
||||
const res = await callGatewayFromCli("cron.list", opts, { includeDisabled: true });
|
||||
const jobs = (res as { jobs?: CronJob[] } | null)?.jobs ?? [];
|
||||
const job = findCronJobForShow(jobs, String(id));
|
||||
const { job, deliveryPreview } = await loadCronJobForShow(opts, String(id));
|
||||
if (!job) {
|
||||
throw new Error(`cron job not found: ${String(id)}`);
|
||||
}
|
||||
@@ -96,8 +125,7 @@ export function registerCronSimpleCommands(cron: Command) {
|
||||
printCronJson(job);
|
||||
return;
|
||||
}
|
||||
const deliveryPreviews = coerceCronDeliveryPreviews(res);
|
||||
printCronShow(job, defaultRuntime, { deliveryPreview: deliveryPreviews.get(job.id) });
|
||||
printCronShow(job, defaultRuntime, { deliveryPreview });
|
||||
} catch (err) {
|
||||
handleCronCliError(err);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user