This repository has been archived on 2022-10-07. You can view files and clone it, but cannot push or open issues or pull requests.
skynet-webportal/packages/health-check/src/schedule.js

46 lines
1.6 KiB
JavaScript
Raw Normal View History

const { CronJob } = require("cron");
const ms = require("ms");
const db = require("./db");
2020-09-09 12:25:00 +00:00
const { criticalChecks } = require("./checks/critical");
const { verboseChecks } = require("./checks/verbose");
// use this timezone to run all cron instances at the same time regardless of the server location
const timezone = "UTC";
// critical health checks job definition
const criticalJobSchedule = "*/5 * * * *"; // on every 5 minute mark
const criticalJobOnTick = async () => {
2020-09-10 12:13:28 +00:00
const entry = {
date: new Date().toISOString(),
checks: await Promise.all(criticalChecks.map((check) => new Promise(check))),
};
// read before writing to make sure no external changes are overwritten
2020-09-18 11:40:14 +00:00
db.read().get("critical").push(entry).write();
};
const criticalJob = new CronJob(criticalJobSchedule, criticalJobOnTick, null, false, timezone);
// verbose health checks job definition
const verboseJobSchedule = "0 * * * *"; // on every full hour mark
const verboseJobOnTick = async () => {
2020-09-10 12:13:28 +00:00
const entry = {
date: new Date().toISOString(),
checks: await Promise.all(verboseChecks.map((check) => new Promise(check))),
};
// read before writing to make sure no external changes are overwritten
2020-09-18 11:40:14 +00:00
db.read().get("verbose").push(entry).write();
};
const verboseJob = new CronJob(verboseJobSchedule, verboseJobOnTick, null, false, timezone);
// fire all health checks on startup (with delay for other services to boot)
2020-07-30 10:00:58 +00:00
setTimeout(() => {
// fire first run manually
criticalJob.fireOnTick();
verboseJob.fireOnTick();
// start cron schedule
criticalJob.start();
verboseJob.start();
}, ms("1 minute"));