This commit is contained in:
Xargana 2025-04-30 17:45:07 +03:00
parent 90b05a5c83
commit b33f04886d

View file

@ -6,7 +6,7 @@ class NotificationService {
this.authorizedUserId = process.env.AUTHORIZED_USER_ID; this.authorizedUserId = process.env.AUTHORIZED_USER_ID;
this.statusChannel = null; this.statusChannel = null;
this.checkInterval = options.checkInterval || 5000; // Changed to 5 seconds default this.checkInterval = options.checkInterval || 5000; // Changed to 5 seconds default
this.statusEndpoint = options.statusEndpoint || 'https://blahaj.tr:2589/status'; this.statusEndpoint = options.statusEndpoint || 'https://xargana.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
@ -85,7 +85,7 @@ class NotificationService {
try { try {
const currentStatus = await this.fetchStatus(); const currentStatus = await this.fetchStatus();
// Process current status and apply failure thresholds // Process current status with failure threshold logic for ALL servers and services
const processedStatus = this.processStatusWithThreshold(currentStatus); const processedStatus = this.processStatusWithThreshold(currentStatus);
// Detect changes between previous status and processed status // Detect changes between previous status and processed status
@ -105,66 +105,95 @@ class NotificationService {
} }
processStatusWithThreshold(currentStatus) { processStatusWithThreshold(currentStatus) {
// Create a deep copy of the current status to avoid modifying the original
const processedStatus = { const processedStatus = {
servers: {...currentStatus.servers}, servers: {},
pm2Services: {...currentStatus.pm2Services} pm2Services: {}
}; };
// Process servers // Process ALL servers with failure threshold logic
for (const server in currentStatus.servers) { if (currentStatus.servers) {
if (!currentStatus.servers[server].online) { Object.keys(currentStatus.servers).forEach(server => {
// Initialize counter if it doesn't exist // Copy the server data
if (!this.failureTracking.servers[server]) { processedStatus.servers[server] = {...currentStatus.servers[server]};
// Apply failure threshold logic
if (!currentStatus.servers[server].online) {
// Initialize counter if it doesn't exist
if (!this.failureTracking.servers[server]) {
this.failureTracking.servers[server] = 0;
}
// Increment failures counter
this.failureTracking.servers[server]++;
// If failures haven't reached threshold, keep it as online in the processed status
if (this.failureTracking.servers[server] < this.failureThreshold) {
processedStatus.servers[server].online = true; // Keep it as online until threshold reached
console.log(`Server ${server} failure count: ${this.failureTracking.servers[server]}/${this.failureThreshold}`);
} else {
console.log(`Server ${server} marked offline after ${this.failureThreshold} consecutive failures`);
}
} else {
// Reset counter if the server is online
this.failureTracking.servers[server] = 0; this.failureTracking.servers[server] = 0;
} }
});
// Increment failures counter
this.failureTracking.servers[server]++;
// If failures haven't reached threshold, keep it as online in the processed status
if (this.failureTracking.servers[server] < this.failureThreshold) {
processedStatus.servers[server] = {
...currentStatus.servers[server],
online: true // Keep it as online until threshold reached
};
console.log(`Server ${server} failure count: ${this.failureTracking.servers[server]}/${this.failureThreshold}`);
} else {
console.log(`Server ${server} marked offline after ${this.failureThreshold} consecutive failures`);
}
} else {
// Reset counter if the server is online
this.failureTracking.servers[server] = 0;
}
} }
// Process PM2 services // Process ALL PM2 services with failure threshold logic
for (const service in currentStatus.pm2Services) { if (currentStatus.pm2Services) {
if (currentStatus.pm2Services[service].status !== 'online') { Object.keys(currentStatus.pm2Services).forEach(service => {
// Initialize counter if it doesn't exist // Copy the service data
if (!this.failureTracking.pm2Services[service]) { processedStatus.pm2Services[service] = {...currentStatus.pm2Services[service]};
// Apply failure threshold logic
if (currentStatus.pm2Services[service].status !== 'online') {
// Initialize counter if it doesn't exist
if (!this.failureTracking.pm2Services[service]) {
this.failureTracking.pm2Services[service] = 0;
}
// Increment failures counter
this.failureTracking.pm2Services[service]++;
// If failures haven't reached threshold, keep it as online in the processed status
if (this.failureTracking.pm2Services[service] < this.failureThreshold) {
processedStatus.pm2Services[service].status = 'online'; // Keep it as online until threshold reached
console.log(`Service ${service} failure count: ${this.failureTracking.pm2Services[service]}/${this.failureThreshold}`);
} else {
console.log(`Service ${service} marked as ${currentStatus.pm2Services[service].status} after ${this.failureThreshold} consecutive failures`);
}
} else {
// Reset counter if the service is online
this.failureTracking.pm2Services[service] = 0; this.failureTracking.pm2Services[service] = 0;
} }
});
// Increment failures counter }
this.failureTracking.pm2Services[service]++;
// Clean up tracking for removed servers and services
// If failures haven't reached threshold, keep it as online in the processed status this.cleanupFailureTracking(currentStatus);
if (this.failureTracking.pm2Services[service] < this.failureThreshold) {
processedStatus.pm2Services[service] = { return processedStatus;
...currentStatus.pm2Services[service], }
status: 'online' // Keep it as online until threshold reached
}; // Remove tracking entries for servers/services that no longer exist
console.log(`Service ${service} failure count: ${this.failureTracking.pm2Services[service]}/${this.failureThreshold}`); cleanupFailureTracking(currentStatus) {
} else { // Clean up servers tracking
console.log(`Service ${service} marked as ${currentStatus.pm2Services[service].status} after ${this.failureThreshold} consecutive failures`); for (const server in this.failureTracking.servers) {
} if (!currentStatus.servers || !currentStatus.servers[server]) {
} else { delete this.failureTracking.servers[server];
// Reset counter if the service is online console.log(`Removed tracking for non-existent server: ${server}`);
this.failureTracking.pm2Services[service] = 0;
} }
} }
return processedStatus; // Clean up services tracking
for (const service in this.failureTracking.pm2Services) {
if (!currentStatus.pm2Services || !currentStatus.pm2Services[service]) {
delete this.failureTracking.pm2Services[service];
console.log(`Removed tracking for non-existent service: ${service}`);
}
}
} }
detectChanges(previousStatus, currentStatus) { detectChanges(previousStatus, currentStatus) {
@ -242,7 +271,7 @@ class NotificationService {
timestamp: new Date(), timestamp: new Date(),
fields: [], fields: [],
footer: { footer: {
text: 'blahaj.tr Status Monitor' text: 'xargana.tr Status Monitor'
} }
}; };
@ -260,7 +289,7 @@ 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') { if (change.responseTime !== undefined && change.responseTime !== 'unknown') {
fieldContent += `\nResponse time: ${change.responseTime}ms`; fieldContent += `\nResponse time: ${change.responseTime}ms`;
} }
} }
@ -292,9 +321,13 @@ class NotificationService {
// Add resource usage if available // Add resource usage if available
if (change.details) { if (change.details) {
const memory = change.details.memory ? Math.round(change.details.memory / (1024 * 1024) * 10) / 10 : 0; if (change.details.memory !== undefined) {
fieldContent += `\nCPU: ${change.details.cpu}% | Memory: ${memory}MB`; const memory = Math.round(change.details.memory / (1024 * 1024) * 10) / 10;
fieldContent += `\nUptime: ${Math.floor(change.details.uptime / 1000)}s | Restarts: ${change.details.restarts}`; fieldContent += `\nCPU: ${change.details.cpu || 0}% | Memory: ${memory}MB`;
}
if (change.details.uptime !== undefined) {
fieldContent += `\nUptime: ${Math.floor(change.details.uptime / 1000)}s | Restarts: ${change.details.restarts || 0}`;
}
} }
} }
} }