Skip to content

Commit

Permalink
Bump package.json version to 1.8.8 and refactor cluster respawn logic…
Browse files Browse the repository at this point in the history
… for improved readability and performance
  • Loading branch information
Digital39999 committed Nov 18, 2024
1 parent 9764e50 commit c782ce7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.8.7",
"version": "1.8.8",
"name": "status-sharding",
"author": "Digital39999",
"scripts": {
Expand Down
45 changes: 29 additions & 16 deletions src/core/clusterManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import { IPCBrokerManager } from '../handlers/broker';
import { PromiseHandler } from '../handlers/promise';
import { RefShardingClient } from './clusterClient';
import { Cluster, RefCluster } from './cluster';
import { ChildProcess } from 'child_process';
import { Queue } from '../handlers/queue';
import { Worker } from 'worker_threads';
import CustomMap from '../other/map';
import { Guild } from 'discord.js';
import EventEmitter from 'events';
Expand Down Expand Up @@ -87,7 +85,7 @@ export class ClusterManager<
* @param {string} file - Path to the file that will be spawned.
* @param {ClusterManagerCreateOptions<ClusteringMode>} options - Options for the ClusterManager.
*/
constructor(public file: string, options: ClusterManagerCreateOptions<ClusteringMode>) {
constructor (public file: string, options: ClusterManagerCreateOptions<ClusteringMode>) {
super();

if (!file) throw new Error('CLIENT_INVALID_OPTION | No File specified.');
Expand Down Expand Up @@ -225,26 +223,35 @@ export class ClusterManager<
* @returns {Promise<Map<number, InternalCluster>>} The clusters after the respawn.
*/
public async respawnAll(clusterDelay: number = 8000, respawnDelay: number = 5500, timeout: number = -1, except: number[] = []): Promise<Map<number, InternalCluster>> {
let s = 0; let i = 0;
this.promise.nonces.clear();
this._debug('[ClusterManager] Respawning all clusters.');

const promises: Promise<ChildProcess | Worker | void>[] = [];
const listOfShardsForCluster = ShardingUtils.chunkArray(this.options.shardList || [], this.options.shardsPerClusters || this.options.totalShards);
const listOfShardsForCluster = ShardingUtils.chunkArray(
this.options.shardList || [],
this.options.shardsPerClusters || this.options.totalShards,
);

const clustersToRestart = Array.from(this.clusters.values()).filter((c) => !except.includes(c.id));
if (clustersToRestart.length === 0) return this.clusters;

for (const cluster of clustersToRestart) {
promises.push(cluster.respawn(respawnDelay, timeout));
let i = 0;

while (clustersToRestart.length > 0) {
const cluster = clustersToRestart.shift();
if (!cluster) continue;

this._debug(`Respawning cluster ${cluster.id}`);
await cluster.respawn(respawnDelay, timeout);

const length = listOfShardsForCluster[i]?.length || this.options.totalShards / this.options.totalClusters;
if (++s < this.clusters.size && clusterDelay > 0) promises.push(ShardingUtils.delayFor(length * clusterDelay));
if (clusterDelay > 0) {
this._debug(`Delaying ${length * clusterDelay}ms for cluster ${cluster.id}`);
await ShardingUtils.delayFor(length * clusterDelay);
}

i++;
}

await Promise.allSettled(promises);
return this.clusters;
}

Expand All @@ -258,26 +265,32 @@ export class ClusterManager<
* @returns {Promise<Map<number, InternalCluster>>} The clusters after the respawn.
*/
public async respawnClusters(clusters: number[], clusterDelay: number = 8000, respawnDelay: number = 5500, timeout: number = -1): Promise<Map<number, InternalCluster>> {
let s = 0; let i = 0;
this.promise.nonces.clear();
this._debug('[ClusterManager] Respawning specific clusters.');

const promises: Promise<ChildProcess | Worker | void>[] = [];
const listOfShardsForCluster = ShardingUtils.chunkArray(this.options.shardList || [], this.options.shardsPerClusters || this.options.totalShards);
const listOfShardsForCluster = ShardingUtils.chunkArray(
this.options.shardList || [],
this.options.shardsPerClusters || this.options.totalShards,
);

const clustersToRestart = Array.from(this.clusters.values()).filter((c) => clusters.includes(c.id));
if (clustersToRestart.length === 0) return this.clusters;

let i = 0;

for (const cluster of clustersToRestart) {
promises.push(cluster.respawn(respawnDelay, timeout));
this._debug(`Respawning cluster ${cluster.id}`);
await cluster.respawn(respawnDelay, timeout);

const length = listOfShardsForCluster[i]?.length || this.options.totalShards / this.options.totalClusters;
if (++s < clusters.length && clusterDelay > 0) promises.push(ShardingUtils.delayFor(length * clusterDelay));
if (clusterDelay > 0 && clustersToRestart.length > 1) {
this._debug(`Delaying ${length * clusterDelay}ms for cluster ${cluster.id}`);
await ShardingUtils.delayFor(length * clusterDelay);
}

i++;
}

await Promise.allSettled(promises);
return this.clusters;
}

Expand Down

0 comments on commit c782ce7

Please sign in to comment.