From 0b8d4cdaac1c613c27bb15ff9119ec6a72931a1b Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Sat, 15 Oct 2022 17:17:26 +0800 Subject: [PATCH] Generate Next Timeslot for recurring interval --- db/patch-maintenance-table2.sql | 2 ++ server/server.js | 2 ++ server/uptime-kuma-server.js | 25 +++++++++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/db/patch-maintenance-table2.sql b/db/patch-maintenance-table2.sql index 24f6da8c..96b2ebde 100644 --- a/db/patch-maintenance-table2.sql +++ b/db/patch-maintenance-table2.sql @@ -65,6 +65,8 @@ CREATE INDEX [active_timeslot_index] ON [maintenance_timeslot] ( [end_date] DESC ); +CREATE INDEX [generated_next_index] ON [maintenance_timeslot] ([generated_next]); + -- monitor_maintenance CREATE TABLE monitor_maintenance ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, diff --git a/server/server.js b/server/server.js index fb6a0a21..888294b1 100644 --- a/server/server.js +++ b/server/server.js @@ -1721,6 +1721,8 @@ async function shutdownFunction(signal) { log.info("server", "Shutdown requested"); log.info("server", "Called signal: " + signal); + await server.stop(); + log.info("server", "Stopping all monitors"); for (let id in server.monitorList) { let monitor = server.monitorList[id]; diff --git a/server/uptime-kuma-server.js b/server/uptime-kuma-server.js index ac832f8e..f84bf312 100644 --- a/server/uptime-kuma-server.js +++ b/server/uptime-kuma-server.js @@ -45,6 +45,8 @@ class UptimeKumaServer { */ indexHTML = ""; + generateMaintenanceTimeslotsInterval = undefined; + static getInstance(args) { if (UptimeKumaServer.instance == null) { UptimeKumaServer.instance = new UptimeKumaServer(args); @@ -90,6 +92,9 @@ class UptimeKumaServer { dayjs.tz.setDefault(process.env.TZ); log.debug("DEBUG", "Timezone: " + process.env.TZ); log.debug("DEBUG", "Current Time: " + dayjs.tz().format()); + + await this.generateMaintenanceTimeslots(); + this.generateMaintenanceTimeslotsInterval = setInterval(this.generateMaintenanceTimeslots, 60 * 1000); } async sendMonitorList(socket) { @@ -213,8 +218,28 @@ class UptimeKumaServer { process.env.TZ = timezone; dayjs.tz.setDefault(timezone); } + + async generateMaintenanceTimeslots() { + + let list = await R.find("maintenance_timeslot", " generated_next = 0 AND start_date <= DATETIME('now') "); + + for (let maintenanceTimeslot of list) { + let maintenance = await maintenanceTimeslot.maintenance; + await MaintenanceTimeslot.generateTimeslot(maintenance, maintenanceTimeslot.end_date, false); + maintenanceTimeslot.generated_next = true; + await R.store(maintenanceTimeslot); + } + + } + + async stop() { + clearTimeout(this.generateMaintenanceTimeslotsInterval); + } } module.exports = { UptimeKumaServer }; + +// Must be at the end +const MaintenanceTimeslot = require("./model/maintenance_timeslot");