Skip to content

Commit

Permalink
Replace exit module with a better workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
smfreegard committed Mar 11, 2016
1 parent 6bc21f7 commit 3f60baa
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
9 changes: 2 additions & 7 deletions haraka.js
Original file line number Diff line number Diff line change
@@ -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('.');
Expand Down Expand Up @@ -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);
});
});

Expand All @@ -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);
Expand Down
10 changes: 9 additions & 1 deletion logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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');
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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" : "*",
Expand Down
12 changes: 5 additions & 7 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -72,7 +71,7 @@ Server.daemonize = function () {
}
catch (err) {
logger.logerror(err.message);
exit(1);
logger.dump_and_exit(1);
}
};

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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 () {
Expand Down

0 comments on commit 3f60baa

Please sign in to comment.