Skip to content

Commit

Permalink
refactor: maintaing queue for pending alerts before initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
mrrishimeena committed Jul 26, 2024
1 parent 37ad834 commit 0ca6b26
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions lib/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ const Main = {
appName: null,
environmentName: process.env.NODE_ENV || null,
serverName: null,
pendingAlerts: [],
isInitializated: false,

initialize (options) {
this.loadPackageInfo();
this.setupConfiguration(options);
Logs.initialize(options);
this.startServer(options);
this.handleUncaughtExceptions(options.exitOnException);
this.isInitializated = true;
this.flushAlerts();
},

loadPackageInfo () {
Expand Down Expand Up @@ -57,7 +61,11 @@ const Main = {
environmentName: this.environmentName,
serverName: this.serverName
};
await Alerts.customLoggerAlert(message, messageExtraInfo);
if (this.isInitializated) {
await Alerts.customLoggerAlert(message, messageExtraInfo);
} else {
this.pendingAlerts.push({ message, messageExtraInfo });
}
}
},

Expand All @@ -71,11 +79,28 @@ const Main = {
environmentName: this.environmentName,
serverName: this.serverName
};
await Alerts.handleUncaughtExceptions(`${errorOrigin}\n${errorMessage}`, messageExtraInfo);
await Logs.clearLogsBeforeExit();
if (this.isInitializated) {
await Alerts.handleUncaughtExceptions(`${errorOrigin}\n${errorMessage}`, messageExtraInfo);
await Logs.clearLogsBeforeExit();
}
if (exitOnException) process.exit(1);
});
},

async flushAlerts () {
const alertsToFlush = [...this.pendingAlerts];
this.pendingAlerts = [];

try {
for (let i = 0; i < alertsToFlush.length; i++) {
const { message, messageExtraInfo } = alertsToFlush[i];
await Alerts.customLoggerAlert(message, messageExtraInfo);
}
} catch (err) {
console.error('Failed to flush alerts:', err);
}
}

};

module.exports = Main;

0 comments on commit 0ca6b26

Please sign in to comment.