feat(core-shared/jobs): InMemoryJobQueue with register()

This commit is contained in:
2026-05-08 11:35:50 +02:00
parent a2a83e1cf2
commit b96cff34ba
2 changed files with 90 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
import type { IJobQueue } from "./job-queue.interface";
export type InMemoryHandler = (input: unknown) => Promise<void> | void;
export class InMemoryJobQueue implements IJobQueue {
private counter = 0;
private readonly handlers: Record<string, InMemoryHandler>;
constructor(handlers: Record<string, InMemoryHandler> = {}) {
this.handlers = { ...handlers };
}
register(slug: string, handler: InMemoryHandler): void {
this.handlers[slug] = handler;
}
async enqueue<T>(
taskSlug: string,
input: T,
options?: { runAt?: Date },
): Promise<{ jobId: string }> {
const handler = this.handlers[taskSlug];
if (!handler) {
throw new Error(`no handler registered for task slug: ${taskSlug}`);
}
this.counter += 1;
const jobId = `in-memory-${this.counter}`;
const delay = options?.runAt ? options.runAt.getTime() - Date.now() : 0;
if (delay > 0) {
setTimeout(() => void handler(input), delay);
} else {
setImmediate(() => void handler(input));
}
return { jobId };
}
}