fix: Update postBotStats to output status codes from request

This commit is contained in:
Marius Begby committed 2023-09-04 09:29:48 +02:00
1 parent 6d12735dc5
commit 9e20805529
1 file changed
+17 -9
+17 -9
View File
@@ -13,7 +13,6 @@ export const postBotStats = async ({ client, executionId }: PostBotStatsParams)
shardId: client.shard?.ids[0]
});
try {
if (client.shard?.ids[0] !== 0) {
return;
}
@@ -23,6 +22,7 @@ export const postBotStats = async ({ client, executionId }: PostBotStatsParams)
const shardCount: number = client.shard.count;
const shardId: number = client.shard.ids[0];
try {
logger.debug('Gathering data about guild and member count from shards...');
await client!
.shard!.fetchClientValues('guilds.cache')
@@ -40,6 +40,11 @@ export const postBotStats = async ({ client, executionId }: PostBotStatsParams)
.catch((error) => {
logger.error(error, 'Failed to fetch client values from shards.');
});
} catch (error) {
logger.error(error, 'Failed to gather data about guild and member count from shards.');
}
logger.info(`Starting to post bot stats with guild count ${guildCount} and member count ${memberCount}...`);
/* eslint-disable camelcase */
const sites: PostBotStatsSite[] = [
@@ -122,13 +127,12 @@ export const postBotStats = async ({ client, executionId }: PostBotStatsParams)
}
];
logger.info(`Starting to post bot stats with guild count ${guildCount} and member count ${memberCount}...`);
const statusCodes: number[] = [];
sites.map((site) => {
try {
const statusPromises = sites.map((site) => {
return new Promise<number | null>((resolve) => {
if (!site.enabled) {
// return if site is disabled
resolve(null);
return;
}
@@ -149,20 +153,24 @@ export const postBotStats = async ({ client, executionId }: PostBotStatsParams)
res.statusCode === 200
? logger.debug(`Request to ${site.hostname}: statusCode: ${res.statusCode}`)
: logger.warn(`Request to ${site.hostname}: statusCode: ${res.statusCode}`);
statusCodes.push(res.statusCode);
resolve(res.statusCode);
} else {
logger.error(
`Request to ${site.hostname}: statusCode is undefined or not a number (${res.statusCode}).`
);
resolve(null);
}
});
request.write(JSON.stringify(site.body));
request.end();
});
});
logger.info(`Successfully posted bot stats with status codes ${statusCodes.join(', ')}.`);
const statusCodes = await Promise.all(statusPromises);
logger.info(
`Successfully posted bot stats with status codes ${statusCodes.filter((code) => code !== null).join(', ')}.`
);
} catch (error) {
logger.error(error, 'Failed to post bot stats to sites.');
}