a
This commit is contained in:
parent
88071c4320
commit
94a9f8f91f
|
@ -57,7 +57,7 @@ class Bot {
|
||||||
|
|
||||||
// Initialize and start the notification service
|
// Initialize and start the notification service
|
||||||
this.notificationService = new NotificationService(this.client, {
|
this.notificationService = new NotificationService(this.client, {
|
||||||
checkInterval: process.env.STATUS_CHECK_INTERVAL ? parseInt(process.env.STATUS_CHECK_INTERVAL) : 60000,
|
checkInterval: process.env.STATUS_CHECK_INTERVAL ? parseInt(process.env.STATUS_CHECK_INTERVAL) : 5000,
|
||||||
statusEndpoint: process.env.STATUS_ENDPOINT || 'https://blahaj.tr:2589/status'
|
statusEndpoint: process.env.STATUS_ENDPOINT || 'https://blahaj.tr:2589/status'
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -5,14 +5,14 @@ class NotificationService {
|
||||||
this.client = client;
|
this.client = client;
|
||||||
this.authorizedUserId = process.env.AUTHORIZED_USER_ID;
|
this.authorizedUserId = process.env.AUTHORIZED_USER_ID;
|
||||||
this.statusChannel = null;
|
this.statusChannel = null;
|
||||||
this.checkInterval = options.checkInterval || 60000; // Default: check every minute
|
this.checkInterval = options.checkInterval || 10000; // Changed to 10 seconds default
|
||||||
this.statusEndpoint = options.statusEndpoint || 'https://blahaj.tr:2589/status';
|
this.statusEndpoint = options.statusEndpoint || 'https://blahaj.tr:2589/status';
|
||||||
this.notificationChannelId = process.env.STATUS_NOTIFICATION_CHANNEL;
|
this.notificationChannelId = process.env.STATUS_NOTIFICATION_CHANNEL;
|
||||||
|
|
||||||
// Store the previous status to compare for changes
|
// Store the previous status to compare for changes
|
||||||
this.previousStatus = {
|
this.previousStatus = {
|
||||||
servers: {},
|
servers: {},
|
||||||
services: {}
|
pm2Services: {} // Changed from services to pm2Services to match API response
|
||||||
};
|
};
|
||||||
|
|
||||||
// Track if this is the first check (to avoid notifications on startup)
|
// Track if this is the first check (to avoid notifications on startup)
|
||||||
|
@ -104,7 +104,8 @@ class NotificationService {
|
||||||
name: server,
|
name: server,
|
||||||
status: currentStatus.servers[server].online ? 'online' : 'offline',
|
status: currentStatus.servers[server].online ? 'online' : 'offline',
|
||||||
previous: previousStatus.servers[server]?.online ? 'online' : 'offline',
|
previous: previousStatus.servers[server]?.online ? 'online' : 'offline',
|
||||||
isNew: !previousStatus.servers[server]
|
isNew: !previousStatus.servers[server],
|
||||||
|
responseTime: currentStatus.servers[server].responseTime
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -122,29 +123,30 @@ class NotificationService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for PM2 service status changes
|
// Check for PM2 service status changes - updated to use pm2Services
|
||||||
if (previousStatus.services && currentStatus.services) {
|
if (previousStatus.pm2Services && currentStatus.pm2Services) {
|
||||||
for (const service in currentStatus.services) {
|
for (const service in currentStatus.pm2Services) {
|
||||||
if (!previousStatus.services[service] ||
|
if (!previousStatus.pm2Services[service] ||
|
||||||
previousStatus.services[service].status !== currentStatus.services[service].status) {
|
previousStatus.pm2Services[service].status !== currentStatus.pm2Services[service].status) {
|
||||||
changes.push({
|
changes.push({
|
||||||
type: 'service',
|
type: 'service',
|
||||||
name: service,
|
name: service,
|
||||||
status: currentStatus.services[service].status,
|
status: currentStatus.pm2Services[service].status,
|
||||||
previous: previousStatus.services[service]?.status || 'unknown',
|
previous: previousStatus.pm2Services[service]?.status || 'unknown',
|
||||||
isNew: !previousStatus.services[service]
|
isNew: !previousStatus.pm2Services[service],
|
||||||
|
details: currentStatus.pm2Services[service]
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for removed services
|
// Check for removed services
|
||||||
for (const service in previousStatus.services) {
|
for (const service in previousStatus.pm2Services) {
|
||||||
if (!currentStatus.services[service]) {
|
if (!currentStatus.pm2Services[service]) {
|
||||||
changes.push({
|
changes.push({
|
||||||
type: 'service',
|
type: 'service',
|
||||||
name: service,
|
name: service,
|
||||||
status: 'removed',
|
status: 'removed',
|
||||||
previous: previousStatus.services[service].status
|
previous: previousStatus.pm2Services[service].status
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -181,6 +183,9 @@ class NotificationService {
|
||||||
fieldContent = `⚪ Server removed (was ${previousEmoji} **${change.previous}**)`;
|
fieldContent = `⚪ Server removed (was ${previousEmoji} **${change.previous}**)`;
|
||||||
} else {
|
} else {
|
||||||
fieldContent = `${previousEmoji} **${change.previous}** → ${statusEmoji} **${change.status}**`;
|
fieldContent = `${previousEmoji} **${change.previous}** → ${statusEmoji} **${change.status}**`;
|
||||||
|
if (change.responseTime !== 'unknown') {
|
||||||
|
fieldContent += `\nResponse time: ${change.responseTime}ms`;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if (change.type === 'service') {
|
} else if (change.type === 'service') {
|
||||||
let statusEmoji = '⚪';
|
let statusEmoji = '⚪';
|
||||||
|
@ -207,6 +212,13 @@ class NotificationService {
|
||||||
fieldContent = `⚪ Service removed (was ${previousEmoji} **${change.previous}**)`;
|
fieldContent = `⚪ Service removed (was ${previousEmoji} **${change.previous}**)`;
|
||||||
} else {
|
} else {
|
||||||
fieldContent = `${previousEmoji} **${change.previous}** → ${statusEmoji} **${change.status}**`;
|
fieldContent = `${previousEmoji} **${change.previous}** → ${statusEmoji} **${change.status}**`;
|
||||||
|
|
||||||
|
// Add resource usage if available
|
||||||
|
if (change.details) {
|
||||||
|
const memory = change.details.memory ? Math.round(change.details.memory / (1024 * 1024) * 10) / 10 : 0;
|
||||||
|
fieldContent += `\nCPU: ${change.details.cpu}% | Memory: ${memory}MB`;
|
||||||
|
fieldContent += `\nUptime: ${Math.floor(change.details.uptime / 1000)}s | Restarts: ${change.details.restarts}`;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -218,9 +230,9 @@ class NotificationService {
|
||||||
});
|
});
|
||||||
|
|
||||||
// Add a detailed status field if there are many services
|
// Add a detailed status field if there are many services
|
||||||
if (Object.keys(currentStatus.services || {}).length > 0) {
|
if (Object.keys(currentStatus.pm2Services || {}).length > 0) {
|
||||||
let servicesStatus = '';
|
let servicesStatus = '';
|
||||||
for (const [name, info] of Object.entries(currentStatus.services)) {
|
for (const [name, info] of Object.entries(currentStatus.pm2Services)) {
|
||||||
let statusEmoji = '⚪';
|
let statusEmoji = '⚪';
|
||||||
switch (info.status) {
|
switch (info.status) {
|
||||||
case 'online': statusEmoji = '🟢'; break;
|
case 'online': statusEmoji = '🟢'; break;
|
||||||
|
|
Loading…
Reference in a new issue