node/lib/internal/cluster/worker.js
Michael Dawson 15164cebce lib,src: update cluster to use Parent
Doc deprecate isMaster and setupMaster in favor
of isPrimary and setupPrimary.

Signed-off-by: Michael Dawson <mdawson@devrus.com>

PR-URL: https://github.com/nodejs/node/pull/36478
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Beth Griggs <bgriggs@redhat.com>
2021-01-05 15:41:45 -05:00

56 lines
1.3 KiB
JavaScript

'use strict';
const {
ObjectSetPrototypeOf,
ReflectApply,
} = primordials;
const EventEmitter = require('events');
module.exports = Worker;
// Common Worker implementation shared between the cluster primary and workers.
function Worker(options) {
if (!(this instanceof Worker))
return new Worker(options);
ReflectApply(EventEmitter, 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)
);
}
}
ObjectSetPrototypeOf(Worker.prototype, EventEmitter.prototype);
ObjectSetPrototypeOf(Worker, EventEmitter);
Worker.prototype.kill = function() {
ReflectApply(this.destroy, this, arguments);
};
Worker.prototype.send = function() {
return ReflectApply(this.process.send, this.process, arguments);
};
Worker.prototype.isDead = function() {
return this.process.exitCode != null || this.process.signalCode != null;
};
Worker.prototype.isConnected = function() {
return this.process.connected;
};