Skip to content

Commit

Permalink
refactor: flushing logs before exit on uncaughtException
Browse files Browse the repository at this point in the history
  • Loading branch information
mrrishimeena committed Jul 16, 2024
1 parent 3d545c1 commit 26058a8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const Main = {
serverName: this.serverName
};
await Alerts.handleUncaughtExceptions(`${errorOrigin}\n${errorMessage}`, messageExtraInfo);
await Logs.clearLogsBeforeExit();
if (exitOnException) process.exit(1);
});
}
Expand Down
11 changes: 11 additions & 0 deletions lib/main/logs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,15 @@ CollectLogsHook.logStream = new stream.Writable({
}
});

CollectLogsHook.clearLogsBeforeExit = async function (timeout = 5000) {
if (typeof this.storage.flushLogs === 'function') {
try {
await Promise.race([
this.storage.flushLogs(),
new Promise((resolve, reject) => setTimeout(() => reject(new Error('flushLogs timed out')), timeout))
]);
} catch (err) { }
}
};

module.exports = CollectLogsHook;

0 comments on commit 26058a8

Please sign in to comment.