2023-05-12 16:55:48 +00:00
|
|
|
const { UptimeKumaServer } = require("./uptime-kuma-server");
|
|
|
|
const { clearOldData } = require("./jobs/clear-old-data");
|
2023-07-07 20:57:53 +00:00
|
|
|
const { incrementalVacuum } = require("./jobs/incremental-vacuum");
|
2023-05-12 16:55:48 +00:00
|
|
|
const Cron = require("croner");
|
|
|
|
|
2021-09-27 15:40:38 +00:00
|
|
|
const jobs = [
|
|
|
|
{
|
|
|
|
name: "clear-old-data",
|
2023-05-12 16:55:48 +00:00
|
|
|
interval: "14 03 * * *",
|
|
|
|
jobFunc: clearOldData,
|
|
|
|
croner: null,
|
2021-10-23 13:44:21 +00:00
|
|
|
},
|
2023-07-07 20:57:53 +00:00
|
|
|
{
|
|
|
|
name: "incremental-vacuum",
|
|
|
|
interval: "*/5 * * * *",
|
|
|
|
jobFunc: incrementalVacuum,
|
|
|
|
croner: null,
|
|
|
|
}
|
2021-09-27 15:40:38 +00:00
|
|
|
];
|
|
|
|
|
2022-04-20 18:56:40 +00:00
|
|
|
/**
|
|
|
|
* Initialize background jobs
|
2023-05-12 16:55:48 +00:00
|
|
|
* @returns {Promise<void>}
|
2022-04-20 18:56:40 +00:00
|
|
|
*/
|
2023-05-12 16:55:48 +00:00
|
|
|
const initBackgroundJobs = async function () {
|
|
|
|
const timezone = await UptimeKumaServer.getInstance().getTimezone();
|
|
|
|
|
|
|
|
for (const job of jobs) {
|
|
|
|
const cornerJob = new Cron(
|
|
|
|
job.interval,
|
|
|
|
{
|
|
|
|
name: job.name,
|
|
|
|
timezone,
|
|
|
|
},
|
|
|
|
job.jobFunc,
|
|
|
|
);
|
|
|
|
job.croner = cornerJob;
|
|
|
|
}
|
2021-09-27 15:40:38 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2023-08-11 07:46:41 +00:00
|
|
|
/**
|
|
|
|
* Stop all background jobs if running
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2022-04-05 11:41:29 +00:00
|
|
|
const stopBackgroundJobs = function () {
|
2023-05-12 16:55:48 +00:00
|
|
|
for (const job of jobs) {
|
|
|
|
if (job.croner) {
|
|
|
|
job.croner.stop();
|
|
|
|
job.croner = null;
|
|
|
|
}
|
2022-04-05 11:41:29 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-27 15:40:38 +00:00
|
|
|
module.exports = {
|
2022-04-05 11:41:29 +00:00
|
|
|
initBackgroundJobs,
|
|
|
|
stopBackgroundJobs
|
2021-09-27 15:40:38 +00:00
|
|
|
};
|