feat: retry AniList "update watch status" request if it fails

This commit is contained in:
2024-09-06 17:54:16 -05:00
parent 7f950d5dc3
commit 3ded897b77
4 changed files with 47 additions and 2 deletions

View File

@@ -1,6 +1,8 @@
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
import { env } from "hono/adapter";
import { verifyQstashHeader } from "~/libs/qstash/verifyQstashHeader";
import { readEnvVariable } from "~/libs/readEnvVariable";
import { setWatchStatus } from "~/models/watchStatus";
import type { Env } from "~/types/env";
import {
@@ -70,7 +72,18 @@ app.openapi(route, async (c) => {
} = await c.req.json<typeof UpdateWatchStatusRequest._type>();
const aniListToken = c.req.header("X-AniList-Token");
if (!isRetrying) {
if (isRetrying) {
if (
!(await verifyQstashHeader(
env<Env, typeof c>(c, "workerd"),
c.req.path,
c.req.header("Upstash-Signature"),
await c.req.text(),
))
) {
return c.json(ErrorResponse, { status: 401 });
}
} else {
try {
const { wasAdded, wasDeleted } = await setWatchStatus(
env<Env, typeof c>(c, "workerd"),
@@ -95,6 +108,19 @@ app.openapi(route, async (c) => {
console.error(
new Error("Failed to update watch status on Anilist", { cause: error }),
);
await import("@upstash/qstash")
.then(
({ Client }) =>
new Client({ token: readEnvVariable(c.env, "QSTASH_TOKEN") }),
)
.then((client) =>
client.publishJSON({
url: c.req.url,
body: { deviceId, watchStatus, titleId, isRetrying: true },
retries: 0,
delay: 60,
}),
);
}
return c.json(SuccessResponse, { status: 200 });