Skip to content

Commit 1d99fc9

Browse files
committed
using cron to run demo cleanup every day at midnight
1 parent 28f0a16 commit 1d99fc9

File tree

1 file changed

+2
-17
lines changed

1 file changed

+2
-17
lines changed

lib/services/demoCleanup.js

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,14 @@
1-
import { setInterval } from 'node:timers';
21
import { removeJobsByUserId } from './storage/jobStorage.js';
32
import { config } from '../utils.js';
43
import { getUsers } from './storage/userStorage.js';
54
import logger from './logger.js';
5+
import cron from 'node-cron';
66

77
/**
88
* if we are running in demo environment, we have to cleanup the db files (specifically the jobs table)
99
*/
1010
export function cleanupDemoAtMidnight() {
11-
const now = new Date();
12-
const millisUntilMidnightUTC =
13-
(24 - now.getUTCHours()) * 60 * 60 * 1000 -
14-
now.getUTCMinutes() * 60 * 1000 -
15-
now.getUTCSeconds() * 1000 -
16-
now.getUTCMilliseconds();
17-
18-
cleanup();
19-
setTimeout(() => {
20-
setInterval(
21-
() => {
22-
cleanup();
23-
},
24-
24 * 60 * 60 * 1000,
25-
);
26-
}, millisUntilMidnightUTC);
11+
cron.schedule('0 0 * * *', cleanup);
2712
}
2813

2914
function cleanup() {

0 commit comments

Comments
 (0)