Skip to content

Commit

Permalink
Worker is not marked dead on requested stop
Browse files Browse the repository at this point in the history
  • Loading branch information
Slava Baginov committed Aug 12, 2019
1 parent b8b7537 commit 67ebeda
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/worker_wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,8 @@ class WorkerWrapper extends EventEmitterEx {
// than `exitThreshold` option value (and option was passed to constructor).
if (this.options.exitThreshold &&
Date.now() - this.startTime < this.options.exitThreshold &&
!this.restarting) {
!this.restarting &&
!this.stopping) {
this._sequentialDeaths++;
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
},
"devDependencies": {
"chai": "^3.5.0",
"delay": "^4.3.0",
"eslint": "^4.19.1",
"eslint-config-nodules": "^0.4.0",
"istanbul": "^0.4.1",
Expand Down
39 changes: 39 additions & 0 deletions test/func/fixtures/dead_workers/master.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const proc = require('luster');

proc
.configure({
app: 'worker.js',
workers: 1,
control: {
stopTimeout: 100,
exitThreshold: 50,
allowedSequentialDeaths: 0,
}
}, true, __dirname)
.run();

if (proc.isMaster) {
proc.once('running', () => {
process.send('ready');
});

proc.on('worker exit', worker => {
console.log(`Worker ${worker.wid} has exited, dead is ${worker.dead}`);
});

process.on('message', message => {
switch (message) {
case 'worker quit':
proc.emitToAll('quit');
break;
case 'worker restart':
proc.forEach(worker => worker.restart());
break;
case 'worker stop':
proc.forEach(worker => worker.stop());
break;
default:
throw new Error(`Got unknown command ${message}`);
}
});
}
1 change: 1 addition & 0 deletions test/func/fixtures/dead_workers/node_modules/luster

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions test/func/fixtures/dead_workers/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const worker = require('luster');

worker.on('master quit', () => {
process.disconnect();
});
2 changes: 1 addition & 1 deletion test/func/fixtures/restart_queue/master.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function killFirstWorker() {
firstWorker.on('state', state => {
// force dead state
if (state === WorkerWrapper.STATES.LAUNCHING) {
firstWorker.stop();
firstWorker.process.kill(9);
}

if (state === WorkerWrapper.STATES.STOPPED && firstWorker.dead) {
Expand Down
50 changes: 50 additions & 0 deletions test/func/test/dead_workers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* globals describe,it,before,after,assert */
'use strict';

const LusterInstance = require('../helpers/luster_instance');
const delay = require('delay');

describe('dead workers', () => {
let instance;

beforeEach(async () => {
instance = await LusterInstance
.run('../fixtures/dead_workers/master.js');
});

it('worker quitting before exitThreshold should be marked as dead', async () => {
await instance.sendWaitTimeout('worker quit', 50);

const expectedEvents = 'Worker 1 has exited, dead is true\n';
assert.equal(instance.output(), expectedEvents);
});

it('worker quitting after exitThreshold should not be marked as dead', async () => {
await delay(50);
await instance.sendWaitTimeout('worker quit', 50);

const expectedEvents = 'Worker 1 has exited, dead is false\n';
assert.equal(instance.output(), expectedEvents);
});

it('worker restarted manually should not be marked as dead', async () => {
await instance.sendWaitTimeout('worker restart', 50);

const expectedEvents = 'Worker 1 has exited, dead is false\n';
assert.equal(instance.output(), expectedEvents);
});

it('worker stopped manually should not be marked as dead', async () => {
await instance.sendWaitTimeout('worker stop', 50);

const expectedEvents = 'Worker 1 has exited, dead is false\n';
assert.equal(instance.output(), expectedEvents);
});

afterEach(() => {
if (instance) {
instance.kill();
instance = null;
}
});
});

0 comments on commit 67ebeda

Please sign in to comment.