feat(core-shared/jobs): PayloadJobQueue
This commit is contained in:
28
packages/core-shared/src/jobs/payload-job-queue.test.ts
Normal file
28
packages/core-shared/src/jobs/payload-job-queue.test.ts
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
import { describe, it, expect, vi } from "vitest";
|
||||||
|
import { PayloadJobQueue } from "@/jobs/payload-job-queue";
|
||||||
|
|
||||||
|
describe("PayloadJobQueue", () => {
|
||||||
|
it("delegates to payload.jobs.queue with the task slug and input", async () => {
|
||||||
|
const queueMock = vi.fn().mockResolvedValue({ id: "payload-job-1" });
|
||||||
|
const payload = { jobs: { queue: queueMock } } as never;
|
||||||
|
const queue = new PayloadJobQueue(payload);
|
||||||
|
const result = await queue.enqueue("blog.republish", { id: 1 });
|
||||||
|
expect(queueMock).toHaveBeenCalledWith({
|
||||||
|
task: "blog.republish",
|
||||||
|
input: { id: 1 },
|
||||||
|
waitUntil: undefined,
|
||||||
|
});
|
||||||
|
expect(result).toEqual({ jobId: "payload-job-1" });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("forwards runAt as waitUntil", async () => {
|
||||||
|
const queueMock = vi.fn().mockResolvedValue({ id: "payload-job-2" });
|
||||||
|
const payload = { jobs: { queue: queueMock } } as never;
|
||||||
|
const queue = new PayloadJobQueue(payload);
|
||||||
|
const future = new Date(Date.now() + 5000);
|
||||||
|
await queue.enqueue("blog.task", {}, { runAt: future });
|
||||||
|
expect(queueMock).toHaveBeenCalledWith(
|
||||||
|
expect.objectContaining({ waitUntil: future }),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
20
packages/core-shared/src/jobs/payload-job-queue.ts
Normal file
20
packages/core-shared/src/jobs/payload-job-queue.ts
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
import type { Payload } from "payload";
|
||||||
|
import type { IJobQueue } from "./job-queue.interface";
|
||||||
|
|
||||||
|
export class PayloadJobQueue implements IJobQueue {
|
||||||
|
constructor(private readonly payload: Payload) {}
|
||||||
|
|
||||||
|
async enqueue<T>(
|
||||||
|
taskSlug: string,
|
||||||
|
input: T,
|
||||||
|
options?: { runAt?: Date },
|
||||||
|
): Promise<{ jobId: string }> {
|
||||||
|
const result = await this.payload.jobs.queue({
|
||||||
|
task: taskSlug,
|
||||||
|
input: input as never,
|
||||||
|
waitUntil: options?.runAt,
|
||||||
|
} as never);
|
||||||
|
const jobId = (result as { id: string | number }).id;
|
||||||
|
return { jobId: String(jobId) };
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user