diff options
Diffstat (limited to 'server/jobs.js')
-rw-r--r-- | server/jobs.js | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/server/jobs.js b/server/jobs.js new file mode 100644 index 0000000..0838731 --- /dev/null +++ b/server/jobs.js @@ -0,0 +1,58 @@ +const { UptimeKumaServer } = require("./uptime-kuma-server"); +const { clearOldData } = require("./jobs/clear-old-data"); +const { incrementalVacuum } = require("./jobs/incremental-vacuum"); +const Cron = require("croner"); + +const jobs = [ + { + name: "clear-old-data", + interval: "14 03 * * *", + jobFunc: clearOldData, + croner: null, + }, + { + name: "incremental-vacuum", + interval: "*/5 * * * *", + jobFunc: incrementalVacuum, + croner: null, + } +]; + +/** + * Initialize background jobs + * @returns {Promise<void>} + */ +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; + } + +}; + +/** + * Stop all background jobs if running + * @returns {void} + */ +const stopBackgroundJobs = function () { + for (const job of jobs) { + if (job.croner) { + job.croner.stop(); + job.croner = null; + } + } +}; + +module.exports = { + initBackgroundJobs, + stopBackgroundJobs +}; |