Enqueue product sync delete event
This commit is contained in:
@@ -6,17 +6,17 @@ import { db } from "../database/db";
|
||||
import { Value } from "@sinclair/typebox/value";
|
||||
import type { TSchema } from "@sinclair/typebox";
|
||||
|
||||
export type EventSpec<P extends JsonValue = JsonValue, S extends JsonValue = JsonValue> = {
|
||||
export type EventSpec<P extends JsonValue, S extends JsonValue> = {
|
||||
payload: P,
|
||||
state: S
|
||||
}
|
||||
export type EventSource = "portal" | "printful" | "webflow";
|
||||
|
||||
type EventProcessor<S extends EventSpec> =
|
||||
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 EventTypeOptions<S extends EventSpec> = {
|
||||
type EventTypeOptions<S extends EventSpec<JsonValue, JsonValue>> = {
|
||||
schemas: {
|
||||
payload: TSchema & { static: S["payload"] },
|
||||
state: TSchema & { static: S["state"] },
|
||||
@@ -24,7 +24,7 @@ type EventTypeOptions<S extends EventSpec> = {
|
||||
processor: EventProcessor<S>,
|
||||
};
|
||||
|
||||
type EventQueueOptions<G extends string, M extends Record<string, EventSpec>> = {
|
||||
type EventQueueOptions<G extends string, M extends Record<string, EventSpec<JsonValue, JsonValue>>> = {
|
||||
group: G,
|
||||
cfg: {
|
||||
retry: {
|
||||
@@ -36,12 +36,12 @@ type EventQueueOptions<G extends string, M extends Record<string, EventSpec>> =
|
||||
timeoutMs: number,
|
||||
},
|
||||
},
|
||||
types: {
|
||||
events: {
|
||||
[T in keyof M]: EventTypeOptions<M[T]>
|
||||
},
|
||||
}
|
||||
|
||||
type EnqueueOptions<M extends Record<string, EventSpec>, T extends keyof M & string> = {
|
||||
type EnqueueOptions<M extends Record<string, EventSpec<JsonValue, JsonValue>>, T extends keyof M & string> = {
|
||||
source: EventSource,
|
||||
type: T,
|
||||
payload: M[T]["payload"]
|
||||
@@ -50,7 +50,7 @@ type EnqueueOptions<M extends Record<string, EventSpec>, T extends keyof M & str
|
||||
class AlreadyClaimedEventError extends Error { }
|
||||
class ConcurrencyLimitReachedError extends Error { }
|
||||
|
||||
export class EventQueue<G extends string, M extends Record<string, EventSpec>> {
|
||||
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;
|
||||
@@ -201,8 +201,8 @@ export class EventQueue<G extends string, M extends Record<string, EventSpec>> {
|
||||
|
||||
private async handleEvent(id: string, type: string, payload: JsonValue) {
|
||||
log.info({ id, payload }, "handling event");
|
||||
if (!(type in this.opt.types)) throw new Error(`Unknown event type: ${type}`);
|
||||
const cfg = this.opt.types[type as keyof M];
|
||||
if (!(type in this.opt.events)) throw new Error(`Unknown event type: ${type}`);
|
||||
const cfg = this.opt.events[type as keyof M];
|
||||
try {
|
||||
Value.Assert(cfg.schemas.payload, payload);
|
||||
await cfg.processor(id, payload, async (state) => {
|
||||
|
||||
Reference in New Issue
Block a user