Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent the engine from exiting on non-critical SMTP errors from the logger #1109

Merged
merged 7 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

All changes that impact users of this module are documented in this file, in the [Common Changelog](https://common-changelog.org) format with some additional specifications defined in the CONTRIBUTING file. This codebase adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased [patch]

> Development of this release was supported by the [French Ministry for Foreign Affairs](https://www.diplomatie.gouv.fr/fr/politique-etrangere-de-la-france/diplomatie-numerique/) through its ministerial [State Startups incubator](https://beta.gouv.fr/startups/open-terms-archive.html) under the aegis of the Ambassador for Digital Affairs.

### Fixed

- Prevent the engine from exiting on non-critical SMTP errors from the logger
Ndpnt marked this conversation as resolved.
Show resolved Hide resolved

## 2.2.1 - 2024-06-07

_Full changeset and discussions: [#1088](https://github.com/OpenTermsArchive/engine/pull/1088)._
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
"simple-git": "^3.8.0",
"swagger-jsdoc": "^6.2.8",
"swagger-ui-express": "^5.0.0",
"winston": "^3.3.3",
"winston": "^3.9.0",
"winston-mail": "^2.0.0"
},
"devDependencies": {
Expand Down
25 changes: 18 additions & 7 deletions src/logger/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,25 @@ const alignedWithColorsAndTime = combine(
}),
);

const consoleTransport = new winston.transports.Console(({ silent: process.env.NODE_ENV === 'test' }));
const consoleTransport = new winston.transports.Console({ silent: process.env.NODE_ENV === 'test' });

const transports = [consoleTransport];

const logger = winston.createLogger({
format: alignedWithColorsAndTime,
transports,
rejectionHandlers: transports,
exitOnError: true,
});

logger.on('error', err => {
if ('smtp' in err) { // Check if err has an `smtp` property, even if it's undefined
logger.warn({ message: `Uncaught exception from SMTP mailer detected and treated as an operational error; process will continue running:\n${err.stack}` });

return; // Prevent process exit
}

return process.exit(1); // Exit process for other errors
});

if (config.get('@opentermsarchive/engine.logger.sendMailOnError')) {
Expand All @@ -51,7 +62,6 @@ if (config.get('@opentermsarchive/engine.logger.sendMailOnError')) {
ssl: true,
timeout: 30 * 1000,
formatter: args => args[Object.getOwnPropertySymbols(args)[1]], // Returns the full error message, the same visible in the console. It is referenced in the argument object with a Symbol of which we do not have the reference but we know it is the second one.
exitOnError: true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why didn't we simply implement the whole thing by removing this line? 🤔 😅

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we remove this line while keeping rejectionHandlers defined, Winston will catch all uncaughtException events without exiting the process, which is not the intended behavior. By default, both Node.js and Winston terminate the process when an uncaughtException occurs, as these are considered programmer errors. And this is the correct approach. As explained here, crashing immediately is the best way to handle programmer errors.

};

transports.push(new winston.transports.Mail({
Expand All @@ -67,14 +77,15 @@ if (config.get('@opentermsarchive/engine.logger.sendMailOnError')) {
subject: `[OTA] Inaccessible content — ${os.hostname()}`,
}));
}

logger.configure({
transports,
rejectionHandlers: transports,
});
}
}

logger.configure({
transports,
rejectionHandlers: transports,
exitOnError: true,
});

let recordedSnapshotsCount;
let recordedVersionsCount;

Expand Down
Loading