Skip to content

Commit e0711b3

Browse files
author
meister03
committed
Fix Typings, add spawnOptions, more consistency to kill Message
1 parent ca1607f commit e0711b3

File tree

6 files changed

+28
-13
lines changed

6 files changed

+28
-13
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "discord-hybrid-sharding",
3-
"version": "1.7.1",
3+
"version": "1.7.2",
44
"description": "The first package which combines sharding manager & internal sharding to save a lot of resources, which allows clustering!",
55
"main": "index.js",
66
"types": "./typings.d.ts",

src/Core/Cluster.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,14 @@ class Cluster extends EventEmitter {
174174
this.thread.kill(options);
175175
this.manager.heartbeat?.clusters.get(this.id)?.stop();
176176
this.restarts.cleanup();
177-
this._handleExit(false);
177+
this._handleExit(false, options);
178178
}
179179
/**
180180
* Kills and restarts the cluster's process/worker.
181181
* @param {ClusterRespawnOptions} [options] Options for respawning the cluster
182182
* @returns {Promise<Child>}
183183
*/
184-
async respawn({ delay = 500, timeout = 30000 } = {}) {
184+
async respawn({ delay = 500, timeout = 30000 } = this.manager.spawnOptions) {
185185
if (this.thread) this.kill({ force: true });
186186
if (delay > 0) await Util.delayFor(delay);
187187
this.manager.heartbeat?.clusters.get(this.id)?.stop();
@@ -269,21 +269,28 @@ class Cluster extends EventEmitter {
269269
* @param {boolean} [respawn=this.manager.respawn] Whether to spawn the cluster again
270270
* @private
271271
*/
272-
_handleExit(respawn = this.manager.respawn) {
272+
_handleExit(respawn = this.manager.respawn, options = {}) {
273273
/**
274274
* Emitted upon the cluster's child process/worker exiting.
275275
* @event Cluster#death
276276
* @param {Child|Worker} process Child process/worker that exited
277277
*/
278278
this.emit('death', this.thread.process);
279-
this.manager._debug(
280-
'[DEATH] Cluster died, attempting respawn | Restarts Left: ' + (this.restarts.max - this.restarts.current),
281-
this.id,
282-
);
279+
if (respawn) {
280+
this.manager._debug(
281+
'[DEATH] Cluster died, attempting respawn | Restarts Left: ' +
282+
(this.restarts.max - this.restarts.current),
283+
this.id,
284+
);
285+
} else {
286+
this.manager._debug('[KILL] Cluster killed with reason: ' + (options.reason || 'not given'), this.id);
287+
}
283288

284289
this.ready = false;
285290
this.thread = null;
286291

292+
if (!respawn) return;
293+
287294
if (this.restarts.current >= this.restarts.max)
288295
this.manager._debug(
289296
'[ATTEMPTED_RESPAWN] Attempted Respawn Declined | Max Restarts have been exceeded',

src/Core/ClusterManager.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class ClusterManager extends EventEmitter {
3333
* @param {number} [options.restarts.max] Maximum amount of restarts a cluster can have in the interval
3434
* @param {object} [options.queue] Control the Spawn Queue
3535
* @param {boolean} [options.queue.auto=true] Whether the spawn queue be automatically managed
36+
* @param {Object} [options.spawnOptions] Options to pass to the spawn,respawn method
3637
*/
3738
constructor(file, options = {}) {
3839
super();
@@ -55,6 +56,10 @@ class ClusterManager extends EventEmitter {
5556
},
5657
clusterData: {},
5758
clusterOptions: {},
59+
spawnOptions: {
60+
delay: 7000,
61+
timeout: -1,
62+
},
5863
},
5964
options,
6065
);
@@ -227,6 +232,8 @@ class ClusterManager extends EventEmitter {
227232
this._debug(`[START] Cluster Manager has been initialized`);
228233

229234
this.promise = new PromiseHandler();
235+
236+
this.spawnOptions = options.spawnOptions;
230237
}
231238
/**
232239
* Spawns multiple internal shards.
@@ -237,7 +244,7 @@ class ClusterManager extends EventEmitter {
237244
* before resolving. (-1 or Infinity for no wait)
238245
* @returns {Promise<Collection<number, Cluster>>}
239246
*/
240-
async spawn({ amount = this.totalShards, delay = 7000, timeout = -1 } = {}) {
247+
async spawn({ amount = this.totalShards, delay, timeout } = this.spawnOptions) {
241248
if (delay < 7000) {
242249
process.emitWarning(
243250
`Spawn Delay (delay: ${delay}) is smaller than 7s, this can cause global rate limits on /gateway/bot`,

src/Plugins/HeartbeatSystem.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class HeartbeatManager {
2626
this.clusters.get(cluster.id).stop(reason);
2727
this.manager._debug(`[Heartbeat_MISSING] ${reason}`, cluster.id);
2828
if (cluster.restarts.current < cluster.restarts.max) {
29-
cluster.respawn({ auto: true });
29+
cluster.respawn({ auto: true, ...this.manager.spawnOptions });
3030
this.manager._debug('[Heartbeat_MISSING] Attempted Respawn', cluster.id);
3131
} else {
3232
this.manager._debug('[Heartbeat_MISSING] Respawn Rejected | Max Restarts of Cluster Reached', cluster.id);

src/Plugins/ReCluster.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class ReClusterManager {
111111
if (switchClusterAfterReady) {
112112
const oldCluster = this.manager.clusters.get(clusterId);
113113
if (oldCluster) {
114-
oldCluster.kill({ force: true });
114+
oldCluster.kill({ force: true, reason: 'Reclusterig' });
115115
oldClusters.delete(clusterId);
116116
}
117117
this.manager.clusters.set(clusterId, cluster);
@@ -131,7 +131,7 @@ class ReClusterManager {
131131
if (oldClusters.size) {
132132
this.manager._debug('[↻][ReClustering] Killing old clusters');
133133
for (let [id, cluster] of oldClusters) {
134-
cluster.kill({ force: true });
134+
cluster.kill({ force: true, reason: 'Reclusterig' });
135135
this.manager._debug(`[↻][ReClustering][${id}] Killed OldCluster`);
136136
this.manager.clusters.delete(id);
137137
}
@@ -146,7 +146,7 @@ class ReClusterManager {
146146
const cluster = newClusters.get(clusterId);
147147
const oldCluster = this.manager.clusters.get(clusterId);
148148
if (oldCluster) {
149-
oldCluster.kill({ force: true });
149+
oldCluster.kill({ force: true, reason: 'Reclusterig' });
150150
oldClusters.delete(clusterId);
151151
}
152152
this.manager.clusters.set(clusterId, cluster);

typings.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ declare module 'discord-hybrid-sharding' {
140140
public clusterList: number[];
141141
public keepAlive: keepAliveOptions;
142142
public queue: Queue;
143+
public spawnOptions: ClusterSpawnOptions;
143144
public recluster?: ReClusterManager;
144145
public heartbeat?: HeartbeatManager;
145146
public broadcast(message: any): Promise<Cluster[]>;

0 commit comments

Comments
 (0)