-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.ts
More file actions
57 lines (43 loc) · 1.39 KB
/
index.ts
File metadata and controls
57 lines (43 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import pino from 'pino';
import cron from 'node-cron';
import { env } from "./env";
const logger = pino({ name: 'anomaly-worker', level: env.LOG_LEVEL });
let isRunning = false;
let isShuttingDown = false;
async function runDetectionCycle() {
if (isRunning || isShuttingDown) {
return;
}
isRunning = true;
logger.info("Starting anomaly detection cycle...");
try {
const { NotificationsService } = await import('@monitor-app/app/server/domain/notifications/service');
const service = new NotificationsService();
await service.notifyNewAnomalies();
logger.info("Anomaly detection cycle finished.");
} catch (error) {
logger.error({ err: error }, "Error during detection cycle.");
} finally {
isRunning = false;
}
}
const task = cron.schedule('30 * * * *', runDetectionCycle);
logger.info("Anomaly worker scheduled (30 * * * *).");
runDetectionCycle();
process.on('SIGTERM', async () => {
isShuttingDown = true;
task.stop();
if (isRunning) {
logger.info("Waiting for the current detection cycle to complete...");
const shutdownTimeout = setTimeout(() => {
logger.error("Shutdown timed out; forcing exit.");
process.exit(1);
}, 20000);
while (isRunning) {
await new Promise(resolve => setTimeout(resolve, 500));
}
clearTimeout(shutdownTimeout);
}
logger.info("Shutdown complete.");
process.exit(0);
});