From f207c1a1fe5f757639c93dadefb02a0a5ee5d6f7 Mon Sep 17 00:00:00 2001 From: stopal Date: Fri, 9 Sep 2016 13:34:04 +0100 Subject: [PATCH 1/2] conn.write callback for buffer drain event --- README.md | 11 +++++------ src/logger.js | 22 ++++++++++++++++------ 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 2cb5ff6..88276c6 100644 --- a/README.md +++ b/README.md @@ -187,7 +187,7 @@ These are events inherited from `Writable`. DEPRECATED. Use `buffer drain` event instead. #### `'buffer drain'` -This event is emitted when the underlying ring buffer is fully consumed and becomes empty. +This event is emitted when the underlying ring buffer is fully consumed and Socket.write callback called. This can be useful when it’s time for the application to terminate but you want to be sure any pending logs have finished writing. @@ -195,11 +195,10 @@ to be sure any pending logs have finished writing. process.on('SIGINT', () => { logger.notice({ type: 'server', event: 'shutdown' }); logger.once('buffer drain', () => { - logger.end(() => { - setTimeout(() => { - process.exit(0); - }, 2000); // this delay gives sometime for the underlying connection to flush its queue - }); + logger.closeConnection(); + logger.on('disconnected', () => { + process.exit(); + }); }); }); ``` diff --git a/src/logger.js b/src/logger.js index 3f3d8de..3bbd638 100644 --- a/src/logger.js +++ b/src/logger.js @@ -166,6 +166,10 @@ class Logger extends Writable { this.ringBuffer.on('buffer shift', () => { this.debugLogger.log('Buffer is full, will be shifting records until buffer is drained.'); }); + + this.on('buffer drain', () => { + this.debugLogger.log('RingBuffer drained.'); + }); } /** @@ -177,14 +181,19 @@ class Logger extends Writable { this.connection.then(conn => { const record = this.ringBuffer.read(); if (record) { - conn.write(record); // we are checking the buffer state here just after conn.write() // to make sure the last event is sent to socket. if (this.ringBuffer.isEmpty()) { - this.emit('buffer drain'); - // this event is DEPRECATED - will be removed in next major release. - // new users should use 'buffer drain' event instead. - this.emit('connection drain'); + conn.write(record, () => { + process.nextTick(() => { + this.emit('buffer drain'); + // this event is DEPRECATED - will be removed in next major release. + // new users should use 'buffer drain' event instead. + this.emit('connection drain'); + }); + }); + } else { + conn.write(record); } } else { this.debugLogger.log('This should not happen. Read from ringBuffer returned null.'); @@ -309,6 +318,7 @@ class Logger extends Writable { closeConnection() { this.debugLogger.log('Closing retry mechanism along with its connection.'); if (!this.reconnection) { + this.debugLogger.log('No reconnection instance found. Returning.'); return; } // this makes sure retry mechanism and connection will be closed. @@ -373,7 +383,6 @@ class Logger extends Writable { this.reconnection.on('connect', (connection) => { this.debugLogger.log('Connected'); this.emit('connected'); - resolve(connection); // connection listeners connection.on('timeout', () => { @@ -385,6 +394,7 @@ class Logger extends Writable { this.connection = null; this.emit('timed out'); }); + resolve(connection); }); this.reconnection.on('reconnect', (n, delay) => { From 33b4c06368a1993cd9d8a3aa99166a3dfb90bbe8 Mon Sep 17 00:00:00 2001 From: stopal Date: Fri, 9 Sep 2016 14:16:34 +0100 Subject: [PATCH 2/2] version bump --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 5596cef..e804814 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "le_node", - "version": "1.6.4", + "version": "1.6.5", "description": "Logentries client for node; use with or without Winston or Bunyan.", "keywords": [ "logentries",