From 5f978205c46bc65169ebf7e9a413ba45fd64f5c8 Mon Sep 17 00:00:00 2001 From: Rushil Perera Date: Sun, 6 Oct 2024 08:48:43 -0400 Subject: [PATCH] remove taskId from optional args of queueTask --- src/libs/tasks/queueTask.ts | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/libs/tasks/queueTask.ts b/src/libs/tasks/queueTask.ts index 691a738..182f708 100644 --- a/src/libs/tasks/queueTask.ts +++ b/src/libs/tasks/queueTask.ts @@ -25,8 +25,8 @@ type ScheduleConfig = | { epochTime: number; delay: never }; interface QueueTaskOptionalArgs { - taskId?: string; scheduleConfig?: ScheduleConfig; + /** when req is not provided, that means the task is being created locally */ req?: HonoRequest; } @@ -34,7 +34,7 @@ export async function queueTask( env: Env, queueName: QueueName, body: QueueBody[QueueName], - { taskId, scheduleConfig, req }: QueueTaskOptionalArgs = {}, + { scheduleConfig, req }: QueueTaskOptionalArgs = {}, ) { const domain = req ? getCurrentDomain(req) @@ -58,11 +58,10 @@ export async function queueTask( task: buildTask( projectId, queueName, - taskId, scheduleConfig, domain, body, - req.header(), + req?.header(), ), }), method: "POST", @@ -73,11 +72,10 @@ export async function queueTask( function buildTask( projectId: string, queueName: QueueName, - taskId: string | undefined, scheduleConfig: ScheduleConfig | undefined, domain: string, body: QueueBody[QueueName], - headers: Record, + headers: Record | undefined, ) { let scheduleTime: string | undefined; if (scheduleConfig) { @@ -88,11 +86,12 @@ function buildTask( scheduleTime = DateTime.now().plus(delay).toUTC().toISO(); } } + let taskId: string; switch (queueName) { case "new-episode": const { aniListId } = body as QueueBody["new-episode"]; - taskId ??= buildNewEpisodeTaskId(aniListId); + taskId = buildNewEpisodeTaskId(aniListId); return { name: `projects/${projectId}/locations/northamerica-northeast1/queues/${queueName}/tasks/${taskId}`, @@ -103,13 +102,13 @@ function buildTask( body: JSON.stringify(body), headers: { "Content-Type": "application/json", - "X-Anilist-Token": headers["X-Anilist-Token"], + "X-Anilist-Token": headers?.["X-Anilist-Token"], }, }, }; case "anilist": const { deviceId, titleId } = body as QueueBody["anilist"]; - taskId ??= buildAnilistRetryTaskId(deviceId, titleId); + taskId = buildAnilistRetryTaskId(deviceId, titleId); return { name: `projects/${projectId}/locations/northamerica-northeast1/queues/${queueName}/tasks/${taskId}`, @@ -120,7 +119,7 @@ function buildTask( body: JSON.stringify(body), headers: { "Content-Type": "application/json", - "X-Anilist-Token": headers["X-Anilist-Token"], + "X-Anilist-Token": headers?.["X-Anilist-Token"], }, }, };