Majorly improve and simplify event queue

This commit is contained in:
Dominic Ferrando
2026-07-12 18:41:29 -04:00
parent 5572f9a957
commit 27c85e6676
3 changed files with 68 additions and 89 deletions
+35 -44
View File
@@ -4,60 +4,47 @@ import { log } from "../util";
import { sleep } from "bun";
import { db } from "../database/db";
import { Value } from "@sinclair/typebox/value";
import type { TSchema } from "@sinclair/typebox";
import type { TSchema, Static } from "@sinclair/typebox";
export type EventSpec<P extends JsonValue, S extends JsonValue> = {
payload: P,
state: S
}
export type EventSource = "portal" | "printful" | "webflow";
export type EventStatus = "pending" | "processing" | "failed" | "fulfilled";
type EventProcessor<S extends EventSpec<JsonValue, JsonValue>> =
(id: string, payload: S["payload"], setState: (state: S["state"]) => Promise<void>) => Promise<void>;
type EventStatus = "pending" | "processing" | "failed" | "fulfilled";
type EventProcessor<P extends TSchema, S extends TSchema> =
(id: string, payload: Static<P>, setState: (state: Static<S>) => Promise<void>) => Promise<void>;
type EventTypeOptions<S extends EventSpec<JsonValue, JsonValue>> = {
schemas: {
payload: TSchema & { static: S["payload"] },
state: TSchema & { static: S["state"] },
},
processor: EventProcessor<S>,
export type EventTypeOptions<P extends TSchema, S extends TSchema> = {
schemas: { payload: P, state: S },
processor: EventProcessor<P, S>,
};
type EventQueueOptions<G extends string, M extends Record<string, EventSpec<JsonValue, JsonValue>>> = {
group: G,
cfg: {
retry: {
limit: number,
delayMs: number,
},
concurrency: {
limit: number,
timeoutMs: number,
},
type EventQueueCfg = {
retry: {
limit: number,
delayMs: number,
},
events: {
[T in keyof M]: EventTypeOptions<M[T]>
concurrency: {
limit: number,
timeoutMs: number,
},
}
type EnqueueOptions<M extends Record<string, EventSpec<JsonValue, JsonValue>>, T extends keyof M & string> = {
source: EventSource,
type: T,
payload: M[T]["payload"]
}
};
class AlreadyClaimedEventError extends Error { }
class ConcurrencyLimitReachedError extends Error { }
export class EventQueue<G extends string, M extends Record<string, EventSpec<JsonValue, JsonValue>>> {
private _lastCleanDate: Date = new Date(0);
readonly cfg: EventQueueOptions<G, M>["cfg"];
readonly group: G;
export function defineEventType<P extends TSchema, S extends TSchema>(opt: EventTypeOptions<P, S>): EventTypeOptions<P, S> {
return opt;
}
constructor(private readonly opt: EventQueueOptions<G, M>) {
export class EventQueue<G extends string, T extends Record<string, EventTypeOptions<any, any>>> {
private _lastCleanDate: Date = new Date(0);
readonly cfg: EventQueueCfg;
readonly group: G;
readonly events: T;
constructor(opt: { group: G, cfg: EventQueueCfg, events: T }) {
this.cfg = opt.cfg;
this.group = opt.group;
this.events = opt.events;
}
get lastCleanDate() { return this._lastCleanDate; };
@@ -139,10 +126,14 @@ export class EventQueue<G extends string, M extends Record<string, EventSpec<Jso
return front;
}
async enqueue<T extends keyof M & string>({ source, type, payload }: EnqueueOptions<M, T>): Promise<string> {
log.info({ payload }, "enqueuing");
async enqueue<K extends keyof T & string>(opt: {
source: EventSource,
type: K,
payload: Static<T[K]["schemas"]["payload"]>
}): Promise<string> {
log.info({ payload: opt.payload }, "enqueuing");
const result = await db.insertInto("events")
.values({ group: this.group, type, source, payload })
.values({ group: this.group, type: opt.type, source: opt.source, payload: opt.payload as JsonValue })
.returning(["id"])
.executeTakeFirstOrThrow();
return result.id;
@@ -201,8 +192,8 @@ export class EventQueue<G extends string, M extends Record<string, EventSpec<Jso
private async processEvent(id: string, type: string, payload: JsonValue) {
log.info({ id, payload }, "processing event");
if (!(type in this.opt.events)) throw new Error(`Unknown event type: ${type}`);
const cfg = this.opt.events[type as keyof M];
if (!(type in this.events)) throw new Error(`event type not registerd in this queue: ${type}`);
const cfg = this.events[type as keyof T]!;
try {
Value.Assert(cfg.schemas.payload, payload);
await cfg.processor(id, payload, async (state) => {