mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 05:37:41 +00:00
refactor(src): split oversized modules
This commit is contained in:
54
src/cron/service/store.ts
Normal file
54
src/cron/service/store.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { migrateLegacyCronPayload } from "../payload-migration.js";
|
||||
import { loadCronStore, saveCronStore } from "../store.js";
|
||||
import type { CronJob } from "../types.js";
|
||||
import { inferLegacyName, normalizeOptionalText } from "./normalize.js";
|
||||
import type { CronServiceState } from "./state.js";
|
||||
|
||||
export async function ensureLoaded(state: CronServiceState) {
|
||||
if (state.store) return;
|
||||
const loaded = await loadCronStore(state.deps.storePath);
|
||||
const jobs = (loaded.jobs ?? []) as unknown as Array<Record<string, unknown>>;
|
||||
let mutated = false;
|
||||
for (const raw of jobs) {
|
||||
const nameRaw = raw.name;
|
||||
if (typeof nameRaw !== "string" || nameRaw.trim().length === 0) {
|
||||
raw.name = inferLegacyName({
|
||||
schedule: raw.schedule as never,
|
||||
payload: raw.payload as never,
|
||||
});
|
||||
mutated = true;
|
||||
} else {
|
||||
raw.name = nameRaw.trim();
|
||||
}
|
||||
|
||||
const desc = normalizeOptionalText(raw.description);
|
||||
if (raw.description !== desc) {
|
||||
raw.description = desc;
|
||||
mutated = true;
|
||||
}
|
||||
|
||||
const payload = raw.payload;
|
||||
if (payload && typeof payload === "object" && !Array.isArray(payload)) {
|
||||
if (migrateLegacyCronPayload(payload as Record<string, unknown>)) {
|
||||
mutated = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
state.store = { version: 1, jobs: jobs as unknown as CronJob[] };
|
||||
if (mutated) await persist(state);
|
||||
}
|
||||
|
||||
export function warnIfDisabled(state: CronServiceState, action: string) {
|
||||
if (state.deps.cronEnabled) return;
|
||||
if (state.warnedDisabled) return;
|
||||
state.warnedDisabled = true;
|
||||
state.deps.log.warn(
|
||||
{ enabled: false, action, storePath: state.deps.storePath },
|
||||
"cron: scheduler disabled; jobs will not run automatically",
|
||||
);
|
||||
}
|
||||
|
||||
export async function persist(state: CronServiceState) {
|
||||
if (!state.store) return;
|
||||
await saveCronStore(state.deps.storePath, state.store);
|
||||
}
|
||||
Reference in New Issue
Block a user