Skip to content

Commit

Permalink
fix(alerting): fix osrs-tracker_process_players alerting not describi…
Browse files Browse the repository at this point in the history
…ng which players failed
  • Loading branch information
FreekMencke committed Aug 24, 2023
1 parent 5545686 commit 8a31159
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
21 changes: 6 additions & 15 deletions lambda/osrs-tracker_process-players/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,13 @@ export const handler = async (event: SQSEvent, context: Context) => {
// wait for all bulk writes to finish
const bulkWriteResults = await Promise.all(bulkWrites);
const updatedPlayerCount = bulkWriteResults.reduce((acc, result) => acc + result.modifiedCount, 0);
const failedMessagesCount = [...failedMap.values()].flat().length;

// throw error if no players were updated. Dont send new SQS messages or we will get stuck in a loop
if (updatedPlayerCount === 0) {
await discordAlert(
'No players were updated',
`Failed to update ${[...failedMap.values()].flat().length} players.`,
context,
);
await discordAlert('No players were updated', [...failedMap.values()].flat(), context);

throw new Error(`No players were updated. Failed to update ${[...failedMap.values()].flat().length} players.`);
throw new Error(`No players were updated. Failed to update ${failedMessagesCount} players.`);
}

// If some usernames updated successfully and some failed, send new SQS messages for the failed usernames
Expand All @@ -103,17 +100,11 @@ export const handler = async (event: SQSEvent, context: Context) => {

console.log(
`Updated ${updatedPlayerCount} players successfully.`,
`Failed to update ${[...failedMap.values()].flat().length} players.`,
`Failed to update ${failedMessagesCount} players.`,
);

if ([...failedMap.values()].flat().length) {
let description = `Failed to update ${[...failedMap.values()].flat().length} players:\n\n`;
description += [...failedMap.values()]
.flat()
.map((username) => `- ${username}`)
.join('\n');

await discordAlert('Failed to process some players', description, context);
if (failedMessagesCount) {
await discordAlert('Failed to process some players', [...failedMap.values()].flat(), context);
}

return context.logStreamName;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { DiscordWebhook } from '@osrs-tracker/discord-webhooks';
import { Context } from 'aws-lambda/handler';

export function discordAlert(title: string, description: string, context: Context) {
export function discordAlert(title: string, failedPlayers: string[], context: Context) {
const region = context.invokedFunctionArn.split(':')[3];

let description = `Failed to update ${failedPlayers.length} player${failedPlayers.length > 1 ? 's:' : ':'}\n`;
description += failedPlayers.map((username) => `- ${username}`).join('\n');

return DiscordWebhook.dispatch({
embeds: [
{
Expand Down

0 comments on commit 8a31159

Please sign in to comment.