fix: internal "new episode" route throwing error code 1042 in prod

This commit is contained in:
2024-09-10 23:04:06 -04:00
parent b3738184c9
commit 76ed45558d
3 changed files with 77 additions and 48 deletions

View File

@@ -43,14 +43,9 @@ const route = createRoute({
const app = new OpenAPIHono<Env>();
app.openapi(route, async (c) => {
const aniListId = Number(c.req.param("aniListId"));
const { result: episodes, errorOccurred } = await fetchFromMultipleSources([
() => {
const isAnifyEnabled = readEnvVariable<boolean>(c.env, "ENABLE_ANIFY");
return getEpisodesFromAnify(isAnifyEnabled, aniListId);
},
export function fetchEpisodes(aniListId: number, isAnifyEnabled: boolean) {
return fetchFromMultipleSources([
() => getEpisodesFromAnify(isAnifyEnabled, aniListId),
// () =>
// import("./consumet").then(({ getEpisodesFromConsumet }) =>
// getEpisodesFromConsumet(aniListId),
@@ -60,6 +55,15 @@ app.openapi(route, async (c) => {
getEpisodesFromAniwatch(aniListId),
),
]);
}
app.openapi(route, async (c) => {
const aniListId = Number(c.req.param("aniListId"));
const { result: episodes, errorOccurred } = await fetchEpisodes(
aniListId,
readEnvVariable<boolean>(c.env, "ENABLE_ANIFY"),
);
if (errorOccurred) {
return c.json(ErrorResponse, { status: 500 });

View File

@@ -68,28 +68,26 @@ const route = createRoute({
const app = new OpenAPIHono<Env>();
app.openapi(route, async (c) => {
const aniListId = Number(c.req.param("aniListId"));
const { provider, id } = await c.req.json<typeof FetchUrlRequest._type>();
const isAnifyEnabled = readEnvVariable(c.env, "ENABLE_ANIFY");
export async function fetchEpisodeUrl(
provider: string,
id: string,
aniListId: number,
isAnifyEnabled: boolean,
) {
if (provider === "consumet" || !isAnifyEnabled) {
try {
const result = await import("./consumet").then(
({ getSourcesFromConsumet }) => getSourcesFromConsumet(id),
);
if (!result) {
return c.json({ success: false }, { status: 404 });
return null;
}
return c.json({
success: true,
result,
});
return result;
} catch (e) {
console.error("Failed to fetch download URL from Consumet", e);
return c.json(ErrorResponse, { status: 500 });
throw e;
}
}
@@ -99,17 +97,14 @@ app.openapi(route, async (c) => {
({ getSourcesFromAniwatch }) => getSourcesFromAniwatch(id),
);
if (!result) {
return c.json({ success: false }, { status: 404 });
return null;
}
return c.json({
success: true,
result,
});
return result;
} catch (e) {
console.error("Failed to fetch download URL from Aniwatch", e);
return c.json(ErrorResponse, { status: 500 });
throw e;
}
}
@@ -118,16 +113,36 @@ app.openapi(route, async (c) => {
getSourcesFromAnify(provider, id, aniListId),
);
if (!result) {
return c.json({ success: false }, { status: 404 });
return null;
}
return c.json({
success: true,
result,
});
return result;
} catch (e) {
console.error("Failed to fetch download URL from Anify", e);
throw e;
}
}
app.openapi(route, async (c) => {
const aniListId = Number(c.req.param("aniListId"));
const { provider, id } = await c.req.json<typeof FetchUrlRequest._type>();
try {
const isAnifyEnabled = readEnvVariable<boolean>(c.env, "ENABLE_ANIFY");
const result = await fetchEpisodeUrl(
provider,
id,
aniListId,
isAnifyEnabled,
);
if (result) {
return c.json({ success: true, result });
} else {
return c.json(ErrorResponse, { status: 404 });
}
} catch (error) {
return c.json(ErrorResponse, { status: 500 });
}
});

View File

@@ -5,6 +5,8 @@ import { env } from "hono/adapter";
import mapKeys from "lodash.mapkeys";
import { z } from "zod";
import { fetchEpisodes } from "~/controllers/episodes/getByAniListId";
import { fetchEpisodeUrl } from "~/controllers/episodes/getEpisodeUrl";
import { Case, changeStringCase } from "~/libs/changeStringCase";
import type { AdminSdkCredentials } from "~/libs/fcm/getGoogleAuthToken";
import { sendFcmMessage } from "~/libs/fcm/sendFcmMessage";
@@ -48,10 +50,15 @@ app.post(
}
const domain = getCurrentDomain(c.req);
const { success, result: fetchEpisodesResult } = await fetch(
`${domain}/episodes/${aniListId}`,
).then((res) => res.json<EpisodesResponseSchema>());
if (!success) {
const isAnifyEnabled = readEnvVariable<boolean>(
env<Env, typeof c>(c, "workerd"),
"ENABLE_ANIFY",
);
const {
errorOccurred: fetchEpisodesErrorOccurred,
result: fetchEpisodesResult,
} = await fetchEpisodes(aniListId, isAnifyEnabled);
if (fetchEpisodesErrorOccurred) {
console.error(`Failed to fetch episodes for title ${aniListId}`);
await scheduleRetry(
readEnvVariable(env<Env, typeof c>(c, "workerd"), "QSTASH_TOKEN"),
@@ -72,20 +79,23 @@ app.post(
return c.json(ErrorResponse, { status: 404 });
}
const { success: fetchUrlSuccess, result: fetchUrlResult } = await fetch(
`${domain}/episodes/${aniListId}/url`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
id: episode.id,
provider: providerId,
}),
},
).then((res) => res.json<FetchUrlResponse>());
if (!fetchUrlSuccess) {
let fetchUrlResult;
try {
fetchUrlResult = await fetchEpisodeUrl(
providerId,
episode.id,
aniListId,
isAnifyEnabled,
);
if (!fetchUrlResult) {
console.error(`Failed to fetch episode URL for episode ${episode.id}`);
await scheduleRetry(
readEnvVariable(env<Env, typeof c>(c, "workerd"), "QSTASH_TOKEN"),
c.req,
);
return c.json(ErrorResponse, { status: 404 });
}
} catch (error) {
console.error(`Failed to fetch episode URL for episode ${episode.id}`);
await scheduleRetry(
readEnvVariable(env<Env, typeof c>(c, "workerd"), "QSTASH_TOKEN"),