refactor: update script
* runs promises serially instead of running them all at once * directly calls /new-episode route for latest episode
This commit is contained in:
5
src/libs/promiseQueue.ts
Normal file
5
src/libs/promiseQueue.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
export async function promiseQueue(queue: (() => Promise<void>)[]) {
|
||||||
|
for (const promise of queue) {
|
||||||
|
await promise();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import { fetchTitleFromAnilist } from "~/libs/anilist/getTitle";
|
import { fetchTitleFromAnilist } from "~/libs/anilist/getTitle";
|
||||||
|
import { promiseQueue } from "~/libs/promiseQueue";
|
||||||
import { queueTask } from "~/libs/tasks/queueTask";
|
import { queueTask } from "~/libs/tasks/queueTask";
|
||||||
import { getDb } from "~/models/db";
|
import { getDb } from "~/models/db";
|
||||||
import { watchStatusTable } from "~/models/schema";
|
import { watchStatusTable } from "~/models/schema";
|
||||||
@@ -13,8 +14,9 @@ if (isDevMode) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
await getTitleIds().then((titles) =>
|
await getTitleIds().then((titles) =>
|
||||||
Promise.all(
|
promiseQueue(
|
||||||
titles.map((title) =>
|
titles.map(
|
||||||
|
(title) => () =>
|
||||||
triggerNextEpisodeRoute(title).then((success) =>
|
triggerNextEpisodeRoute(title).then((success) =>
|
||||||
console.log(
|
console.log(
|
||||||
`Triggered next episode route for title ${title}: ${success}`,
|
`Triggered next episode route for title ${title}: ${success}`,
|
||||||
@@ -33,7 +35,13 @@ function getTitleIds() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function triggerNextEpisodeRoute(titleId: number) {
|
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) {
|
if (!title) {
|
||||||
console.error(`Failed to fetch title ${titleId}`);
|
console.error(`Failed to fetch title ${titleId}`);
|
||||||
return false;
|
return false;
|
||||||
@@ -61,8 +69,7 @@ async function triggerNextEpisodeRoute(titleId: number) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isDevMode) {
|
return fetch(`${serverUrl}/internal/new-episode`, {
|
||||||
return fetch("http://127.0.0.1:8080/internal/new-episode", {
|
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
@@ -72,31 +79,21 @@ async function triggerNextEpisodeRoute(titleId: number) {
|
|||||||
episodeNumber: mostRecentEpisodeNumber,
|
episodeNumber: mostRecentEpisodeNumber,
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
.then((res) => res.json() as Promise<{ success: boolean }>)
|
.then(
|
||||||
.then(({ success, error }) => {
|
(res) =>
|
||||||
|
res.json() as Promise<{ success: boolean; message?: string }>,
|
||||||
|
)
|
||||||
|
.then((response) => {
|
||||||
|
const { success } = response;
|
||||||
if (!success) {
|
if (!success) {
|
||||||
console.error(
|
console.error(
|
||||||
`Failed to trigger next episode route for title ${titleId} (most recent episode: ${mostRecentEpisodeNumber})`,
|
`Failed to trigger next episode route for title ${titleId} (most recent episode: ${mostRecentEpisodeNumber})`,
|
||||||
error,
|
response,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return success;
|
return success;
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
return queueTask(process.env, "new-episode", {
|
|
||||||
aniListId: titleId,
|
|
||||||
episodeNumber: mostRecentEpisodeNumber,
|
|
||||||
})
|
|
||||||
.then(() => true)
|
|
||||||
.catch((error) => {
|
|
||||||
console.error(
|
|
||||||
`Failed to trigger next episode route for title ${titleId} (most recent episode: ${mostRecentEpisodeNumber})`,
|
|
||||||
error,
|
|
||||||
);
|
|
||||||
return false;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!wasSuccessful) {
|
if (!wasSuccessful) {
|
||||||
|
|||||||
Reference in New Issue
Block a user