From 3f60baa3a1629f6c39a24e072171d855069f7a91 Mon Sep 17 00:00:00 2001 From: Steve Freegard Date: Thu, 10 Mar 2016 22:42:34 +0000 Subject: [PATCH] Replace exit module with a better workaround --- haraka.js | 9 ++------- logger.js | 10 +++++++++- package.json | 3 +-- server.js | 12 +++++------- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/haraka.js b/haraka.js index 1c21e992d..340c93d61 100644 --- a/haraka.js +++ b/haraka.js @@ -1,9 +1,7 @@ #!/usr/bin/env node 'use strict'; - var path = require('path'); -var exit = require('exit'); // this must be set before "server.js" is loaded process.env.HARAKA = process.env.HARAKA || path.resolve('.'); @@ -36,16 +34,14 @@ process.on('uncaughtException', function (err) { else { logger.logcrit('Caught exception: ' + JSON.stringify(err)); } - logger.dump_logs(); - exit(1); + logger.dump_and_exit(1); }); ['SIGTERM', 'SIGINT'].forEach(function (sig) { process.on(sig, function () { process.title = path.basename(process.argv[1], '.js'); logger.lognotice(sig + ' received'); - logger.dump_logs(); - exit(1); + logger.dump_and_exit(1); }); }); @@ -58,7 +54,6 @@ process.on('exit', function(code) { process.title = path.basename(process.argv[1], '.js'); logger.lognotice('Shutting down'); logger.dump_logs(); - exit(code); }); logger.log("NOTICE", "Starting up Haraka version " + exports.version); diff --git a/logger.js b/logger.js index 978b5737b..e1a832b59 100644 --- a/logger.js +++ b/logger.js @@ -54,7 +54,7 @@ logger.colorize = function (color, str) { '\u001b[' + util.inspect.colors[color][1] + 'm'; }; -logger.dump_logs = function () { +logger.dump_logs = function (cb) { while (logger.deferred_logs.length > 0) { var log_item = logger.deferred_logs.shift(); var color = logger.colors[log_item.level]; @@ -65,9 +65,17 @@ logger.dump_logs = function () { console.log(log_item.data); } } + // Run callback after flush + if (cb) process.stdout.write('', cb); return true; }; +logger.dump_and_exit = function (code) { + this.dump_logs(function () { + process.exit(code); + }); +} + logger.log = function (level, data) { if (level === 'PROTOCOL') { data = data.replace(/\n/g, '\\n\n'); diff --git a/package.json b/package.json index 9bce50850..762b32a3b 100644 --- a/package.json +++ b/package.json @@ -28,8 +28,7 @@ "semver" : "~5.0.3", "sprintf-js" : "~1.0.3", "haraka-tld" : "*", - "haraka-constants" : "*", - "exit" : "~0.1.2" + "haraka-constants" : "*" }, "optionalDependencies": { "elasticsearch" : "*", diff --git a/server.js b/server.js index b10adf736..148ed2ada 100644 --- a/server.js +++ b/server.js @@ -13,7 +13,6 @@ var cluster = require('cluster'); var async = require('async'); var daemon = require('daemon'); var path = require('path'); -var exit = require('exit'); // Need these here so we can run hooks logger.add_log_methods(exports, 'server'); @@ -72,7 +71,7 @@ Server.daemonize = function () { } catch (err) { logger.logerror(err.message); - exit(1); + logger.dump_and_exit(1); } }; @@ -185,8 +184,7 @@ Server.setup_smtp_listeners = function (plugins2, type, inactivity_timeout) { var runInitHooks = function (err) { if (err) { logger.logerror("Failed to setup listeners: " + err.message); - logger.dump_logs(); - exit(-1); + return logger.dump_and_exit(-1); } Server.listening(); plugins2.run_hooks('init_' + type, Server); @@ -294,7 +292,7 @@ Server.init_master_respond = function (retval, msg) { if (!(retval === constants.ok || retval === constants.cont)) { Server.logerror("init_master returned error" + ((msg) ? ': ' + msg : '')); - exit(1); + return logger.dump_and_exit(1); } var c = Server.cfg.main; @@ -312,7 +310,7 @@ Server.init_master_respond = function (retval, msg) { out.scan_queue_pids(function (err, pids) { if (err) { Server.logcrit("Scanning queue failed. Shutting down."); - exit(1); + return logger.dump_and_exit(1); } Server.daemonize(); // Fork workers @@ -374,7 +372,7 @@ Server.init_child_respond = function (retval, msg) { catch (err) { Server.logerror('Terminating child'); } - exit(1); + logger.dump_and_exit(1); }; Server.listening = function () {