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

View File

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

View File

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