Files
fredy/lib/services/demoCleanup.js

38 lines
1004 B
JavaScript
Raw Normal View History

import { setInterval } from 'node:timers';
2025-05-16 13:20:54 +02:00
import { removeJobsByUserName } from './storage/jobStorage.js';
import { config } from '../utils.js';
import { getUsers } from './storage/userStorage.js';
/**
* if we are running in demo environment, we have to cleanup the db files (specifically the jobs table)
*/
export function cleanupDemoAtMidnight() {
2025-05-16 13:20:54 +02:00
const now = new Date();
const millisUntilMidnightUTC =
(24 - now.getUTCHours()) * 60 * 60 * 1000 -
now.getUTCMinutes() * 60 * 1000 -
now.getUTCSeconds() * 1000 -
now.getUTCMilliseconds();
2025-05-16 13:26:39 +02:00
cleanup();
2025-05-16 13:20:54 +02:00
setTimeout(() => {
setInterval(
() => {
cleanup();
},
24 * 60 * 60 * 1000,
);
}, millisUntilMidnightUTC);
}
2025-05-16 13:20:54 +02:00
function cleanup() {
if (config.demoMode) {
const demoUser = getUsers(false).find((user) => user.username === 'demo');
if (demoUser == null) {
console.error('Demo user not found, cannot remove Jobs');
return;
}
2025-05-16 13:20:54 +02:00
removeJobsByUserName(demoUser.id);
}
}