mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-13 13:10:34 +00:00
feat(cron): configurable failure alerts for repeated job errors (openclaw#24789) thanks @0xbrak
Verified: - pnpm install --frozen-lockfile - pnpm check - pnpm test -- --run src/cron/service.failure-alert.test.ts src/cli/cron-cli.test.ts src/gateway/protocol/cron-validators.test.ts Co-authored-by: 0xbrak <181251288+0xbrak@users.noreply.github.com> Co-authored-by: Tak Hoffman <781889+Takhoffman@users.noreply.github.com>
This commit is contained in:
@@ -9,6 +9,7 @@ import {
|
||||
import type {
|
||||
CronDelivery,
|
||||
CronDeliveryPatch,
|
||||
CronFailureAlert,
|
||||
CronJob,
|
||||
CronJobCreate,
|
||||
CronJobPatch,
|
||||
@@ -419,6 +420,7 @@ export function createJob(state: CronServiceState, input: CronJobCreate): CronJo
|
||||
wakeMode: input.wakeMode,
|
||||
payload: input.payload,
|
||||
delivery: input.delivery,
|
||||
failureAlert: input.failureAlert,
|
||||
state: {
|
||||
...input.state,
|
||||
},
|
||||
@@ -483,6 +485,9 @@ export function applyJobPatch(job: CronJob, patch: CronJobPatch) {
|
||||
if (patch.delivery) {
|
||||
job.delivery = mergeCronDelivery(job.delivery, patch.delivery);
|
||||
}
|
||||
if ("failureAlert" in patch) {
|
||||
job.failureAlert = mergeCronFailureAlert(job.failureAlert, patch.failureAlert);
|
||||
}
|
||||
if (job.sessionTarget === "main" && job.delivery?.mode !== "webhook") {
|
||||
job.delivery = undefined;
|
||||
}
|
||||
@@ -648,6 +653,42 @@ function mergeCronDelivery(
|
||||
return next;
|
||||
}
|
||||
|
||||
function mergeCronFailureAlert(
|
||||
existing: CronFailureAlert | false | undefined,
|
||||
patch: CronFailureAlert | false | undefined,
|
||||
): CronFailureAlert | false | undefined {
|
||||
if (patch === false) {
|
||||
return false;
|
||||
}
|
||||
if (patch === undefined) {
|
||||
return existing;
|
||||
}
|
||||
const base = existing === false || existing === undefined ? {} : existing;
|
||||
const next: CronFailureAlert = { ...base };
|
||||
|
||||
if ("after" in patch) {
|
||||
const after = typeof patch.after === "number" && Number.isFinite(patch.after) ? patch.after : 0;
|
||||
next.after = after > 0 ? Math.floor(after) : undefined;
|
||||
}
|
||||
if ("channel" in patch) {
|
||||
const channel = typeof patch.channel === "string" ? patch.channel.trim() : "";
|
||||
next.channel = channel ? channel : undefined;
|
||||
}
|
||||
if ("to" in patch) {
|
||||
const to = typeof patch.to === "string" ? patch.to.trim() : "";
|
||||
next.to = to ? to : undefined;
|
||||
}
|
||||
if ("cooldownMs" in patch) {
|
||||
const cooldownMs =
|
||||
typeof patch.cooldownMs === "number" && Number.isFinite(patch.cooldownMs)
|
||||
? patch.cooldownMs
|
||||
: -1;
|
||||
next.cooldownMs = cooldownMs >= 0 ? Math.floor(cooldownMs) : undefined;
|
||||
}
|
||||
|
||||
return next;
|
||||
}
|
||||
|
||||
export function isJobDue(job: CronJob, nowMs: number, opts: { forced: boolean }) {
|
||||
if (!job.state) {
|
||||
job.state = {};
|
||||
|
||||
Reference in New Issue
Block a user