Created
June 12, 2025 12:54
-
-
Save louislam/b7570a09f59b5d5aa2885f1711475bbd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For https://github.com/louislam/uptime-kuma/issues/5872 | |
const {Cron} = require("croner"); | |
const fibonacci = (n) => n < 1 ? 0 : n <= 2 ? 1 : fibonacci(n - 1) + fibonacci(n - 2); | |
function main() { | |
new CronerTester("*/5 * * * * *", {}, "Task1"); | |
new CronerTester("* * * * * *", { interval: 5 }, "Task2"); | |
setInterval(() => { | |
console.time("fibonacci"); | |
fibonacci(43); | |
console.timeEnd("fibonacci"); | |
}, 1000) | |
} | |
class CronerTester { | |
expectedNextRuns = []; | |
constructor(cronExpression, options, name) { | |
const cron = new Cron(cronExpression, options, () => { | |
const actualRunTime = new Date(); | |
//greenLog(`[${name}] CronJob started at ${actualRunTime.toLocaleTimeString()}`); | |
const nextRuns = cron.nextRuns(2); | |
if (this.expectedNextRuns.length > 0) { | |
// Check if it call started at the expected time | |
const expectedTimestamp = Math.floor(this.expectedNextRuns[0].getTime() / 1000) | |
const actualTimestamp = Math.floor(actualRunTime.getTime() / 1000); | |
if (expectedTimestamp !== actualTimestamp) { | |
warnLog(`[${name}] Delayed, expected: ${this.expectedNextRuns[0].toLocaleTimeString()}`); | |
} | |
// Check if the next run time is drifting | |
if (nextRuns[0].getTime() !== this.expectedNextRuns[1].getTime()) { | |
errorLog(`[${name}] Next run time is drifting to ${nextRuns[0].toLocaleTimeString()} from ${this.expectedNextRuns[1].toLocaleTimeString()}`); | |
} | |
} | |
this.expectedNextRuns = nextRuns; | |
//infoLog(`[${name}] Next runs:`, this.expectedNextRuns.map((date) => date.toLocaleTimeString()).join(", ")); | |
}); | |
} | |
} | |
// Red Color console.log | |
function errorLog(...args) { | |
console.error("\x1b[31m", ...args, "\x1b[0m"); | |
} | |
// Yellow Color console.log | |
function warnLog(...args) { | |
console.warn("\x1b[33m", ...args, "\x1b[0m"); | |
} | |
// Grey Color console.log | |
function infoLog(...args) { | |
console.info("\x1b[90m", ...args, "\x1b[0m"); | |
} | |
// Green Color console.log | |
function greenLog(...args) { | |
console.log("\x1b[32m", ...args, "\x1b[0m"); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment