Skip to content

Commit e4b4aa4

Browse files
author
huzhou
committed
1 parent dfe9922 commit e4b4aa4

File tree

3 files changed

+60
-51
lines changed

3 files changed

+60
-51
lines changed

lib/misc.js

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -172,65 +172,65 @@ exports.deathQueueGenerator = function(options){
172172
//retry of the 'die' process
173173
die(retry - 1);
174174

175-
}, wait);
175+
}, wait),
176+
onSuccessorListening = function onSuccessorListening(onboard){
176177

177-
//when successor is in place, the old worker could be discontinued finally
178-
emitter.on('listening', function onSuccessorListening(onboard){
178+
if(successorPid !== onboard){
179+
return; //noop
180+
}
181+
else{
182+
emitter.removeListener('listening', onSuccessorListening);
183+
}
179184

180-
if(successorPid !== onboard){
181-
return; //noop
182-
}
183-
else{
184-
emitter.removeListener('listening', onSuccessorListening);
185-
}
185+
clearTimeout(successorGuard);
186+
logger.debug('[deathQueue] successor:%d of %d is ready, wait for %s and timeout in:%dms', successorPid, pid, death, wait);
186187

187-
clearTimeout(successorGuard);
188-
logger.debug('[deathQueue] successor:%d of %d is ready, wait for %s and timeout in:%dms', successorPid, pid, death, wait);
188+
var onDeath = function onDeath(dismiss){
189189

190-
var deathGuard = setTimeout(function(){
190+
if(pid !== dismiss){
191+
return;
192+
}
193+
else{
194+
emitter.removeListener('died', onDeath);
195+
}
191196

192-
if(!exports.safeKill(pid, 'SIGTERM', logger)){
193-
//worker still there, should emit 'exit' eventually
194-
logger.debug('[deathQueue] worker:%d did not report death by:%d, kill by SIGTERM', pid, wait);
195-
//remove the redundant exit listener
196-
emitter.removeListener('died', onDeath);
197-
}
198-
else{//suicide or accident already happended, process has run away
199-
//we emit this from master on behalf of the run away process.
200-
logger.debug('[deathQueue] worker:%d probably ran away, emit:%s on behalf', death);
201-
//immediately report death to the master
202-
emitter.emit('died', pid);
203-
}
197+
logger.debug('[deathQueue] %d died', pid);
204198

205-
}, wait);
199+
clearTimeout(deathGuard);//release the deathGuard
200+
201+
tillDeath.resolve(pid);
206202

207-
worker.kill('SIGINT');
208-
209-
emitter.on('died', function onDeath(dismiss){
203+
if(tillPrevDeath === afterDeath){//last of dyingQueue resolved, clean up the dyingQueue
210204

211-
if(pid !== dismiss){
212-
return;
213-
}
214-
else{
215-
emitter.removeListener('died', onDeath);
216-
}
205+
logger.debug('[deathQueue] death queue cleaned up');
217206

218-
logger.debug('[deathQueue] %d died', pid);
207+
tillPrevDeath = null;
208+
209+
queue = [];
210+
}
211+
},
212+
deathGuard = setTimeout(function(){
219213

220-
clearTimeout(deathGuard);//release the deathGuard
221-
222-
tillDeath.resolve(pid);
214+
if(!exports.safeKill(pid, 'SIGTERM', logger)){
215+
//worker still there, should emit 'exit' eventually
216+
logger.debug('[deathQueue] worker:%d did not report death by:%d, kill by SIGTERM', pid, wait);
217+
}
218+
else{//suicide or accident already happended, process has run away
219+
//we emit this from master on behalf of the run away process.
220+
logger.debug('[deathQueue] worker:%d probably ran away, emit:%s on behalf', death);
221+
//immediately report death to the master
222+
emitter.emit('died', pid);
223+
}
223224

224-
if(tillPrevDeath === afterDeath){//last of dyingQueue resolved, clean up the dyingQueue
225+
}, wait);
225226

226-
logger.debug('[deathQueue] death queue cleaned up');
227+
worker.kill('SIGINT');
228+
229+
emitter.on('died', onDeath);
230+
};
227231

228-
tillPrevDeath = null;
229-
230-
queue.splice(0, queue.length);
231-
}
232-
});
233-
});
232+
//when successor is in place, the old worker could be discontinued finally
233+
emitter.on('listening', onSuccessorListening);
234234
};
235235

236236
if(!tillPrevDeath){//1st in the dying queue,

lib/process.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -470,12 +470,20 @@ Process.prototype.listen = function() {
470470
var listening = false, conns = 0, totalConns = 0, timedoutConns = 0, noAppClosed = 0, graceful = _.once(function graceful(signal, code){
471471

472472
_.each(apps, function(app){
473+
log('[graceful] app status:%s', app.listening);
473474
if(app.listening){
474475
try {
475-
app.app.close();
476+
if(app.server){
477+
log('[graceful] app server shutdown');
478+
app.server.close();
479+
}
480+
else{
481+
log('[graceful] app app shutdown');
482+
app._router = null;
483+
}
476484
}
477485
catch(e) {
478-
486+
log('worker shutdown error:', e);
479487
}
480488
}
481489
});
@@ -503,9 +511,9 @@ Process.prototype.listen = function() {
503511

504512
process.on('SIGTERM', function() {
505513

506-
log(process.pid + ' got SIGTERM');
514+
log(process.pid, 'got SIGTERM');
507515

508-
graceful('SIGTERM', 0);
516+
process.exit(0);
509517
});
510518

511519
// Set time out on idle sockets
@@ -570,6 +578,7 @@ Process.prototype.listen = function() {
570578
app.app.emit('listening');
571579
});
572580
server.on('connection', monitorConnection);
581+
app.server = server;
573582
});
574583
});
575584

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"email": "huzhou@ebay.com"
1010
}],
1111
"name": "cluster2",
12-
"version": "0.4.23",
12+
"version": "0.4.24",
1313
"repository": {
1414
"type": "git",
1515
"url": "https://github.com/cubejs/cluster2"

0 commit comments

Comments
 (0)