From c2e4c19d4150e2d9092ad565674856b9545e9f10 Mon Sep 17 00:00:00 2001 From: Skillz Date: Fri, 30 Oct 2020 15:47:03 -0400 Subject: [PATCH] work --- src/module/bigbrainbot.ts | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/module/bigbrainbot.ts b/src/module/bigbrainbot.ts index d0d30b8f8..db30b173a 100644 --- a/src/module/bigbrainbot.ts +++ b/src/module/bigbrainbot.ts @@ -5,6 +5,7 @@ import { ClientOptions, EventHandlers } from "../types/options.ts"; import { botGatewayData } from "./client.ts"; const botOptions = { + createNextShard: false, workers: new Map(), eventHandlers: {} as EventHandlers, botGatewayData: {} as DiscordBotGatewayData, @@ -54,26 +55,29 @@ async function spawnBigBrainBotShards(shardID = 0, skipChecks = 0) { if (shardID >= botOptions.botGatewayData.shards) return; // 25 shards but shards start at 0 so we use 24 - const workerID = shardID % 24; + const workerID = shardID % botOptions.shardsPerWorker - 1; const worker = botOptions.workers.get(workerID) // High max concurrency allows starting shards faster if (skipChecks) { // If the worker exists we just need to add if (worker) { - addShardToWorker(workerID, shardID); + worker.postMessage({ type: "CREATE_SHARD", shardID, workerID, botOptions }); } else { - createShardWorker(workerID, shardID); + const path = new URL("./shard.ts", import.meta.url).toString(); + const newWorker = new Worker(path, { type: "module", deno: true }); + // Add to worker map + botOptions.workers.set(workerID, newWorker); + newWorker.postMessage({ type: "CREATE_SHARD", shardID, workerID, botOptions }); } spawnBigBrainBotShards(shardID + 1, skipChecks - 1); } // Make sure we can create a shard or we are waiting for shards to connect still. - if (createNextShard) { + if (botOptions.createNextShard) { // !(shardid % botOptions.botGatewayData.session_start_limit.max_concurrency) - createNextShard = false; - if (botOptions.botGatewayData.shards >= 25) createShardWorker(); + botOptions.createNextShard = false; // Start the next few shards based on max concurrency spawnBigBrainBotShards(shardID + 1, botOptions.botGatewayData.session_start_limit.max_concurrency); return; @@ -83,14 +87,6 @@ async function spawnBigBrainBotShards(shardID = 0, skipChecks = 0) { spawnBigBrainBotShards(shardID); } -export function createShardWorker(workerID: number, shardID: number) { - const path = new URL("./shard.ts", import.meta.url).toString(); - const shard = new Worker(path, { type: "module", deno: true }); - // Add to worker map - botOptions.workers.set(workerID, shard) - -} - export interface BigBrainBotOptions extends ClientOptions { /** This can be used to distribute your bot across different servers. For example, if you wanted 1 million shards per server you could control it using this. */ shards?: [number, number]; @@ -102,4 +98,6 @@ export interface BigBrainBotOptions extends ClientOptions { * @default 25 */ shardsPerWorker?: number; + /** The absolute file path to the file where the worker will run. */ + workerFilePath?: string; }