galactic.ts 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -195,6 +195,7 @@ var BridgeClientConnection = class {
195
195
  data;
196
196
  connectionStatus = "ready" /* READY */;
197
197
  dev = false;
198
+ establishedAt = Date.now();
198
199
  _onMessage;
199
200
  _onRequest;
200
201
  constructor(instanceID, connection, data, dev) {
@@ -388,6 +389,7 @@ var Bridge = class {
388
389
  intents;
389
390
  shardsPerCluster = 1;
390
391
  clusterToStart = 1;
392
+ reclusteringTimeoutInMs;
391
393
  clusterCalculator;
392
394
  eventMap = {
393
395
  CLUSTER_READY: void 0,
@@ -400,12 +402,13 @@ var Bridge = class {
400
402
  ERROR: void 0,
401
403
  CLIENT_STOP: void 0
402
404
  };
403
- constructor(port, token, intents, shardsPerCluster, clusterToStart) {
405
+ constructor(port, token, intents, shardsPerCluster, clusterToStart, reclusteringTimeoutInMs) {
404
406
  this.port = port;
405
407
  this.token = token;
406
408
  this.intents = intents;
407
409
  this.clusterToStart = clusterToStart;
408
410
  this.shardsPerCluster = shardsPerCluster;
411
+ this.reclusteringTimeoutInMs = reclusteringTimeoutInMs;
409
412
  this.clusterCalculator = new ClusterCalculator(this.clusterToStart, this.shardsPerCluster);
410
413
  this.server = new Server({
411
414
  port: this.port
@@ -429,7 +432,7 @@ var Bridge = class {
429
432
  if (!up) {
430
433
  return;
431
434
  }
432
- const connectedClients = this.connectedClients.values().filter((c) => c.connectionStatus == "ready" /* READY */ && !c.dev).toArray();
435
+ const connectedClients = this.connectedClients.values().filter((c) => c.connectionStatus == "ready" /* READY */).filter((c) => !c.dev).filter((c) => c.establishedAt + this.reclusteringTimeoutInMs < Date.now()).toArray();
433
436
  const { most, least } = this.clusterCalculator.findMostAndLeastClustersForConnections(connectedClients);
434
437
  if (most) {
435
438
  const clusterToSteal = this.clusterCalculator.getClusterForConnection(most)[0] || void 0;
@@ -731,6 +734,17 @@ var Bridge = class {
731
734
  await instance.connection.close("Instance stopped.", false);
732
735
  }
733
736
  }
737
+ sendRequestToGuild(cluster, guildID, data, timeout = 5e3) {
738
+ if (!cluster.connection) {
739
+ return Promise.reject(new Error("No connection defined for cluster " + cluster.clusterID));
740
+ }
741
+ return cluster.connection.eventManager.request({
742
+ type: "REDIRECT_REQUEST_TO_GUILD",
743
+ clusterID: cluster.clusterID,
744
+ guildID,
745
+ data
746
+ }, timeout);
747
+ }
734
748
  };
735
749
 
736
750
  // src/cluster/Cluster.ts
@@ -744,6 +758,7 @@ var Cluster = class _Cluster {
744
758
  intents;
745
759
  eventManager;
746
760
  client;
761
+ onSelfDestruct;
747
762
  eventMap = {
748
763
  message: void 0,
749
764
  request: void 0,
@@ -882,6 +897,10 @@ var Cluster = class _Cluster {
882
897
  } else {
883
898
  return result;
884
899
  }
900
+ } else if (m.type == "SELF_DESTRUCT") {
901
+ if (this.onSelfDestruct) {
902
+ this.onSelfDestruct();
903
+ }
885
904
  }
886
905
  return void 0;
887
906
  }
@@ -1015,6 +1034,7 @@ var BotInstance = class {
1015
1034
  "message": void 0,
1016
1035
  "request": void 0,
1017
1036
  "PROCESS_KILLED": void 0,
1037
+ "PROCESS_SELF_DESTRUCT_ERROR": void 0,
1018
1038
  "PROCESS_SPAWNED": void 0,
1019
1039
  "ERROR": void 0,
1020
1040
  "PROCESS_ERROR": void 0,
@@ -1044,7 +1064,8 @@ var BotInstance = class {
1044
1064
  },
1045
1065
  stdio: "inherit",
1046
1066
  execArgv: this.execArgv,
1047
- silent: false
1067
+ silent: false,
1068
+ detached: true
1048
1069
  });
1049
1070
  const client = new ClusterProcess(clusterID, child, shardList, totalShards);
1050
1071
  child.stdout?.on("data", (data) => {
@@ -1079,18 +1100,29 @@ var BotInstance = class {
1079
1100
  }
1080
1101
  killProcess(client, reason) {
1081
1102
  client.status = "stopped";
1082
- if (client.child && client.child.pid) {
1083
- if (client.child.kill("SIGKILL")) {
1084
- if (this.eventMap.PROCESS_KILLED) this.eventMap.PROCESS_KILLED(client, reason, true);
1103
+ client.eventManager.request({
1104
+ type: "SELF_DESTRUCT",
1105
+ reason
1106
+ }, 5e3).catch(() => {
1107
+ if (this.eventMap.PROCESS_SELF_DESTRUCT_ERROR) this.eventMap.PROCESS_SELF_DESTRUCT_ERROR(client, reason, "Cluster didnt respond to shot-call.");
1108
+ }).finally(() => {
1109
+ if (client.child && client.child.pid) {
1110
+ if (client.child.kill("SIGKILL")) {
1111
+ if (this.eventMap.PROCESS_KILLED) this.eventMap.PROCESS_KILLED(client, reason, true);
1112
+ } else {
1113
+ if (this.eventMap.ERROR) this.eventMap.ERROR(`Failed to kill process for cluster ${client.id}`);
1114
+ client.child.kill("SIGKILL");
1115
+ }
1116
+ try {
1117
+ process.kill(-client.child.pid);
1118
+ } catch {
1119
+ }
1085
1120
  } else {
1086
- if (this.eventMap.ERROR) this.eventMap.ERROR(`Failed to kill process for cluster ${client.id}`);
1087
- client.child.kill("SIGKILL");
1121
+ if (this.eventMap.PROCESS_KILLED) this.eventMap.PROCESS_KILLED(client, reason, false);
1088
1122
  }
1089
- } else {
1090
- if (this.eventMap.PROCESS_KILLED) this.eventMap.PROCESS_KILLED(client, reason, false);
1091
- }
1092
- this.clients.delete(client.id);
1093
- this.setClusterStopped(client, reason);
1123
+ this.clients.delete(client.id);
1124
+ this.setClusterStopped(client, reason);
1125
+ });
1094
1126
  }
1095
1127
  onMessage(client, message2) {
1096
1128
  if (message2.type === "CLUSTER_READY") {