From 42c041a4e083c30dd777735b32055dd32c663533 Mon Sep 17 00:00:00 2001 From: Rushil Perera Date: Mon, 21 Oct 2024 18:42:29 +0800 Subject: [PATCH] fix: hash task name when failing to queue new task this should help with gcp cloud task deduplication causing 409 errors --- src/libs/tasks/queueTask.ts | 60 +++++++++++++++++++++++++++++-------- 1 file changed, 47 insertions(+), 13 deletions(-) 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 {