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