|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import os from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import { CronService } from "./service.js"; |
| 6 | + |
| 7 | +const noopLogger = { |
| 8 | + debug: vi.fn(), |
| 9 | + info: vi.fn(), |
| 10 | + warn: vi.fn(), |
| 11 | + error: vi.fn(), |
| 12 | +}; |
| 13 | + |
| 14 | +async function makeStorePath() { |
| 15 | + const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-cron-failure-alert-")); |
| 16 | + return { |
| 17 | + storePath: path.join(dir, "cron", "jobs.json"), |
| 18 | + cleanup: async () => { |
| 19 | + await fs.rm(dir, { recursive: true, force: true }); |
| 20 | + }, |
| 21 | + }; |
| 22 | +} |
| 23 | + |
| 24 | +describe("CronService failure alerts", () => { |
| 25 | + beforeEach(() => { |
| 26 | + vi.useFakeTimers(); |
| 27 | + vi.setSystemTime(new Date("2026-01-01T00:00:00.000Z")); |
| 28 | + noopLogger.debug.mockClear(); |
| 29 | + noopLogger.info.mockClear(); |
| 30 | + noopLogger.warn.mockClear(); |
| 31 | + noopLogger.error.mockClear(); |
| 32 | + }); |
| 33 | + |
| 34 | + afterEach(() => { |
| 35 | + vi.useRealTimers(); |
| 36 | + }); |
| 37 | + |
| 38 | + it("alerts after configured consecutive failures and honors cooldown", async () => { |
| 39 | + const store = await makeStorePath(); |
| 40 | + const sendCronFailureAlert = vi.fn(async () => undefined); |
| 41 | + const runIsolatedAgentJob = vi.fn(async () => ({ |
| 42 | + status: "error" as const, |
| 43 | + error: "wrong model id", |
| 44 | + })); |
| 45 | + |
| 46 | + const cron = new CronService({ |
| 47 | + storePath: store.storePath, |
| 48 | + cronEnabled: true, |
| 49 | + cronConfig: { |
| 50 | + failureAlert: { |
| 51 | + enabled: true, |
| 52 | + after: 2, |
| 53 | + cooldownMs: 60_000, |
| 54 | + }, |
| 55 | + }, |
| 56 | + log: noopLogger, |
| 57 | + enqueueSystemEvent: vi.fn(), |
| 58 | + requestHeartbeatNow: vi.fn(), |
| 59 | + runIsolatedAgentJob, |
| 60 | + sendCronFailureAlert, |
| 61 | + }); |
| 62 | + |
| 63 | + await cron.start(); |
| 64 | + const job = await cron.add({ |
| 65 | + name: "daily report", |
| 66 | + enabled: true, |
| 67 | + schedule: { kind: "every", everyMs: 60_000 }, |
| 68 | + sessionTarget: "isolated", |
| 69 | + wakeMode: "next-heartbeat", |
| 70 | + payload: { kind: "agentTurn", message: "run report" }, |
| 71 | + delivery: { mode: "announce", channel: "telegram", to: "19098680" }, |
| 72 | + }); |
| 73 | + |
| 74 | + await cron.run(job.id, "force"); |
| 75 | + expect(sendCronFailureAlert).not.toHaveBeenCalled(); |
| 76 | + |
| 77 | + await cron.run(job.id, "force"); |
| 78 | + expect(sendCronFailureAlert).toHaveBeenCalledTimes(1); |
| 79 | + expect(sendCronFailureAlert).toHaveBeenLastCalledWith( |
| 80 | + expect.objectContaining({ |
| 81 | + job: expect.objectContaining({ id: job.id }), |
| 82 | + channel: "telegram", |
| 83 | + to: "19098680", |
| 84 | + text: expect.stringContaining('Cron job "daily report" failed 2 times'), |
| 85 | + }), |
| 86 | + ); |
| 87 | + |
| 88 | + await cron.run(job.id, "force"); |
| 89 | + expect(sendCronFailureAlert).toHaveBeenCalledTimes(1); |
| 90 | + |
| 91 | + vi.advanceTimersByTime(60_000); |
| 92 | + await cron.run(job.id, "force"); |
| 93 | + expect(sendCronFailureAlert).toHaveBeenCalledTimes(2); |
| 94 | + expect(sendCronFailureAlert).toHaveBeenLastCalledWith( |
| 95 | + expect.objectContaining({ |
| 96 | + text: expect.stringContaining('Cron job "daily report" failed 4 times'), |
| 97 | + }), |
| 98 | + ); |
| 99 | + |
| 100 | + cron.stop(); |
| 101 | + await store.cleanup(); |
| 102 | + }); |
| 103 | + |
| 104 | + it("supports per-job failure alert override when global alerts are disabled", async () => { |
| 105 | + const store = await makeStorePath(); |
| 106 | + const sendCronFailureAlert = vi.fn(async () => undefined); |
| 107 | + const runIsolatedAgentJob = vi.fn(async () => ({ |
| 108 | + status: "error" as const, |
| 109 | + error: "timeout", |
| 110 | + })); |
| 111 | + |
| 112 | + const cron = new CronService({ |
| 113 | + storePath: store.storePath, |
| 114 | + cronEnabled: true, |
| 115 | + cronConfig: { |
| 116 | + failureAlert: { |
| 117 | + enabled: false, |
| 118 | + }, |
| 119 | + }, |
| 120 | + log: noopLogger, |
| 121 | + enqueueSystemEvent: vi.fn(), |
| 122 | + requestHeartbeatNow: vi.fn(), |
| 123 | + runIsolatedAgentJob, |
| 124 | + sendCronFailureAlert, |
| 125 | + }); |
| 126 | + |
| 127 | + await cron.start(); |
| 128 | + const job = await cron.add({ |
| 129 | + name: "job with override", |
| 130 | + enabled: true, |
| 131 | + schedule: { kind: "every", everyMs: 60_000 }, |
| 132 | + sessionTarget: "isolated", |
| 133 | + wakeMode: "next-heartbeat", |
| 134 | + payload: { kind: "agentTurn", message: "run report" }, |
| 135 | + failureAlert: { |
| 136 | + after: 1, |
| 137 | + channel: "telegram", |
| 138 | + to: "12345", |
| 139 | + cooldownMs: 1, |
| 140 | + }, |
| 141 | + }); |
| 142 | + |
| 143 | + await cron.run(job.id, "force"); |
| 144 | + expect(sendCronFailureAlert).toHaveBeenCalledTimes(1); |
| 145 | + expect(sendCronFailureAlert).toHaveBeenLastCalledWith( |
| 146 | + expect.objectContaining({ |
| 147 | + channel: "telegram", |
| 148 | + to: "12345", |
| 149 | + }), |
| 150 | + ); |
| 151 | + |
| 152 | + cron.stop(); |
| 153 | + await store.cleanup(); |
| 154 | + }); |
| 155 | + |
| 156 | + it("respects per-job failureAlert=false and suppresses alerts", async () => { |
| 157 | + const store = await makeStorePath(); |
| 158 | + const sendCronFailureAlert = vi.fn(async () => undefined); |
| 159 | + const runIsolatedAgentJob = vi.fn(async () => ({ |
| 160 | + status: "error" as const, |
| 161 | + error: "auth error", |
| 162 | + })); |
| 163 | + |
| 164 | + const cron = new CronService({ |
| 165 | + storePath: store.storePath, |
| 166 | + cronEnabled: true, |
| 167 | + cronConfig: { |
| 168 | + failureAlert: { |
| 169 | + enabled: true, |
| 170 | + after: 1, |
| 171 | + }, |
| 172 | + }, |
| 173 | + log: noopLogger, |
| 174 | + enqueueSystemEvent: vi.fn(), |
| 175 | + requestHeartbeatNow: vi.fn(), |
| 176 | + runIsolatedAgentJob, |
| 177 | + sendCronFailureAlert, |
| 178 | + }); |
| 179 | + |
| 180 | + await cron.start(); |
| 181 | + const job = await cron.add({ |
| 182 | + name: "disabled alert job", |
| 183 | + enabled: true, |
| 184 | + schedule: { kind: "every", everyMs: 60_000 }, |
| 185 | + sessionTarget: "isolated", |
| 186 | + wakeMode: "next-heartbeat", |
| 187 | + payload: { kind: "agentTurn", message: "run report" }, |
| 188 | + failureAlert: false, |
| 189 | + }); |
| 190 | + |
| 191 | + await cron.run(job.id, "force"); |
| 192 | + await cron.run(job.id, "force"); |
| 193 | + expect(sendCronFailureAlert).not.toHaveBeenCalled(); |
| 194 | + |
| 195 | + cron.stop(); |
| 196 | + await store.cleanup(); |
| 197 | + }); |
| 198 | +}); |
0 commit comments