fix: adjust task delay threshold to 9 hours

Updates the maximum delay for direct task queuing to 9 hours. This change ensures that tasks with delays exceeding this threshold are stored in KV for later processing.

The update also reflects the new delay threshold in the unit tests.
This commit is contained in:
2025-12-16 08:28:14 -05:00
parent 9b17f5bcfe
commit 1501aff3b6
2 changed files with 14 additions and 15 deletions

View File

@@ -35,8 +35,8 @@ describe("queueTask - delayed task handling", () => {
(globalThis as any).crypto = { randomUUID: vi.fn(() => "test-uuid-123") }; (globalThis as any).crypto = { randomUUID: vi.fn(() => "test-uuid-123") };
}); });
describe("tasks with delay <= 12 hours", () => { describe("tasks with delay <= 9 hours", () => {
it("queues task directly when delay is less than 12 hours", async () => { it("queues task directly when delay is less than 9 hours", async () => {
await queueTask( await queueTask(
"NEW_EPISODE", "NEW_EPISODE",
{ aniListId: 123, episodeNumber: 1 }, { aniListId: 123, episodeNumber: 1 },
@@ -52,12 +52,12 @@ describe("queueTask - delayed task handling", () => {
expect(kvPutSpy).not.toHaveBeenCalled(); expect(kvPutSpy).not.toHaveBeenCalled();
}); });
it("queues task directly when delay is exactly 12 hours", async () => { it("queues task directly when delay is exactly 9 hours", async () => {
await queueTask( await queueTask(
"NEW_EPISODE", "NEW_EPISODE",
{ aniListId: 456, episodeNumber: 2 }, { aniListId: 456, episodeNumber: 2 },
{ {
scheduleConfig: { delay: { hours: 12 } }, scheduleConfig: { delay: { hours: 9 } },
env: mockEnv, env: mockEnv,
}, },
); );
@@ -81,8 +81,8 @@ describe("queueTask - delayed task handling", () => {
}); });
}); });
describe("tasks with delay > 12 hours", () => { describe("tasks with delay > 9 hours", () => {
it("stores task in KV when delay exceeds 12 hours", async () => { it("stores task in KV when delay exceeds 9 hours", async () => {
await queueTask( await queueTask(
"NEW_EPISODE", "NEW_EPISODE",
{ aniListId: 111, episodeNumber: 4 }, { aniListId: 111, episodeNumber: 4 },
@@ -98,12 +98,12 @@ describe("queueTask - delayed task handling", () => {
expect(queueSendSpy).not.toHaveBeenCalled(); expect(queueSendSpy).not.toHaveBeenCalled();
}); });
it("stores task in KV when delay is 12 hours + 1 second", async () => { it("stores task in KV when delay is 9 hours + 1 second", async () => {
await queueTask( await queueTask(
"NEW_EPISODE", "NEW_EPISODE",
{ aniListId: 222, episodeNumber: 5 }, { aniListId: 222, episodeNumber: 5 },
{ {
scheduleConfig: { delay: { hours: 12, seconds: 1 } }, scheduleConfig: { delay: { hours: 9, seconds: 1 } },
env: mockEnv, env: mockEnv,
}, },
); );
@@ -176,7 +176,7 @@ describe("queueTask - delayed task handling", () => {
}); });
describe("epoch time scheduling", () => { describe("epoch time scheduling", () => {
it("queues directly when epoch time is within 12 hours", async () => { it("queues directly when epoch time is within 9 hours", async () => {
const futureTime = Math.floor(Date.now() / 1000) + 3600; // 1 hour from now const futureTime = Math.floor(Date.now() / 1000) + 3600; // 1 hour from now
await queueTask( await queueTask(
@@ -192,7 +192,7 @@ describe("queueTask - delayed task handling", () => {
expect(kvPutSpy).not.toHaveBeenCalled(); expect(kvPutSpy).not.toHaveBeenCalled();
}); });
it("stores in KV when epoch time is beyond 12 hours", async () => { it("stores in KV when epoch time is beyond 9 hours", async () => {
const futureTime = Math.floor(Date.now() / 1000) + 24 * 3600; // 24 hours from now const futureTime = Math.floor(Date.now() / 1000) + 24 * 3600; // 24 hours from now
await queueTask( await queueTask(

View File

@@ -40,17 +40,16 @@ export async function queueTask(
req?.header(), req?.header(),
); );
const MAX_DELAY_SECONDS = 12 * 60 * 60; // 43,200 seconds (12 hours) const MAX_DELAY_SECONDS = Duration.fromObject({ hours: 9 }).as("seconds");
// If delay exceeds 12 hours, store in KV for later processing // If delay exceeds 9 hours, store in KV for later processing
if (scheduleTime > MAX_DELAY_SECONDS) { if (scheduleTime > MAX_DELAY_SECONDS) {
if (!env || !env.DELAYED_TASKS) { if (!env || !env.DELAYED_TASKS) {
throw new Error("DELAYED_TASKS KV namespace not available"); throw new Error("DELAYED_TASKS KV namespace not available");
} }
const { generateTaskKey, serializeDelayedTask } = await import( const { generateTaskKey, serializeDelayedTask } =
"./delayedTask" await import("./delayedTask");
);
const taskId = crypto.randomUUID(); const taskId = crypto.randomUUID();
const scheduledEpochTime = Math.floor(Date.now() / 1000) + scheduleTime; const scheduledEpochTime = Math.floor(Date.now() / 1000) + scheduleTime;