diff --git a/server/jobs.js b/server/jobs.js index 18460a1a6..d8435ba37 100644 --- a/server/jobs.js +++ b/server/jobs.js @@ -5,7 +5,7 @@ const { SHARE_ENV } = require("worker_threads"); const jobs = [ { name: "clear-old-data", - interval: "every 1 minute", + interval: "every 1 day", } ]; @@ -17,6 +17,9 @@ const initBackgroundJobs = function (args) { env: SHARE_ENV, workerData: args, }, + workerMessageHandler: (message) => { + console.log("[Background Job]:", message); + } }); bree.start(); diff --git a/server/jobs/clear-old-data.js b/server/jobs/clear-old-data.js index 5b109da05..2b1ed47cb 100644 --- a/server/jobs/clear-old-data.js +++ b/server/jobs/clear-old-data.js @@ -1,24 +1,40 @@ -const path = require("path"); +const { log, exit, connectDb } = require("./util-worker"); const { R } = require("redbean-node"); -const Database = require("../database"); +const { setSetting, setting } = require("../util-server"); -const dbPath = path.join( - process.env.DATA_DIR || - require("worker_threads").workerData["data-dir"] || - "./data/" -); - -Database.init({ - "data-dir": dbPath, -}); +const DEFAULT_KEEP_PERIOD = 30; (async () => { - await Database.connect(); + await connectDb(); + + let period = await setting("keepDataPeriodDays"); + + // Set Default Period + if (period == null) { + await setSetting("keepDataPeriodDays", DEFAULT_KEEP_PERIOD); + period = DEFAULT_KEEP_PERIOD; + } + + // Try parse setting + let parsedPeriod; + try { + parsedPeriod = parseInt(period); + } catch (_) { + log("Failed to parse setting, resetting to default.."); + await setSetting("keepDataPeriodDays", DEFAULT_KEEP_PERIOD); + parsedPeriod = DEFAULT_KEEP_PERIOD; + } + + log(`Clearing Data older than ${parsedPeriod} days...`); - console.log(await R.getAll("PRAGMA journal_mode")); - console.log( - await R.getAll("SELECT * from setting WHERE key = 'database_version'") - ); + try { + await R.exec( + "DELETE FROM heartbeat WHERE time < DATETIME('now', '-' || ? || ' days') ", + [parsedPeriod] + ); + } catch (e) { + log(`Failed to clear old data: ${e.message}`); + } - process.exit(0); + exit(); })(); diff --git a/server/jobs/util-worker.js b/server/jobs/util-worker.js new file mode 100644 index 000000000..9426840d7 --- /dev/null +++ b/server/jobs/util-worker.js @@ -0,0 +1,39 @@ +const { parentPort, workerData } = require("worker_threads"); +const Database = require("../database"); +const path = require("path"); + +const log = function (any) { + if (parentPort) { + parentPort.postMessage(any); + } +}; + +const exit = function (error) { + if (error && error != 0) { + process.exit(error); + } else { + if (parentPort) { + parentPort.postMessage("done"); + } else { + process.exit(0); + } + } +}; + +const connectDb = async function () { + const dbPath = path.join( + process.env.DATA_DIR || workerData["data-dir"] || "./data/" + ); + + Database.init({ + "data-dir": dbPath, + }); + + await Database.connect(); +}; + +module.exports = { + log, + exit, + connectDb, +}; diff --git a/src/languages/en.js b/src/languages/en.js index 2ce8f46be..31eaee0e0 100644 --- a/src/languages/en.js +++ b/src/languages/en.js @@ -281,4 +281,6 @@ export default { promosmsPhoneNumber: "Phone number (for Polish recipient You can skip area codes)", promosmsSMSSender: "SMS Sender Name : Pre-registred name or one of defaults: InfoSMS, SMS Info, MaxSMS, INFO, SMS", // End notification form + "Monitor History": "Monitor History", + clearDataOlderThan: "Remove monitor data older than {0} days", }; diff --git a/src/pages/Settings.vue b/src/pages/Settings.vue index 259f334b7..b5f0f7a71 100644 --- a/src/pages/Settings.vue +++ b/src/pages/Settings.vue @@ -212,6 +212,14 @@ {{ importAlert }} +