1'use strict'; 2 3const { 4 ObjectSetPrototypeOf, 5} = primordials; 6 7const EventEmitter = require('events'); 8 9module.exports = Worker; 10 11// Common Worker implementation shared between the cluster master and workers. 12function Worker(options) { 13 if (!(this instanceof Worker)) 14 return new Worker(options); 15 16 EventEmitter.call(this); 17 18 if (options === null || typeof options !== 'object') 19 options = {}; 20 21 this.exitedAfterDisconnect = undefined; 22 23 this.state = options.state || 'none'; 24 this.id = options.id | 0; 25 26 if (options.process) { 27 this.process = options.process; 28 this.process.on('error', (code, signal) => 29 this.emit('error', code, signal) 30 ); 31 this.process.on('message', (message, handle) => 32 this.emit('message', message, handle) 33 ); 34 } 35} 36 37ObjectSetPrototypeOf(Worker.prototype, EventEmitter.prototype); 38ObjectSetPrototypeOf(Worker, EventEmitter); 39 40Worker.prototype.kill = function() { 41 this.destroy.apply(this, arguments); 42}; 43 44Worker.prototype.send = function() { 45 return this.process.send.apply(this.process, arguments); 46}; 47 48Worker.prototype.isDead = function() { 49 return this.process.exitCode != null || this.process.signalCode != null; 50}; 51 52Worker.prototype.isConnected = function() { 53 return this.process.connected; 54}; 55