fix(Shard): use provided timeout when respawning (#6735)

This commit is contained in:
SpaceEEC
2021-10-03 15:04:23 +02:00
committed by GitHub
parent 34b2ad0d8e
commit 905d100d4d
2 changed files with 9 additions and 5 deletions

View File

@@ -92,7 +92,7 @@ class Shard extends EventEmitter {
* @type {Function}
* @private
*/
this._exitListener = this._handleExit.bind(this, undefined);
this._exitListener = null;
}
/**
@@ -106,6 +106,8 @@ class Shard extends EventEmitter {
if (this.process) throw new Error('SHARDING_PROCESS_EXISTS', this.id);
if (this.worker) throw new Error('SHARDING_WORKER_EXISTS', this.id);
this._exitListener = this._handleExit.bind(this, undefined, timeout);
if (this.manager.mode === 'process') {
this.process = childProcess
.fork(path.resolve(this.manager.file), this.args, {
@@ -132,7 +134,7 @@ class Shard extends EventEmitter {
*/
this.emit('spawn', child);
if (timeout === -1 || timeout === Infinity) return child;
if (timeout === -1 || timeout === Infinity) return Promise.resolve(child);
return new Promise((resolve, reject) => {
const cleanup = () => {
clearTimeout(spawnTimeoutTimer);
@@ -379,9 +381,11 @@ class Shard extends EventEmitter {
/**
* Handles the shard's process/worker exiting.
* @param {boolean} [respawn=this.manager.respawn] Whether to spawn the shard again
* @param {number} [timeout] The amount in milliseconds to wait until the {@link Client}
* has become ready (`-1` or `Infinity` for no wait)
* @private
*/
_handleExit(respawn = this.manager.respawn) {
_handleExit(respawn = this.manager.respawn, timeout) {
/**
* Emitted upon the shard's child process/worker exiting.
* @event Shard#death
@@ -395,7 +399,7 @@ class Shard extends EventEmitter {
this._evals.clear();
this._fetches.clear();
if (respawn) this.spawn().catch(err => this.emit('error', err));
if (respawn) this.spawn(timeout).catch(err => this.emit('error', err));
}
}

2
typings/index.d.ts vendored
View File

@@ -1677,7 +1677,7 @@ export class Shard extends EventEmitter {
private _evals: Map<string, Promise<unknown>>;
private _exitListener: (...args: any[]) => void;
private _fetches: Map<string, Promise<unknown>>;
private _handleExit(respawn?: boolean): void;
private _handleExit(respawn?: boolean, timeout?: number): void;
private _handleMessage(message: unknown): void;
public args: string[];