2021-10-09 18:33:47 +03:00
|
|
|
const { log, exit, connectDb } = require("./util-worker");
|
2021-09-27 18:40:38 +03:00
|
|
|
const { R } = require("redbean-node");
|
2021-10-09 18:33:47 +03:00
|
|
|
const { setSetting, setting } = require("../util-server");
|
2021-09-27 18:40:38 +03:00
|
|
|
|
2021-10-09 18:33:47 +03:00
|
|
|
const DEFAULT_KEEP_PERIOD = 30;
|
2021-09-27 18:40:38 +03:00
|
|
|
|
|
|
|
(async () => {
|
2021-10-09 18:33:47 +03:00
|
|
|
await connectDb();
|
|
|
|
|
|
|
|
let period = await setting("keepDataPeriodDays");
|
|
|
|
|
|
|
|
// Set Default Period
|
|
|
|
if (period == null) {
|
2021-10-09 19:16:13 +03:00
|
|
|
await setSetting("keepDataPeriodDays", DEFAULT_KEEP_PERIOD, "general");
|
2021-10-09 18:33:47 +03:00
|
|
|
period = DEFAULT_KEEP_PERIOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try parse setting
|
|
|
|
let parsedPeriod;
|
|
|
|
try {
|
|
|
|
parsedPeriod = parseInt(period);
|
|
|
|
} catch (_) {
|
|
|
|
log("Failed to parse setting, resetting to default..");
|
2021-10-09 19:16:13 +03:00
|
|
|
await setSetting("keepDataPeriodDays", DEFAULT_KEEP_PERIOD, "general");
|
2021-10-09 18:33:47 +03:00
|
|
|
parsedPeriod = DEFAULT_KEEP_PERIOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
log(`Clearing Data older than ${parsedPeriod} days...`);
|
2021-09-27 18:40:38 +03:00
|
|
|
|
2021-10-09 18:33:47 +03:00
|
|
|
try {
|
|
|
|
await R.exec(
|
|
|
|
"DELETE FROM heartbeat WHERE time < DATETIME('now', '-' || ? || ' days') ",
|
|
|
|
[parsedPeriod]
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
log(`Failed to clear old data: ${e.message}`);
|
|
|
|
}
|
2021-09-27 18:40:38 +03:00
|
|
|
|
2021-10-09 18:33:47 +03:00
|
|
|
exit();
|
2021-09-27 18:40:38 +03:00
|
|
|
})();
|