forked from ChickenAI/multizlogin
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathhealthCheck.js
More file actions
38 lines (34 loc) · 1.21 KB
/
healthCheck.js
File metadata and controls
38 lines (34 loc) · 1.21 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
import { zaloAccounts } from './api/zalo/zalo.js';
import { getWebhookConfig, triggerN8nWebhook } from './helpers.js';
const HEALTH_CHECK_INTERVAL = 30 * 60 * 1000; // 30 minutes
async function checkSessions() {
console.log('Running health check for all accounts...');
for (const account of zaloAccounts) {
try {
const info = await account.api.fetchAccountInfo();
if (!info || !info.profile) {
throw new Error('Session lost');
}
} catch (error) {
console.error(`Account ${account.ownId} is LOGOUT or encountered error:`, error.message);
await reportLogout(account.ownId);
}
}
}
async function reportLogout(ownId) {
const config = getWebhookConfig(ownId);
if (config && config.url) {
await triggerN8nWebhook({
action: 'session_logout',
ownId: ownId,
status: 'LOGOUT',
timestamp: new Date().toISOString()
}, config.url);
}
}
export function startHealthCheck() {
console.log('Health Check worker started');
setInterval(checkSessions, HEALTH_CHECK_INTERVAL);
// Run once on start after 5 seconds
setTimeout(checkSessions, 5000);
}