From fe40e2f8e63c00077a781841f211dfad84b43dd8 Mon Sep 17 00:00:00 2001 From: Justin Gallardo Date: Wed, 21 Sep 2016 14:35:16 -0700 Subject: [PATCH 1/3] Make writing to stdout/stderr blocking in modular inputs. This lets us rely on output being flushed whenever we exit instead of waiting for 'drain' to happen, which doesn't ever happen. --- lib/modularinputs/index.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/modularinputs/index.js b/lib/modularinputs/index.js index dae22c1f6..488e2ba32 100644 --- a/lib/modularinputs/index.js +++ b/lib/modularinputs/index.js @@ -56,6 +56,13 @@ ModularInputs.execute = function(exports, module) { // by ModularInput.runScript(). var ew = new this.EventWriter(); + // In order to ensure that everything that is written to stdout/stderr is flushed before we exit, + // set the file handles to blocking. This ensures we exit properly in a timely fashion. + // https://github.com/nodejs/node/issues/6456 + [process.stdout, process.stderr].forEach(function(s) { + s && s.isTTY && s._handle && s._handle.setBlocking && s._handle.setBlocking(true); + }); + var scriptStatus; Async.chain([ function(done) { @@ -74,10 +81,7 @@ ModularInputs.execute = function(exports, module) { ModularInputs.Logger.error('', err, ew._err); } - // Wait for process.stdout to drain before exiting the process. - process.stdout.once("drain", function() { - process.exit(scriptStatus || err ? 1 : 0); - }); + process.exit(scriptStatus || err ? 1 : 0); } ); } From 570d4b8bd3859a2db0528baef9f54668cd7ea2a1 Mon Sep 17 00:00:00 2001 From: Justin Gallardo Date: Thu, 22 Sep 2016 13:41:35 -0700 Subject: [PATCH 2/3] Handle process.argv in a more portable way. --- lib/modularinputs/index.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/modularinputs/index.js b/lib/modularinputs/index.js index 488e2ba32..d582fb85d 100644 --- a/lib/modularinputs/index.js +++ b/lib/modularinputs/index.js @@ -35,12 +35,8 @@ var ModularInputs = { */ ModularInputs.execute = function(exports, module) { if (require.main === module) { - var args = process.argv; - - // Trim the first argument, if it is the node.js executable. - if (args[0] === 'node' || ModularInputs.utils.contains(args[0], 'node.exe')) { - args = args.slice(1, args.length); - } + // Slice process.argv ignoring the first argument as it is the path to the node executable. + var args = process.argv.slice(1); // Default empty functions for life cycle events. exports.setup = exports.setup || ModularInputs.ModularInput.prototype.setup; From 5fd8a39441bfd90e8d18ed0240a4c89f60cc3fda Mon Sep 17 00:00:00 2001 From: Justin Gallardo Date: Thu, 22 Sep 2016 13:42:01 -0700 Subject: [PATCH 3/3] Don't open the inputStream(stdin) if it is a TTY --- lib/modularinputs/modularinput.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/modularinputs/modularinput.js b/lib/modularinputs/modularinput.js index 66fdda593..4abc0ecba 100644 --- a/lib/modularinputs/modularinput.js +++ b/lib/modularinputs/modularinput.js @@ -61,7 +61,8 @@ var that = this; // Resume streams before trying to read their data. - if (inputStream.resume) { + // If the inputStream is a TTY, we don't want to open the stream as it will hold the process open. + if (inputStream.resume && !inputStream.isTTY) { inputStream.resume(); } var bigBuff = new Buffer(0);