Skip to content

Commit

Permalink
fix: add error logging and retry to threadCreate resume (#395)
Browse files Browse the repository at this point in the history
  • Loading branch information
kristersd authored Aug 7, 2024
1 parent ba3c491 commit 5ddaebb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
30 changes: 23 additions & 7 deletions src/features/resume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
InteractionType,
ComponentType,
ChannelType,
Message,
} from "discord.js";
import OpenAI from "openai";
import { CHANNELS } from "../constants/channels";
Expand Down Expand Up @@ -75,13 +76,18 @@ export const resumeResources = async (bot: Client) => {
const deferred = await interaction.deferReply({ ephemeral: true });
deferred.edit("Looking for a resume…");
const messages = await interaction.channel.messages.fetch();

const channel = interaction.channel;
const firstMessage = await retry(
() => channel.fetchStarterMessage(),
5,
10,
);

let firstMessage: Message<true> | null = null;
try {
firstMessage = await retry(() => channel.fetchStarterMessage(), {
retries: 5,
delayMs: 10,
});
} catch {
logger.log("RESUME", "Failed to fetch interaction first message");
}

if (!firstMessage) {
await interaction.reply({
ephemeral: true,
Expand Down Expand Up @@ -207,7 +213,17 @@ export const resumeResources = async (bot: Client) => {
if (thread.parentId !== CHANNELS.resumeReview) {
return;
}
const firstMessage = await thread.fetchStarterMessage();

let firstMessage: Message<true> | null = null;
try {
firstMessage = await retry(() => thread.fetchStarterMessage(), {
retries: 5,
delayMs: 10,
});
} catch {
logger.log("RESUME", "Failed to fetch thread first message");
}

if (!firstMessage) {
return;
}
Expand Down
8 changes: 4 additions & 4 deletions src/features/retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ as the first line of the `attempt` function produced:
*
* @template T
* @param {() => Promise<T>} fn - The function returning a promise to be retried.
* @param {number} [retries=3] - The number of retry attempts.
* @param {number} [delayMs=1000] - The initial delay in milliseconds before retrying.
* @param {{ retries?: number; delayMs?: number }} [options] - The retry options.
* @returns {Promise<T>} A promise that resolves with the result of the function or rejects after all retries have been exhausted.
*/
export function retry<T>(
fn: () => Promise<T>,
retries = 3,
delayMs = 1000,
options = { retries: 3, delayMs: 1000 },
): Promise<T> {
const { retries, delayMs } = options;

return new Promise((resolve, reject) => {
const attempt = (retryCount: number) => {
fn()
Expand Down

0 comments on commit 5ddaebb

Please sign in to comment.