Skip to content

Commit

Permalink
feat: add consistent debugger port for each worker with inspectPort o…
Browse files Browse the repository at this point in the history
…ption
  • Loading branch information
anmisttt committed Mar 25, 2024
1 parent 44b4753 commit eda8053
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
16 changes: 4 additions & 12 deletions lib/master.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ class Master extends ClusterProcess {
*/
this._restartQueue = new RestartQueue();

/**
* Configuration object to pass to cluster.setupMaster()
* @type {Object}
* @private
*/
this._masterOpts = {};

this.id = 0;
this.wid = 0;
this.pid = process.pid;
Expand All @@ -52,13 +45,12 @@ class Master extends ClusterProcess {
/**
* Allows same object structure as cluster.setupMaster().
* This function must be used instead of cluster.setupMaster(),
* because all calls of cluster.setupMaster() ignored, except first one.
* An instance of Master will call it, when running.
* @param {Object} opts
* @see {@link http://nodejs.org/api/cluster.html#cluster_cluster_setupmaster_settings}
* @see {@link https://nodejs.org/api/cluster.html#clustersetupmastersettings}
*/
setup(opts) {
Object.assign(this._masterOpts, opts);
cluster.setupMaster({...cluster.settings, ...opts});
}

/**
Expand Down Expand Up @@ -222,6 +214,7 @@ class Master extends ClusterProcess {
isServerPortSet = this.config.has('server.port'),
groups = this.config.get('server.groups', 1),
portsPerGroup = this.config.get('server.portsPerGroup', 1),
masterInspectPort = this.config.get('properties.masterInspectPort'),
workersPerGroup = Math.floor(count / groups);

let port,
Expand All @@ -242,6 +235,7 @@ class Master extends ClusterProcess {
exitThreshold,
allowedSequentialDeaths,
port: isServerPortSet ? port.next(group * portsPerGroup) : 0,
masterInspectPort,
maxListeners: this.getMaxListeners(),
}));

Expand Down Expand Up @@ -457,8 +451,6 @@ class Master extends ClusterProcess {
async _run() {
await this.whenInitialized();

cluster.setupMaster(this._masterOpts);

// TODO maybe run this after starting waitForAllWorkers
this.forEach(worker => worker.run());

Expand Down
12 changes: 12 additions & 0 deletions lib/worker_wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class WorkerWrapperOptions {
this.exitThreshold = options.exitThreshold;
this.allowedSequentialDeaths = options.allowedSequentialDeaths || 0;
this.port = options.port;
this.masterInspectPort = options.masterInspectPort;
}

get port() {
Expand Down Expand Up @@ -130,6 +131,13 @@ class WorkerWrapper extends EventEmitterEx {
*/
this.dead = false;

/**
* Port for node v8 debugger on this worker.
* @type {Number}
* @public
*/
this.inspectPort = this.options.masterInspectPort + this.wid;

/**
* Number of sequential deaths when worker life time was less than `exitThreshold` option value.
* @type {Number}
Expand Down Expand Up @@ -381,6 +389,10 @@ class WorkerWrapper extends EventEmitterEx {
}

setImmediate(() => {
// this call of setup sets inspectPort for node js debugger
// it should be here so that every worker can receive the same debugger port after restarting
this._master.setup({inspectPort: this.inspectPort})

/** @private */
this._worker = cluster.fork({
port: this.options.port,
Expand Down

0 comments on commit eda8053

Please sign in to comment.