fix: cloud tasks not being queued and failing silently

This commit is contained in:
2024-10-14 08:28:22 +08:00
parent 91dd250823
commit 37cf7bd738
2 changed files with 12 additions and 2 deletions

View File

@@ -0,0 +1,5 @@
export class FailedToQueueTaskError extends Error {
constructor(status: number, bodyText: string) {
super(`Failed to queue task; status: ${status}, body: ${bodyText}`);
}
}

View File

@@ -4,6 +4,7 @@ import { DateTime, type DurationLike } from "luxon";
import type { Env } from "~/types/env"; import type { Env } from "~/types/env";
import type { WatchStatus } from "~/types/title/watchStatus"; import type { WatchStatus } from "~/types/title/watchStatus";
import { FailedToQueueTaskError } from "../errors/FailedToQueueTask";
import { getAdminSdkCredentials } from "../gcloud/getAdminSdkCredentials"; import { getAdminSdkCredentials } from "../gcloud/getAdminSdkCredentials";
import { getGoogleAuthToken } from "../gcloud/getGoogleAuthToken"; import { getGoogleAuthToken } from "../gcloud/getGoogleAuthToken";
import { getCurrentDomain } from "../getCurrentDomain"; import { getCurrentDomain } from "../getCurrentDomain";
@@ -47,7 +48,7 @@ export async function queueTask(
const adminSdkCredentials = getAdminSdkCredentials(env); const adminSdkCredentials = getAdminSdkCredentials(env);
const { projectId } = adminSdkCredentials; const { projectId } = adminSdkCredentials;
await fetch( const res = await fetch(
`https://content-cloudtasks.googleapis.com/v2/projects/${projectId}/locations/northamerica-northeast1/queues/${queueName}/tasks?alt=json`, `https://content-cloudtasks.googleapis.com/v2/projects/${projectId}/locations/northamerica-northeast1/queues/${queueName}/tasks?alt=json`,
{ {
headers: { headers: {
@@ -67,6 +68,10 @@ export async function queueTask(
method: "POST", method: "POST",
}, },
); );
if (!res.ok) {
throw new FailedToQueueTaskError(res.status, await res.text());
}
} }
function buildTask( function buildTask(
@@ -99,7 +104,7 @@ function buildTask(
httpRequest: { httpRequest: {
url: `${domain}/internal/new-episode`, url: `${domain}/internal/new-episode`,
httpMethod: "POST", httpMethod: "POST",
body: JSON.stringify(body), body: Buffer.from(JSON.stringify(body)).toString("base64"),
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
"X-Anilist-Token": headers?.["X-Anilist-Token"], "X-Anilist-Token": headers?.["X-Anilist-Token"],