Flush batch logger to not lose info

This commit is contained in:
Daniel García Aubert 2019-04-04 18:15:21 +02:00
parent 12e35b02e8
commit f2b5921ade
2 changed files with 27 additions and 13 deletions

25
app.js
View File

@ -124,20 +124,19 @@ process.on('SIGHUP', function() {
});
process.on('SIGTERM', function () {
global.logger.debug('Signal TERM Received');
listener.close();
global.logger.debug('Http server closed');
listener.close(function () {
server.batch.stop(function () {
server.batch.drain(function (err) {
if (err) {
global.logger.error(err);
return process.exit(1);
}
server.batch.stop();
global.logger.debug('Batch queries stopped');
server.batch.drain(function (err) {
if (err) {
global.logger.error(err);
return process.exit(1);
}
global.logger.debug('Batch queries drained');
global.logger.info('Exit gracefully');
process.exit(0);
global.logger.info('Exit gracefully');
server.batch.logger.end();
process.exit(0);
});
});
});
});

View File

@ -50,6 +50,21 @@ class Logger {
reopenFileStreams () {
this.logger.reopenFileStreams();
}
// Ensures that the writable stream is flushed.
// Use this function before exiting the process to not lose log entries
//
// See: https://github.com/trentm/node-bunyan/issues/37
// See: https://github.com/trentm/node-bunyan/issues/73
end () {
// process.stdout cannot be closed
if (!this.path) {
return;
}
this.logger.streams[0].stream.on('finish', resolve);
this.logger.streams[0].stream.end(); // close stream, flush buffer to disk
}
}
module.exports = Logger;