Skip to content

Commit

Permalink
EOD Reminders: Adds support for retries (#456)
Browse files Browse the repository at this point in the history
* EOD Reminders: Adds support for retries

* switch to remind sequentially

* throw for http error

* adds missing await
  • Loading branch information
rithviknishad committed May 19, 2024
1 parent d550c28 commit dbb2ee9
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 24 deletions.
20 changes: 11 additions & 9 deletions activities-bot/src/sendReminder.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { getContributors, getEODUpdates, sendSlackMessage } from "./utils.mjs";
import {
getContributors,
getEODUpdates,
sendSlackMessage,
withRetry,
} from "./utils.mjs";

const remind = async ({ slackId, updates }) => {
await sendSlackMessage(
Expand Down Expand Up @@ -35,14 +40,11 @@ async function main() {
const eodUpdates = await getEODUpdates();

console.info("⚙️ Reminding users...");
await Promise.all(
Object.entries(allContributors).map(([githubId, slackId]) =>
remind({
slackId,
updates: eodUpdates[githubId],
}),
),
);
for (const [githubId, slackId] of Object.entries(allContributors)) {
await withRetry(() => remind({ slackId, updates: eodUpdates[githubId] }), {
attempts: 3,
});
}
console.info("✅ Completed!");
}

Expand Down
61 changes: 46 additions & 15 deletions activities-bot/src/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ function isAllowedEvent(event) {
}
}

const throwForHttpError = async (promise) => {
const res = await promise;

if (!res.ok) {
throw new Error(await res.text());
}

return res;
};

const octokit = new Octokit({ auth: GITHUB_TOKEN });

export async function getEvents(allowedAuthors) {
Expand Down Expand Up @@ -112,17 +122,22 @@ const leaderboardApiHeaders = {
const eodUpdatesApi = `${LEADERBOARD_URL}/api/slack-eod-bot/eod-updates`;

export async function getEODUpdates() {
const res = await fetch(eodUpdatesApi, {
headers: leaderboardApiHeaders,
});
const res = await throwForHttpError(
fetch(eodUpdatesApi, {
headers: leaderboardApiHeaders,
}),
);

return res.json();
}

export async function flushEODUpdates() {
await fetch(eodUpdatesApi, {
headers: leaderboardApiHeaders,
method: "DELETE",
});
const res = await throwForHttpError(
fetch(eodUpdatesApi, {
headers: leaderboardApiHeaders,
method: "DELETE",
}),
);
}

const slackApiHeaders = {
Expand All @@ -131,15 +146,17 @@ const slackApiHeaders = {
};

export async function sendSlackMessage(channel, text, blocks) {
const res = await fetch("https://slack.com/api/chat.postMessage", {
method: "POST",
headers: slackApiHeaders,
body: JSON.stringify({
channel,
text,
...blocks,
const res = await throwForHttpError(
fetch("https://slack.com/api/chat.postMessage", {
method: "POST",
headers: slackApiHeaders,
body: JSON.stringify({
channel,
text,
...blocks,
}),
}),
});
);

const data = await res.json();
if (!data.ok) {
Expand Down Expand Up @@ -252,3 +269,17 @@ export async function postEODMessage({ github, slack, updates }) {
getHumanReadableUpdates(updates, slack, github),
);
}

export async function withRetry(method, { attempts }) {
while (attempts) {
try {
return await method();
} catch (error) {
attempts -= 1;

if (!attempts) {
throw error;
}
}
}
}

0 comments on commit dbb2ee9

Please sign in to comment.