diff --git a/src/cluster/common.ts b/src/cluster/common.ts index 9ebbec08..d43953d1 100644 --- a/src/cluster/common.ts +++ b/src/cluster/common.ts @@ -106,6 +106,7 @@ export async function createManageServer( metrics: () => MetricsInfo, restart: (id: number) => boolean, restartAll: () => void, + recalcShards?: (shards: number[][]) => void, ) { const database = await dbInit(); const httpServer = createServer(async (req, res) => { @@ -182,6 +183,17 @@ esmbot_total_mem ${info.totalMem} return res.end(JSON.stringify(procData)); } + if (reqUrl.pathname === "/recalc") { + const gatewayData = await getGatewayData(); + const shardArray = []; + for (let i = 0; i < gatewayData.shards; i++) { + shardArray.push(i); + } + const shardArrays = calcShards(shardArray, gatewayData.procAmount); + recalcShards?.(shardArrays); + return res.end(`Recalculated shards, the new recommended amount is ${gatewayData.shards}`); + } + if (reqUrl.pathname === "/restart") { if (!reqUrl.searchParams.has("id")) { res.statusCode = 400; diff --git a/src/cluster/node.ts b/src/cluster/node.ts index c9826704..a868f00b 100644 --- a/src/cluster/node.ts +++ b/src/cluster/node.ts @@ -185,6 +185,9 @@ if (port && port !== "") { }); } }, + (arrays) => { + shardArrays = arrays; + }, ); } @@ -204,7 +207,7 @@ const shardArray = []; for (let i = 0; i < shards; i++) { shardArray.push(i); } -const shardArrays = calcShards(shardArray, procAmount); +let shardArrays = calcShards(shardArray, procAmount); cluster.on("exit", async (worker, code, signal) => { const id = processes.findIndex((v) => worker.id === v.id);