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:
@@ -35,8 +35,8 @@ describe("queueTask - delayed task handling", () => {
|
||||
(globalThis as any).crypto = { randomUUID: vi.fn(() => "test-uuid-123") };
|
||||
});
|
||||
|
||||
describe("tasks with delay <= 12 hours", () => {
|
||||
it("queues task directly when delay is less than 12 hours", async () => {
|
||||
describe("tasks with delay <= 9 hours", () => {
|
||||
it("queues task directly when delay is less than 9 hours", async () => {
|
||||
await queueTask(
|
||||
"NEW_EPISODE",
|
||||
{ aniListId: 123, episodeNumber: 1 },
|
||||
@@ -52,12 +52,12 @@ describe("queueTask - delayed task handling", () => {
|
||||
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(
|
||||
"NEW_EPISODE",
|
||||
{ aniListId: 456, episodeNumber: 2 },
|
||||
{
|
||||
scheduleConfig: { delay: { hours: 12 } },
|
||||
scheduleConfig: { delay: { hours: 9 } },
|
||||
env: mockEnv,
|
||||
},
|
||||
);
|
||||
@@ -81,8 +81,8 @@ describe("queueTask - delayed task handling", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("tasks with delay > 12 hours", () => {
|
||||
it("stores task in KV when delay exceeds 12 hours", async () => {
|
||||
describe("tasks with delay > 9 hours", () => {
|
||||
it("stores task in KV when delay exceeds 9 hours", async () => {
|
||||
await queueTask(
|
||||
"NEW_EPISODE",
|
||||
{ aniListId: 111, episodeNumber: 4 },
|
||||
@@ -98,12 +98,12 @@ describe("queueTask - delayed task handling", () => {
|
||||
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(
|
||||
"NEW_EPISODE",
|
||||
{ aniListId: 222, episodeNumber: 5 },
|
||||
{
|
||||
scheduleConfig: { delay: { hours: 12, seconds: 1 } },
|
||||
scheduleConfig: { delay: { hours: 9, seconds: 1 } },
|
||||
env: mockEnv,
|
||||
},
|
||||
);
|
||||
@@ -176,7 +176,7 @@ describe("queueTask - delayed task handling", () => {
|
||||
});
|
||||
|
||||
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
|
||||
|
||||
await queueTask(
|
||||
@@ -192,7 +192,7 @@ describe("queueTask - delayed task handling", () => {
|
||||
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
|
||||
|
||||
await queueTask(
|
||||
|
||||
@@ -40,17 +40,16 @@ export async function queueTask(
|
||||
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 (!env || !env.DELAYED_TASKS) {
|
||||
throw new Error("DELAYED_TASKS KV namespace not available");
|
||||
}
|
||||
|
||||
const { generateTaskKey, serializeDelayedTask } = await import(
|
||||
"./delayedTask"
|
||||
);
|
||||
const { generateTaskKey, serializeDelayedTask } =
|
||||
await import("./delayedTask");
|
||||
const taskId = crypto.randomUUID();
|
||||
const scheduledEpochTime = Math.floor(Date.now() / 1000) + scheduleTime;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user