Nelson Chan
3 years ago
5 changed files with 90 additions and 18 deletions
@ -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(); |
|||
})(); |
|||
|
@ -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, |
|||
}; |
Loading…
Reference in new issue