mirror of
https://github.com/nodejs/node.git
synced 2024-12-01 16:10:02 +01:00
1fcb76e8f2
PR-URL: https://github.com/nodejs/node/pull/13702 Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Sam Roberts <vieuxtech@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
50 lines
1.1 KiB
JavaScript
50 lines
1.1 KiB
JavaScript
'use strict';
|
|
const EventEmitter = require('events');
|
|
const util = require('util');
|
|
|
|
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;
|
|
|
|
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;
|
|
};
|