fix: hash task name when failing to queue new task

this should help with gcp cloud task deduplication causing 409 errors
This commit is contained in:
2024-10-21 18:42:29 +08:00
parent 2a4e8144c4
commit 42c041a4e0

View File

@@ -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 {