MCPcopy Create free account
hub / github.com/esmBot/esmBot / createManageServer

Function createManageServer

src/cluster/common.ts:104–229  ·  view source on GitHub ↗
(
  port: string,
  metrics: () => MetricsInfo,
  restart: (id: number) => boolean,
  restartAll: () => void,
  recalcShards?: (shards: number[][]) => void,
)

Source from the content-addressed store, hash-verified

102}
103
104export async function createManageServer(
105 port: string,
106 metrics: () => MetricsInfo,
107 restart: (id: number) => boolean,
108 restartAll: () => void,
109 recalcShards?: (shards: number[][]) => void,
110) {
111 const database = await dbInit();
112 const httpServer = createServer(async (req, res) => {
113 if (req.method !== "GET") {
114 res.statusCode = 405;
115 return res.end("GET only");
116 }
117 if (!req.url) throw Error("URL not found");
118
119 const reqUrl = new URL(req.url, `http://${req.headers.host}`);
120 if (reqUrl.pathname === "/" || reqUrl.pathname === "/metrics") {
121 res.setHeader("Content-Type", "text/plain; version=0.0.4; charset=utf-8");
122 res.write(`# HELP esmbot_command_count Number of times a command has been run
123# TYPE esmbot_command_count counter
124`);
125 if (database) {
126 const counts = await database.getCounts(true);
127 for (const [i, w] of counts.entries()) {
128 res.write(`esmbot_command_count{command="${i}"} ${w}\n`);
129 }
130 }
131
132 const info = metrics();
133
134 res.write(`# HELP esmbot_servers Number of servers/guilds the bot is in
135# TYPE esmbot_servers gauge
136esmbot_servers ${info.servers}
137# HELP esmbot_shards Number of shards the bot has
138# TYPE esmbot_shards gauge
139esmbot_shards ${info.shards.length}
140# HELP esmbot_shard_ping Latency of each of the bot's shards
141# TYPE esmbot_shard_ping gauge
142`);
143
144 for (const shard of info.shards) {
145 if (shard.latency)
146 res.write(`esmbot_shard_ping{shard="${shard.id}", status="${shard.status}"} ${shard.latency}\n`);
147 }
148
149 res.write(`# HELP esmbot_total_mem Total memory usage of the bot
150# TYPE esmbot_total_mem gauge
151esmbot_total_mem ${info.totalMem}
152`);
153
154 return res.end();
155 }
156
157 if (reqUrl.pathname === "/shard") {
158 if (!reqUrl.searchParams.has("id")) {
159 res.statusCode = 400;
160 return res.end("400 Bad Request");
161 }

Callers 1

node.tsFile · 0.90

Calls 3

getGatewayDataFunction · 0.85
calcShardsFunction · 0.85
getCountsMethod · 0.45

Tested by

no test coverage detected