Skip to content

Commit

Permalink
cluster: alias suicide to exitedAfterDisconnect
Browse files Browse the repository at this point in the history
This is a backport of 4f619bd which
migrates from worker.suicide to worker.exitedAfterDisconnect.

Refs: #3743
PR-URL: #7998
Reviewed-By: Сковорода Никита Андреевич <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Myles Borins <[email protected]>
  • Loading branch information
evanlucas authored and jasnell committed Aug 12, 2016
1 parent d771d5e commit 810f29f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
24 changes: 24 additions & 0 deletions doc/api/cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,29 @@ if (cluster.isMaster) {
}
```

### worker.exitedAfterDisconnect

* {Boolean}

Alias to [`worker.suicide`][].

Set by calling `.kill()` or `.disconnect()`, until then it is `undefined`.

The boolean `worker.exitedAfterDisconnect` lets you distinguish between
voluntary and accidental exit, the master may choose not to respawn a worker
based on this value.

```js
cluster.on('exit', (worker, code, signal) => {
if (worker.exitedAfterDisconnect === true) {
console.log('The worker exited after disconnect.').
}
});

// kill worker
worker.kill();
```

### worker.id

* {Number}
Expand Down Expand Up @@ -690,3 +713,4 @@ socket.on('data', (id) => {
[child_process event: 'exit']: child_process.html#child_process_event_exit
[child_process event: 'message']: child_process.html#child_process_event_message
[`process` event: `'message'`]: process.html#process_event_message
[`worker.suicide`]: #cluster_worker_suicide
10 changes: 10 additions & 0 deletions lib/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ function Worker(options) {
this.state = options.state || 'none';
this.id = options.id | 0;

Object.defineProperty(this, 'exitedAfterDisconnect', {
get: function() {
return this.suicide;
},
set: function(val) {
this.suicide = val;
},
enumerable: true
});

if (options.process) {
this.process = options.process;
this.process.on('error', (code, signal) =>
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-cluster-worker-constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var worker;

worker = new cluster.Worker();
assert.equal(worker.suicide, undefined);
assert.equal(worker.exitedAfterDisconnect, undefined);
assert.equal(worker.state, 'none');
assert.equal(worker.id, 0);
assert.equal(worker.process, undefined);
Expand All @@ -19,6 +20,7 @@ worker = new cluster.Worker({
process: process
});
assert.equal(worker.suicide, undefined);
assert.equal(worker.exitedAfterDisconnect, undefined);
assert.equal(worker.state, 'online');
assert.equal(worker.id, 3);
assert.equal(worker.process, process);
Expand Down
4 changes: 4 additions & 0 deletions test/parallel/test-cluster-worker-exit.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ if (cluster.isWorker) {
worker_emitExit: [1, "the worker did not emit 'exit'"],
worker_state: ['disconnected', 'the worker state is incorrect'],
worker_suicideMode: [false, 'the worker.suicide flag is incorrect'],
worker_exitedAfterDisconnect: [false,
'the .exitedAfterDisconnect flag is incorrect'],
worker_died: [true, 'the worker is still running'],
worker_exitCode: [EXIT_CODE, 'the worker exited w/ incorrect exitCode'],
worker_signalCode: [null, 'the worker exited w/ incorrect signalCode']
Expand Down Expand Up @@ -66,6 +68,8 @@ if (cluster.isWorker) {
worker.on('disconnect', function() {
results.worker_emitDisconnect += 1;
results.worker_suicideMode = worker.suicide;
results.worker_exitedAfterDisconnect = worker.exitedAfterDisconnect;
assert.strictEqual(worker.suicide, worker.exitedAfterDisconnect);
results.worker_state = worker.state;
if (results.worker_emitExit > 0) {
process.nextTick(function() { finish_test(); });
Expand Down

0 comments on commit 810f29f

Please sign in to comment.