34 lines
1.1 KiB
JavaScript
34 lines
1.1 KiB
JavaScript
const { fetchAppSettings } = require('#src/data/app-settings');
|
|
|
|
const TASK = {
|
|
key: 'audit-log-sweep',
|
|
title: 'Audit log cleanup',
|
|
category: 'cleanup',
|
|
intervalMs: 24 * 60 * 60 * 1000
|
|
};
|
|
|
|
function registerAuditLogSweepTask(options) {
|
|
const backgroundTaskQueue = options && options.backgroundTaskQueue;
|
|
const pool = options && options.pool;
|
|
if (!backgroundTaskQueue || !pool) {
|
|
throw new Error('registerAuditLogSweepTask requires audit cleanup dependencies.');
|
|
}
|
|
backgroundTaskQueue.registerRecurringTask({
|
|
key: TASK.key,
|
|
title: TASK.title,
|
|
category: TASK.category,
|
|
intervalMs: TASK.intervalMs,
|
|
metadata: {},
|
|
run: async function () {
|
|
const settings = await fetchAppSettings(pool);
|
|
const retentionDays = Number(settings['audit.retention_days']);
|
|
if (!Number.isInteger(retentionDays) || retentionDays <= 0) {
|
|
return;
|
|
}
|
|
const cutoff = new Date(Date.now() - retentionDays * 24 * 60 * 60 * 1000);
|
|
await pool.query('DELETE FROM o_audit_events WHERE occurred_at < ?', [cutoff]);
|
|
}
|
|
});
|
|
}
|
|
|
|
module.exports = { registerAuditLogSweepTask }; |