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

Terminate the workers more gracefully #151

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions lib/master.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ Master.prototype._rollingRestart = function() {
});
};

Master.prototype._stopWorker = function(workerId) {
Master.prototype._stopWorker = function(workerId, force) {
var self = this;
var worker = cluster.workers[workerId];
if (!worker || worker.state === 'disconnected') {
Expand Down Expand Up @@ -156,7 +156,13 @@ Master.prototype._stopWorker = function(workerId) {
resolve();
});
});
worker.disconnect();

if (force) {
worker.disconnect();
} else {
worker.send({ type: 'terminate' });
}

return res;
};

Expand Down Expand Up @@ -316,7 +322,7 @@ Master.prototype._checkHeartbeat = function() {
info.status = lastBeat.status;
}
self._logger.log('error/service-runner/master', info);
self._stopWorker(workerId);
self._stopWorker(workerId, true);
// Don't need to respawn a worker, it will be restarted upon 'exit' event
}
});
Expand Down
12 changes: 10 additions & 2 deletions lib/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ Worker.prototype._getConfigUpdateAction = function() {
if (self._ratelimiter) {
self._ratelimiter._updateBlocks(message.value);
}
} else if (message.type === 'terminate') {
self.stop();
} else {
reject(new Error('Invalid message received: ' + JSON.stringify(message)));
}
Expand Down Expand Up @@ -68,15 +70,21 @@ Worker.prototype._getConfigUpdateAction = function() {
Worker.prototype.stop = function() {
var self = this;
BaseService.prototype.stop.call(self);
var stoppers;
if (Array.isArray(self.serviceReturns)) {
return P.each(self.serviceReturns, function(serviceRet) {
stoppers = P.each(self.serviceReturns, function(serviceRet) {
Copy link
Member

Choose a reason for hiding this comment

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

If we expect each service to implement a graceful shutdown independently, then we should probably start all those in parallel.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ye, that's a good idea, why not..

if (serviceRet && typeof serviceRet.close === 'function') {
return serviceRet.close();
}
});
} else {
return P.resolve();
stoppers = P.resolve();
}
return stoppers.then(function() {
if (cluster.isWorker) {
cluster.worker.disconnect();
}
});
};

Worker.prototype._start = function() {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "service-runner",
"version": "2.1.16",
"version": "2.1.17",
"description": "Generic nodejs service supervisor / cluster runner",
"main": "service-runner.js",
"bin": {
Expand Down