diff --git a/src/libs/promiseQueue.ts b/src/libs/promiseQueue.ts new file mode 100644 index 0000000..9f30287 --- /dev/null +++ b/src/libs/promiseQueue.ts @@ -0,0 +1,5 @@ +export async function promiseQueue(queue: (() => Promise)[]) { + for (const promise of queue) { + await promise(); + } +} diff --git a/src/scripts/initializeNextEpisodeQueue.ts b/src/scripts/initializeNextEpisodeQueue.ts index 31a83fa..57eedb2 100644 --- a/src/scripts/initializeNextEpisodeQueue.ts +++ b/src/scripts/initializeNextEpisodeQueue.ts @@ -1,4 +1,5 @@ import { fetchTitleFromAnilist } from "~/libs/anilist/getTitle"; +import { promiseQueue } from "~/libs/promiseQueue"; import { queueTask } from "~/libs/tasks/queueTask"; import { getDb } from "~/models/db"; import { watchStatusTable } from "~/models/schema"; @@ -13,13 +14,14 @@ if (isDevMode) { } await getTitleIds().then((titles) => - Promise.all( - titles.map((title) => - triggerNextEpisodeRoute(title).then((success) => - console.log( - `Triggered next episode route for title ${title}: ${success}`, + promiseQueue( + titles.map( + (title) => () => + triggerNextEpisodeRoute(title).then((success) => + console.log( + `Triggered next episode route for title ${title}: ${success}`, + ), ), - ), ), ), ); @@ -33,7 +35,13 @@ function getTitleIds() { } async function triggerNextEpisodeRoute(titleId: number) { - const title = await fetchTitleFromAnilist(titleId); + let title; + try { + title = await fetchTitleFromAnilist(titleId); + } catch (error) { + console.error(`Failed to fetch title ${titleId}`, error); + return false; + } if (!title) { console.error(`Failed to fetch title ${titleId}`); return false; @@ -61,42 +69,31 @@ async function triggerNextEpisodeRoute(titleId: number) { return false; } - if (isDevMode) { - return fetch("http://127.0.0.1:8080/internal/new-episode", { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - aniListId: titleId, - episodeNumber: mostRecentEpisodeNumber, - }), - }) - .then((res) => res.json() as Promise<{ success: boolean }>) - .then(({ success, error }) => { - if (!success) { - console.error( - `Failed to trigger next episode route for title ${titleId} (most recent episode: ${mostRecentEpisodeNumber})`, - error, - ); - } - - return success; - }); - } else { - return queueTask(process.env, "new-episode", { + return fetch(`${serverUrl}/internal/new-episode`, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ aniListId: titleId, episodeNumber: mostRecentEpisodeNumber, - }) - .then(() => true) - .catch((error) => { + }), + }) + .then( + (res) => + res.json() as Promise<{ success: boolean; message?: string }>, + ) + .then((response) => { + const { success } = response; + if (!success) { console.error( `Failed to trigger next episode route for title ${titleId} (most recent episode: ${mostRecentEpisodeNumber})`, - error, + response, ); - return false; - }); - } + } + + return success; + }); }); if (!wasSuccessful) {