remove taskId from optional args of queueTask

This commit is contained in:
2024-10-06 08:48:43 -04:00
parent d13bc2a64e
commit 5f978205c4

View File

@@ -25,8 +25,8 @@ type ScheduleConfig =
| { epochTime: number; delay: never }; | { epochTime: number; delay: never };
interface QueueTaskOptionalArgs { interface QueueTaskOptionalArgs {
taskId?: string;
scheduleConfig?: ScheduleConfig; scheduleConfig?: ScheduleConfig;
/** when req is not provided, that means the task is being created locally */
req?: HonoRequest; req?: HonoRequest;
} }
@@ -34,7 +34,7 @@ export async function queueTask(
env: Env, env: Env,
queueName: QueueName, queueName: QueueName,
body: QueueBody[QueueName], body: QueueBody[QueueName],
{ taskId, scheduleConfig, req }: QueueTaskOptionalArgs = {}, { scheduleConfig, req }: QueueTaskOptionalArgs = {},
) { ) {
const domain = req const domain = req
? getCurrentDomain(req) ? getCurrentDomain(req)
@@ -58,11 +58,10 @@ export async function queueTask(
task: buildTask( task: buildTask(
projectId, projectId,
queueName, queueName,
taskId,
scheduleConfig, scheduleConfig,
domain, domain,
body, body,
req.header(), req?.header(),
), ),
}), }),
method: "POST", method: "POST",
@@ -73,11 +72,10 @@ export async function queueTask(
function buildTask( function buildTask(
projectId: string, projectId: string,
queueName: QueueName, queueName: QueueName,
taskId: string | undefined,
scheduleConfig: ScheduleConfig | undefined, scheduleConfig: ScheduleConfig | undefined,
domain: string, domain: string,
body: QueueBody[QueueName], body: QueueBody[QueueName],
headers: Record<string, string>, headers: Record<string, string> | undefined,
) { ) {
let scheduleTime: string | undefined; let scheduleTime: string | undefined;
if (scheduleConfig) { if (scheduleConfig) {
@@ -88,11 +86,12 @@ function buildTask(
scheduleTime = DateTime.now().plus(delay).toUTC().toISO(); scheduleTime = DateTime.now().plus(delay).toUTC().toISO();
} }
} }
let taskId: string;
switch (queueName) { switch (queueName) {
case "new-episode": case "new-episode":
const { aniListId } = body as QueueBody["new-episode"]; const { aniListId } = body as QueueBody["new-episode"];
taskId ??= buildNewEpisodeTaskId(aniListId); taskId = buildNewEpisodeTaskId(aniListId);
return { return {
name: `projects/${projectId}/locations/northamerica-northeast1/queues/${queueName}/tasks/${taskId}`, name: `projects/${projectId}/locations/northamerica-northeast1/queues/${queueName}/tasks/${taskId}`,
@@ -103,13 +102,13 @@ function buildTask(
body: JSON.stringify(body), body: JSON.stringify(body),
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
"X-Anilist-Token": headers["X-Anilist-Token"], "X-Anilist-Token": headers?.["X-Anilist-Token"],
}, },
}, },
}; };
case "anilist": case "anilist":
const { deviceId, titleId } = body as QueueBody["anilist"]; const { deviceId, titleId } = body as QueueBody["anilist"];
taskId ??= buildAnilistRetryTaskId(deviceId, titleId); taskId = buildAnilistRetryTaskId(deviceId, titleId);
return { return {
name: `projects/${projectId}/locations/northamerica-northeast1/queues/${queueName}/tasks/${taskId}`, name: `projects/${projectId}/locations/northamerica-northeast1/queues/${queueName}/tasks/${taskId}`,
@@ -120,7 +119,7 @@ function buildTask(
body: JSON.stringify(body), body: JSON.stringify(body),
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
"X-Anilist-Token": headers["X-Anilist-Token"], "X-Anilist-Token": headers?.["X-Anilist-Token"],
}, },
}, },
}; };