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