diff --git a/src/libs/tasks/queueTask.ts b/src/libs/tasks/queueTask.ts index 05f94b6..43b2277 100644 --- a/src/libs/tasks/queueTask.ts +++ b/src/libs/tasks/queueTask.ts @@ -57,17 +57,7 @@ export async function queueTask( body, req?.header(), ); - const res = await fetch( - `https://content-cloudtasks.googleapis.com/v2/projects/${projectId}/locations/northamerica-northeast1/queues/${queueName}/tasks?alt=json`, - { - headers: { - Authorization: `Bearer ${await getGoogleAuthToken(adminSdkCredentials)}`, - "Content-Type": "application/json", - }, - body: JSON.stringify({ task }), - method: "POST", - }, - ); + const { res } = await queueCloudTask(task); if (!res.ok) { if (res.status === 409) { @@ -80,11 +70,55 @@ export async function queueTask( ) ) { return; + } else { + const hashedTaskName = await import("node:crypto").then( + ({ createHash }) => + createHash("sha256") + .update(task.name.split("/").at(-1)!) + .digest("hex"), + ); + console.log(hashedTaskName); + console.log( + { + ...task, + name: hashedTaskName, + }, + { + ...task, + name: + task.name.split("/").slice(0, -1).join("/") + + "/" + + hashedTaskName, + }, + ); + const { res } = await queueCloudTask({ + ...task, + name: + task.name.split("/").slice(0, -1).join("/") + "/" + hashedTaskName, + }); + if (!res.ok) { + throw new FailedToQueueTaskError(res.status, await res.text()); + } } } throw new FailedToQueueTaskError(res.status, await res.text()); } + + async function queueCloudTask(task: object) { + const res = await fetch( + `https://content-cloudtasks.googleapis.com/v2/projects/${projectId}/locations/northamerica-northeast1/queues/${queueName}/tasks?alt=json`, + { + headers: { + Authorization: `Bearer ${await getGoogleAuthToken(adminSdkCredentials)}`, + "Content-Type": "application/json", + }, + body: JSON.stringify({ task }), + method: "POST", + }, + ); + return { res }; + } } async function checkIfTaskExists( @@ -149,8 +183,8 @@ function buildTask( }, }, }; - case "anilist": - const { deviceId, titleId } = body as QueueBody["anilist"]; + case "anilist-updates": + const { deviceId, titleId } = body as QueueBody[typeof queueName]; taskId = buildAnilistRetryTaskId(deviceId, titleId); return {