(memOnly: boolean = false)
| 23 | let totalMem = 0; |
| 24 | |
| 25 | function updateStats(memOnly: boolean = false): Promise<void> { |
| 26 | return new Promise((resolve, reject) => { |
| 27 | if (!memOnly) { |
| 28 | serverCount = 0; |
| 29 | shardData = []; |
| 30 | } |
| 31 | responseCount = 0; |
| 32 | totalMem = process.memoryUsage().heapUsed; |
| 33 | clusterCount = processes.length; |
| 34 | let timeout: ReturnType<typeof setTimeout> | undefined; |
| 35 | const done: boolean[] = Array(clusterCount).fill(false); |
| 36 | for (const [i, worker] of processes.entries()) { |
| 37 | if (!worker.isConnected()) { |
| 38 | clusterCount -= 1; |
| 39 | if (responseCount >= clusterCount) { |
| 40 | logger.debug( |
| 41 | `Stats collected (clusterCount: ${clusterCount}, responseCount: ${responseCount}, serverCount: ${serverCount}, totalMem: ${totalMem})`, |
| 42 | ); |
| 43 | resolve(); |
| 44 | } |
| 45 | continue; |
| 46 | } |
| 47 | |
| 48 | const listener = (packet: IncomingProcMessage) => { |
| 49 | if (packet.data?.type === "serverCounts") { |
| 50 | const countData = packet as ServerCountMessage; |
| 51 | clearTimeout(timeout); |
| 52 | if (done[i]) return; |
| 53 | done[i] = true; |
| 54 | if (!memOnly) { |
| 55 | serverCount += countData.data.guilds; |
| 56 | shardData = [...shardData, ...countData.data.shards].sort((a, b) => a.id - b.id); |
| 57 | } |
| 58 | responseCount += 1; |
| 59 | totalMem += countData.data.mem; |
| 60 | if (responseCount >= clusterCount) { |
| 61 | worker.off("message", listener); |
| 62 | logger.debug( |
| 63 | `Stats collected (clusterCount: ${clusterCount}, responseCount: ${responseCount}, serverCount: ${serverCount}, totalMem: ${totalMem})`, |
| 64 | ); |
| 65 | resolve(); |
| 66 | } |
| 67 | timeout = setTimeout(() => { |
| 68 | worker.off("message", listener); |
| 69 | reject("Timed out while waiting for stats"); |
| 70 | }, 5000); |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | timeout = setTimeout(() => { |
| 75 | worker.off("message", listener); |
| 76 | reject("Timed out while waiting for stats"); |
| 77 | }, 5000); |
| 78 | |
| 79 | worker.on("message", listener); |
| 80 | worker.send({ |
| 81 | data: { |
| 82 | type: "serverCounts", |
no outgoing calls
no test coverage detected