Skip to content

cluster: alias suicide to exitedAfterDisconnect #7998

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

Closed
wants to merge 1 commit 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
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`][].
Copy link
Member

@ChALkeR ChALkeR Aug 6, 2016

Choose a reason for hiding this comment

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

This link doesn't seem to work. Perhaps the definition is missing (at the end of file)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep, fixed, sorry about that


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