mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-18 04:23:31 +01:00
refactor(Sharding): use options objects (#5510)
Co-authored-by: Vlad Frangu <kingdgrizzle@gmail.com>
This commit is contained in:
@@ -102,11 +102,11 @@ class Shard extends EventEmitter {
|
|||||||
/**
|
/**
|
||||||
* Forks a child process or creates a worker thread for the shard.
|
* Forks a child process or creates a worker thread for the shard.
|
||||||
* <warn>You should not need to call this manually.</warn>
|
* <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)
|
* before resolving. (-1 or Infinity for no wait)
|
||||||
* @returns {Promise<ChildProcess>}
|
* @returns {Promise<ChildProcess>}
|
||||||
*/
|
*/
|
||||||
async spawn(spawnTimeout = 30000) {
|
async spawn(timeout = 30000) {
|
||||||
if (this.process) throw new Error('SHARDING_PROCESS_EXISTS', this.id);
|
if (this.process) throw new Error('SHARDING_PROCESS_EXISTS', this.id);
|
||||||
if (this.worker) throw new Error('SHARDING_WORKER_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);
|
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) => {
|
await new Promise((resolve, reject) => {
|
||||||
const cleanup = () => {
|
const cleanup = () => {
|
||||||
clearTimeout(spawnTimeoutTimer);
|
clearTimeout(spawnTimeoutTimer);
|
||||||
@@ -163,7 +163,7 @@ class Shard extends EventEmitter {
|
|||||||
reject(new Error('SHARDING_READY_TIMEOUT', this.id));
|
reject(new Error('SHARDING_READY_TIMEOUT', this.id));
|
||||||
};
|
};
|
||||||
|
|
||||||
const spawnTimeoutTimer = setTimeout(onTimeout, spawnTimeout);
|
const spawnTimeoutTimer = setTimeout(onTimeout, timeout);
|
||||||
this.once('ready', onReady);
|
this.once('ready', onReady);
|
||||||
this.once('disconnect', onDisconnect);
|
this.once('disconnect', onDisconnect);
|
||||||
this.once('death', onDeath);
|
this.once('death', onDeath);
|
||||||
@@ -188,15 +188,18 @@ class Shard extends EventEmitter {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Kills and restarts the shard's process/worker.
|
* 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 {Object} [options] Respawn options for the shard
|
||||||
* @param {number} [spawnTimeout=30000] The amount in milliseconds to wait until the {@link Client} has become ready
|
* @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)
|
* before resolving. (-1 or Infinity for no wait)
|
||||||
* @returns {Promise<ChildProcess>}
|
* @returns {Promise<ChildProcess>}
|
||||||
*/
|
*/
|
||||||
async respawn(delay = 500, spawnTimeout) {
|
async respawn({ delay = 500, timeout = 30000 } = {}) {
|
||||||
this.kill();
|
this.kill();
|
||||||
if (delay > 0) await Util.delayFor(delay);
|
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
|
// Shard is requesting a respawn of all shards
|
||||||
if (message._sRespawnAll) {
|
if (message._sRespawnAll) {
|
||||||
const { shardDelay, respawnDelay, spawnTimeout } = message._sRespawnAll;
|
const { shardDelay, respawnDelay, timeout } = message._sRespawnAll;
|
||||||
this.manager.respawnAll(shardDelay, respawnDelay, spawnTimeout).catch(() => {
|
this.manager.respawnAll({ shardDelay, respawnDelay, timeout }).catch(() => {
|
||||||
// Do nothing
|
// Do nothing
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -158,16 +158,17 @@ class ShardClientUtil {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Requests a respawn of all shards.
|
* Requests a respawn of all shards.
|
||||||
* @param {number} [shardDelay=5000] How long to wait between shards (in milliseconds)
|
* @param {Object} [options] Options for respawning shards
|
||||||
* @param {number} [respawnDelay=500] How long to wait between killing a shard's process/worker and restarting it
|
* @param {number} [options.shardDelay=5000] How long to wait between shards (in milliseconds)
|
||||||
* (in milliseconds)
|
* @param {number} [options.respawnDelay=500] How long to wait between killing a shard's process/worker and
|
||||||
* @param {number} [spawnTimeout=30000] The amount in milliseconds to wait for a shard to become ready before
|
* restarting it (in milliseconds)
|
||||||
|
* @param {number} [options.timeout=30000] The amount in milliseconds to wait for a shard to become ready before
|
||||||
* continuing to another. (-1 or Infinity for no wait)
|
* continuing to another. (-1 or Infinity for no wait)
|
||||||
* @returns {Promise<void>} Resolves upon the message being sent
|
* @returns {Promise<void>} Resolves upon the message being sent
|
||||||
* @see {@link ShardingManager#respawnAll}
|
* @see {@link ShardingManager#respawnAll}
|
||||||
*/
|
*/
|
||||||
respawnAll(shardDelay = 5000, respawnDelay = 500, spawnTimeout = 30000) {
|
respawnAll({ shardDelay = 5000, respawnDelay = 500, timeout = 30000 } = {}) {
|
||||||
return this.send({ _sRespawnAll: { shardDelay, respawnDelay, spawnTimeout } });
|
return this.send({ _sRespawnAll: { shardDelay, respawnDelay, timeout } });
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -161,13 +161,13 @@ class ShardingManager extends EventEmitter {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Spawns multiple shards.
|
* Spawns multiple shards.
|
||||||
* @param {number|string} [amount=this.totalShards] Number of shards to spawn
|
* @param {Object} [options] Options for spawning shards
|
||||||
* @param {number} [delay=5500] How long to wait in between spawning each shard (in milliseconds)
|
* @param {number|string} [options.amount=this.totalShards] Number of shards to spawn
|
||||||
* @param {number} [spawnTimeout=30000] The amount in milliseconds to wait until the {@link Client} has become ready
|
* @param {number} [options.delay=5500] How long to wait in between spawning each shard (in milliseconds)
|
||||||
* before resolving. (-1 or Infinity for no wait)
|
* @param {number} [options.timeout=30000] The amount in milliseconds to wait until the {@link Client} has become
|
||||||
* @returns {Promise<Collection<number, Shard>>}
|
* @returns {Promise<Collection<number, Shard>>}
|
||||||
*/
|
*/
|
||||||
async spawn(amount = this.totalShards, delay = 5500, spawnTimeout) {
|
async spawn({ amount = this.totalShards, delay = 5500, timeout = 30000 } = {}) {
|
||||||
// Obtain/verify the number of shards to spawn
|
// Obtain/verify the number of shards to spawn
|
||||||
if (amount === 'auto') {
|
if (amount === 'auto') {
|
||||||
amount = await Util.fetchRecommendedShards(this.token);
|
amount = await Util.fetchRecommendedShards(this.token);
|
||||||
@@ -202,7 +202,7 @@ class ShardingManager extends EventEmitter {
|
|||||||
for (const shardID of this.shardList) {
|
for (const shardID of this.shardList) {
|
||||||
const promises = [];
|
const promises = [];
|
||||||
const shard = this.createShard(shardID);
|
const shard = this.createShard(shardID);
|
||||||
promises.push(shard.spawn(spawnTimeout));
|
promises.push(shard.spawn(timeout));
|
||||||
if (delay > 0 && this.shards.size !== this.shardList.length) promises.push(Util.delayFor(delay));
|
if (delay > 0 && this.shards.size !== this.shardList.length) promises.push(Util.delayFor(delay));
|
||||||
await Promise.all(promises); // eslint-disable-line no-await-in-loop
|
await Promise.all(promises); // eslint-disable-line no-await-in-loop
|
||||||
}
|
}
|
||||||
@@ -270,17 +270,18 @@ class ShardingManager extends EventEmitter {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Kills all running shards and respawns them.
|
* Kills all running shards and respawns them.
|
||||||
* @param {number} [shardDelay=5000] How long to wait between shards (in milliseconds)
|
* @param {Object} [options] Options for respawning shards
|
||||||
* @param {number} [respawnDelay=500] How long to wait between killing a shard's process and restarting it
|
* @param {number} [options.shardDelay=5000] How long to wait between shards (in milliseconds)
|
||||||
|
* @param {number} [options.respawnDelay=500] How long to wait between killing a shard's process and restarting it
|
||||||
* (in milliseconds)
|
* (in milliseconds)
|
||||||
* @param {number} [spawnTimeout=30000] The amount in milliseconds to wait for a shard to become ready before
|
* @param {number} [options.timeout=30000] The amount in milliseconds to wait for a shard to become ready before
|
||||||
* continuing to another. (-1 or Infinity for no wait)
|
* continuing to another. (-1 or Infinity for no wait)
|
||||||
* @returns {Promise<Collection<string, Shard>>}
|
* @returns {Promise<Collection<string, Shard>>}
|
||||||
*/
|
*/
|
||||||
async respawnAll(shardDelay = 5000, respawnDelay = 500, spawnTimeout) {
|
async respawnAll({ shardDelay = 5000, respawnDelay = 500, timeout = 30000 } = {}) {
|
||||||
let s = 0;
|
let s = 0;
|
||||||
for (const shard of this.shards.values()) {
|
for (const shard of this.shards.values()) {
|
||||||
const promises = [shard.respawn(respawnDelay, spawnTimeout)];
|
const promises = [shard.respawn({ respawnDelay, timeout })];
|
||||||
if (++s < this.shards.size && shardDelay > 0) promises.push(Util.delayFor(shardDelay));
|
if (++s < this.shards.size && shardDelay > 0) promises.push(Util.delayFor(shardDelay));
|
||||||
await Promise.all(promises); // eslint-disable-line no-await-in-loop
|
await Promise.all(promises); // eslint-disable-line no-await-in-loop
|
||||||
}
|
}
|
||||||
|
|||||||
14
typings/index.d.ts
vendored
14
typings/index.d.ts
vendored
@@ -1368,9 +1368,9 @@ declare module 'discord.js' {
|
|||||||
public eval<T>(fn: (client: Client) => T): Promise<T[]>;
|
public eval<T>(fn: (client: Client) => T): Promise<T[]>;
|
||||||
public fetchClientValue(prop: string): Promise<any>;
|
public fetchClientValue(prop: string): Promise<any>;
|
||||||
public kill(): void;
|
public kill(): void;
|
||||||
public respawn(delay?: number, spawnTimeout?: number): Promise<ChildProcess>;
|
public respawn(options?: { delay?: number, timeout?: number }): Promise<ChildProcess>;
|
||||||
public send(message: any): Promise<Shard>;
|
public send(message: any): Promise<Shard>;
|
||||||
public spawn(spawnTimeout?: number): Promise<ChildProcess>;
|
public spawn(timeout?: number): Promise<ChildProcess>;
|
||||||
|
|
||||||
public on(event: 'spawn' | 'death', listener: (child: ChildProcess) => void): this;
|
public on(event: 'spawn' | 'death', listener: (child: ChildProcess) => void): this;
|
||||||
public on(event: 'disconnect' | 'ready' | 'reconnecting', listener: () => void): this;
|
public on(event: 'disconnect' | 'ready' | 'reconnecting', listener: () => void): this;
|
||||||
@@ -1401,7 +1401,7 @@ declare module 'discord.js' {
|
|||||||
public broadcastEval<T>(fn: (client: Client) => T, shard: number): Promise<T>;
|
public broadcastEval<T>(fn: (client: Client) => T, shard: number): Promise<T>;
|
||||||
public fetchClientValues(prop: string): Promise<any[]>;
|
public fetchClientValues(prop: string): Promise<any[]>;
|
||||||
public fetchClientValues(prop: string, shard: number): Promise<any>;
|
public fetchClientValues(prop: string, shard: number): Promise<any>;
|
||||||
public respawnAll(shardDelay?: number, respawnDelay?: number, spawnTimeout?: number): Promise<void>;
|
public respawnAll(options?: { shardDelay?: number, respawnDelay?: number, timeout?: number }): Promise<void>;
|
||||||
public send(message: any): Promise<void>;
|
public send(message: any): Promise<void>;
|
||||||
|
|
||||||
public static singleton(client: Client, mode: ShardingManagerMode): ShardClientUtil;
|
public static singleton(client: Client, mode: ShardingManagerMode): ShardClientUtil;
|
||||||
@@ -1437,12 +1437,12 @@ declare module 'discord.js' {
|
|||||||
public createShard(id: number): Shard;
|
public createShard(id: number): Shard;
|
||||||
public fetchClientValues(prop: string): Promise<any[]>;
|
public fetchClientValues(prop: string): Promise<any[]>;
|
||||||
public fetchClientValues(prop: string, shard: number): Promise<any>;
|
public fetchClientValues(prop: string, shard: number): Promise<any>;
|
||||||
public respawnAll(
|
public respawnAll(options?: {
|
||||||
shardDelay?: number,
|
shardDelay?: number,
|
||||||
respawnDelay?: number,
|
respawnDelay?: number,
|
||||||
spawnTimeout?: number,
|
timeout?: number,
|
||||||
): Promise<Collection<number, Shard>>;
|
}): Promise<Collection<number, Shard>>;
|
||||||
public spawn(amount?: number | 'auto', delay?: number, spawnTimeout?: number): Promise<Collection<number, Shard>>;
|
public spawn(options?: { amount?: number | 'auto', delay?: number, timeout?: number }): Promise<Collection<number, Shard>>;
|
||||||
|
|
||||||
public on(event: 'shardCreate', listener: (shard: Shard) => void): this;
|
public on(event: 'shardCreate', listener: (shard: Shard) => void): this;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user