node/lib/internal/cluster/worker.js
cjihrig d751afae0f
cluster: refactor module into multiple files
This commit splits the existing cluster module into several
internal modules. More specifically, the cluster master and
worker implementations are separated, and the various data
structures are separated.

PR-URL: https://github.com/nodejs/node/pull/10746
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
2017-01-30 12:08:44 -05:00

64 lines
1.6 KiB
JavaScript

'use strict';
const EventEmitter = require('events');
const internalUtil = require('internal/util');
const util = require('util');
const defineProperty = Object.defineProperty;
const suicideDeprecationMessage =
'worker.suicide is deprecated. Please use worker.exitedAfterDisconnect.';
module.exports = Worker;
// Common Worker implementation shared between the cluster master and workers.
function Worker(options) {
if (!(this instanceof Worker))
return new Worker(options);
EventEmitter.call(this);
if (options === null || typeof options !== 'object')
options = {};
this.exitedAfterDisconnect = undefined;
defineProperty(this, 'suicide', {
get: internalUtil.deprecate(
() => this.exitedAfterDisconnect,
suicideDeprecationMessage),
set: internalUtil.deprecate(
(val) => { this.exitedAfterDisconnect = val; },
suicideDeprecationMessage),
enumerable: true
});
this.state = options.state || 'none';
this.id = options.id | 0;
if (options.process) {
this.process = options.process;
this.process.on('error', (code, signal) =>
this.emit('error', code, signal)
);
this.process.on('message', (message, handle) =>
this.emit('message', message, handle)
);
}
}
util.inherits(Worker, EventEmitter);
Worker.prototype.kill = function() {
this.destroy.apply(this, arguments);
};
Worker.prototype.send = function() {
return this.process.send.apply(this.process, arguments);
};
Worker.prototype.isDead = function() {
return this.process.exitCode != null || this.process.signalCode != null;
};
Worker.prototype.isConnected = function() {
return this.process.connected;
};