fix(Shard): add env, execArgv, and argv for worker-based shards (#10429)

* fix(Shard): add env, execArgv, and argv to worker-based threads

* chore: remove process only docs assertion from certain shard options

* chore: update comments for Shard.js

* refactor: Use SHARE_ENV for worker shard's env

* chore: import order

---------

Co-authored-by: Cat++ <69035887+NotGhex@users.noreply.github.com>
This commit is contained in:
Cat++
2024-08-20 21:33:23 +08:00
committed by GitHub
parent bf83db9480
commit b0f8df0f6c
2 changed files with 10 additions and 5 deletions

View File

@@ -5,9 +5,11 @@ const path = require('node:path');
const process = require('node:process');
const { setTimeout, clearTimeout } = require('node:timers');
const { setTimeout: sleep } = require('node:timers/promises');
const { SHARE_ENV } = require('node:worker_threads');
const { DiscordjsError, ErrorCodes } = require('../errors');
const ShardEvents = require('../util/ShardEvents');
const { makeError, makePlainError } = require('../util/Util');
let childProcess = null;
let Worker = null;
@@ -49,13 +51,13 @@ class Shard extends EventEmitter {
this.silent = manager.silent;
/**
* Arguments for the shard's process (only when {@link ShardingManager#mode} is `process`)
* Arguments for the shard's process/worker
* @type {string[]}
*/
this.args = manager.shardArgs ?? [];
/**
* Arguments for the shard's process executable (only when {@link ShardingManager#mode} is `process`)
* Arguments for the shard's process/worker executable
* @type {string[]}
*/
this.execArgv = manager.execArgv;
@@ -136,7 +138,12 @@ class Shard extends EventEmitter {
.on('exit', this._exitListener);
break;
case 'worker':
this.worker = new Worker(path.resolve(this.manager.file), { workerData: this.env })
this.worker = new Worker(path.resolve(this.manager.file), {
workerData: this.env,
env: SHARE_ENV,
execArgv: this.execArgv,
argv: this.args,
})
.on('message', this._handleMessage.bind(this))
.on('exit', this._exitListener);
break;

View File

@@ -37,9 +37,7 @@ class ShardingManager extends EventEmitter {
* @property {boolean} [silent=false] Whether to pass the silent flag to child process
* (only available when mode is set to 'process')
* @property {string[]} [shardArgs=[]] Arguments to pass to the shard script when spawning
* (only available when mode is set to 'process')
* @property {string[]} [execArgv=[]] Arguments to pass to the shard script executable when spawning
* (only available when mode is set to 'process')
* @property {string} [token] Token to use for automatic shard count and passing to shards
*/