refactor(Sharding): use options objects (#5510)

Co-authored-by: Vlad Frangu <kingdgrizzle@gmail.com>
This commit is contained in:
monbrey
2021-05-01 05:46:20 +10:00
committed by GitHub
parent 116ecf246e
commit c9b53539d5
4 changed files with 39 additions and 34 deletions

View File

@@ -102,11 +102,11 @@ class Shard extends EventEmitter {
/**
* Forks a child process or creates a worker thread for the shard.
* <warn>You should not need to call this manually.</warn>
* @param {number} [spawnTimeout=30000] The amount in milliseconds to wait until the {@link Client} has become ready
* @param {number} [timeout=30000] The amount in milliseconds to wait until the {@link Client} has become ready
* before resolving. (-1 or Infinity for no wait)
* @returns {Promise<ChildProcess>}
*/
async spawn(spawnTimeout = 30000) {
async spawn(timeout = 30000) {
if (this.process) throw new Error('SHARDING_PROCESS_EXISTS', this.id);
if (this.worker) throw new Error('SHARDING_WORKER_EXISTS', this.id);
@@ -134,7 +134,7 @@ class Shard extends EventEmitter {
*/
this.emit('spawn', this.process || this.worker);
if (spawnTimeout === -1 || spawnTimeout === Infinity) return this.process || this.worker;
if (timeout === -1 || timeout === Infinity) return this.process || this.worker;
await new Promise((resolve, reject) => {
const cleanup = () => {
clearTimeout(spawnTimeoutTimer);
@@ -163,7 +163,7 @@ class Shard extends EventEmitter {
reject(new Error('SHARDING_READY_TIMEOUT', this.id));
};
const spawnTimeoutTimer = setTimeout(onTimeout, spawnTimeout);
const spawnTimeoutTimer = setTimeout(onTimeout, timeout);
this.once('ready', onReady);
this.once('disconnect', onDisconnect);
this.once('death', onDeath);
@@ -188,15 +188,18 @@ class Shard extends EventEmitter {
/**
* Kills and restarts the shard's process/worker.
* @param {number} [delay=500] How long to wait between killing the process/worker and restarting it (in milliseconds)
* @param {number} [spawnTimeout=30000] The amount in milliseconds to wait until the {@link Client} has become ready
* @param {Object} [options] Respawn options for the shard
* @param {number} [options.delay=500] How long to wait between killing the process/worker and
* restarting it (in milliseconds)
* @param {number} [options.timeout=30000] The amount in milliseconds to wait until the {@link Client}
* has become ready
* before resolving. (-1 or Infinity for no wait)
* @returns {Promise<ChildProcess>}
*/
async respawn(delay = 500, spawnTimeout) {
async respawn({ delay = 500, timeout = 30000 } = {}) {
this.kill();
if (delay > 0) await Util.delayFor(delay);
return this.spawn(spawnTimeout);
return this.spawn(timeout);
}
/**
@@ -354,8 +357,8 @@ class Shard extends EventEmitter {
// Shard is requesting a respawn of all shards
if (message._sRespawnAll) {
const { shardDelay, respawnDelay, spawnTimeout } = message._sRespawnAll;
this.manager.respawnAll(shardDelay, respawnDelay, spawnTimeout).catch(() => {
const { shardDelay, respawnDelay, timeout } = message._sRespawnAll;
this.manager.respawnAll({ shardDelay, respawnDelay, timeout }).catch(() => {
// Do nothing
});
return;