Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set graceful shutdown off by default #1927

Merged
merged 3 commits into from May 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions configfile.js
Expand Up @@ -266,6 +266,7 @@ cfreader.ensure_enoent_timer = function () {
})(files[i]); // END BLOCK SCOPE
}
}, 60 * 1000);
cfreader._enoent_timer.unref();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW how come we now have a haraka-config package and this file is still in Haraka?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since moving config into a module (so npm plugins could depend on it) I've been moving more and more stuff to depend on haraka-config, with the eventual plan to deprecate the build-in conifg.js and configfile.js. We're a ways down that road and I don't recall right now what's left to do.

};

process.on('message', function (msg) {
Expand Down
4 changes: 2 additions & 2 deletions haraka.js
Expand Up @@ -50,10 +50,10 @@ var shutting_down = false;
logger.lognotice(sig + ' received');
logger.dump_and_exit(function () {
if (server.cluster && server.cluster.isMaster) {
server.gracefulShutdown();
server.performShutdown();
}
else if (!server.cluster) {
server.gracefulShutdown();
server.performShutdown();
}
});
});
Expand Down
34 changes: 23 additions & 11 deletions server.js
@@ -1,6 +1,7 @@
'use strict';
// smtp network server

// var log = require('why-is-node-running');
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW I left this in but commented because it makes it easy to debug any kind of "won't quit" issues in the future.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Says the guy that use to be adamantly opposed to such a thing. ;-) Welcome to the dark side. ;-)

var net = require('./tls_socket');
var conn = require('./connection');
var outbound = require('./outbound');
Expand All @@ -27,6 +28,7 @@ Server.load_smtp_ini = function () {
Server.cfg = Server.config.get('smtp.ini', {
booleans: [
'-main.daemonize',
'-main.graceful_shutdown',
],
}, function () {
Server.load_smtp_ini();
Expand Down Expand Up @@ -97,26 +99,33 @@ Server.gracefulRestart = function () {
Server._graceful();
}

Server.performShutdown = function () {
if (Server.cfg.main.graceful_shutdown) {
return Server.gracefulShutdown();
}
logger.loginfo("Shutting down.");
process.exit(0);
}

Server.gracefulShutdown = function () {
logger.loginfo('Shutting down listeners');
Server.listeners.forEach(function (server) {
server.close();
});
Server._graceful(function () {
// log();
logger.loginfo("Failed to shutdown naturally. Exiting.");
process.exit(0);
});
}

Server._graceful = function (shutdown) {
if (!Server.cluster) {
if (shutdown) {
['outbound', 'cfreader', 'plugins'].forEach(function (module) {
process.emit('message', {event: module + '.shutdown'});
});
var t = setTimeout(shutdown, Server.cfg.main.force_shutdown_timeout * 1000);
return t.unref();
}
if (!Server.cluster && shutdown) {
['outbound', 'cfreader', 'plugins'].forEach(function (module) {
process.emit('message', {event: module + '.shutdown'});
});
var t = setTimeout(shutdown, Server.cfg.main.force_shutdown_timeout * 1000);
return t.unref();
}

if (gracefull_in_progress) {
Expand All @@ -129,10 +138,13 @@ Server._graceful = function (shutdown) {
var disconnect_timeout = 30;
var exit_timeout = 30;
cluster.removeAllListeners('exit');
// only reload one worker at a time
// otherwise, we'll have a time when no connection handlers are running
// we reload using eachLimit where limit = num_workers - 1
// this kills all-but-one workers in parallel, leaving one running
// for new connections, and then restarts that one last worker.
var worker_ids = Object.keys(cluster.workers);
async.eachSeries(worker_ids, function (id, cb) {
var limit = worker_ids.length - 1;
if (limit < 2) limit = 1;
async.eachLimit(worker_ids, limit, function (id, cb) {
logger.lognotice("Killing node: " + id);
var worker = cluster.workers[id];
['outbound', 'cfreader', 'plugins'].forEach(function (module) {
Expand Down
4 changes: 4 additions & 0 deletions tls_socket.js
Expand Up @@ -137,6 +137,10 @@ pluggableStream.prototype.setKeepAlive = function (bool) {
pluggableStream.prototype.setNoDelay = function (/* true||false */) {
};

pluggableStream.prototype.unref = function () {
return this.targetsocket.unref();
};
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this in case we need to unref a socket in the future.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this ever get called while targetsocket is undefined?


pluggableStream.prototype.setTimeout = function (timeout) {
this._timeout = timeout;
return this.targetsocket.setTimeout(timeout);
Expand Down