Majorly improve and simplify event queue
This commit is contained in:
@@ -1,44 +1,11 @@
|
||||
import { PrintfulClient, ProductSyncer, WebflowClient } from "@blade-and-brawn/commerce";
|
||||
import { env, log } from "../util";
|
||||
import { db } from "../database/db";
|
||||
import { EventQueue, type EventSpec } from "./event-queue";
|
||||
import { defineEventType, EventQueue } from "./event-queue";
|
||||
import { Type as t } from "@sinclair/typebox";
|
||||
import type { Static } from "@sinclair/typebox";
|
||||
import { Value } from "@sinclair/typebox/value";
|
||||
import { sql } from "kysely";
|
||||
|
||||
// product_sync_update
|
||||
const ProductSyncUpdatePayloadSchema = t.Object({
|
||||
session: t.Object({
|
||||
name: t.String(),
|
||||
id: t.String()
|
||||
}),
|
||||
filter: t.Optional(
|
||||
t.Object({
|
||||
pProductIds: t.Optional(t.Array(t.Number()))
|
||||
})
|
||||
)
|
||||
});
|
||||
type ProductSyncUpdatePayload = Static<typeof ProductSyncUpdatePayloadSchema>;
|
||||
const ProductSyncUpdateStateSchema = t.Union([
|
||||
t.Object({
|
||||
pProductIds: t.Optional(t.Array(t.Number()))
|
||||
}),
|
||||
t.Null()
|
||||
]);
|
||||
type ProductSyncUpdateState = Static<typeof ProductSyncUpdateStateSchema>;
|
||||
|
||||
// product_sync_delete
|
||||
const ProductSyncDeletePayloadSchema = t.Object({ wProductId: t.String() });
|
||||
type ProductSyncDeletePayload = Static<typeof ProductSyncDeletePayloadSchema>;
|
||||
const ProductSyncDeleteStateSchema = t.Union([t.Object({}), t.Null()]);
|
||||
type ProductSyncDeleteState = Static<typeof ProductSyncDeleteStateSchema>;
|
||||
|
||||
export type ProductSyncEvents = {
|
||||
product_sync_update: EventSpec<ProductSyncUpdatePayload, ProductSyncUpdateState>;
|
||||
product_sync_delete: EventSpec<ProductSyncDeletePayload, ProductSyncDeleteState>;
|
||||
};
|
||||
|
||||
export class CommerceService {
|
||||
readonly Printful = new PrintfulClient({
|
||||
token: env.PRINTFUL_AUTH,
|
||||
@@ -55,22 +22,40 @@ export class CommerceService {
|
||||
|
||||
class ProductSyncService {
|
||||
static readonly EVENT_GROUP = "product_sync";
|
||||
readonly Queue: EventQueue<"product_sync", ProductSyncEvents>;
|
||||
readonly Queue;
|
||||
private readonly ProductSyncer: ProductSyncer;
|
||||
|
||||
// TODO: make each service have their own logger, so we can enforce .child({component: <ServiceName>})
|
||||
|
||||
constructor(private readonly Printful: PrintfulClient, private readonly Webflow: WebflowClient) {
|
||||
this.ProductSyncer = new ProductSyncer(this.Printful, this.Webflow, log);
|
||||
this.Queue = new EventQueue<"product_sync", ProductSyncEvents>({
|
||||
this.Queue = new EventQueue({
|
||||
group: ProductSyncService.EVENT_GROUP,
|
||||
cfg: {
|
||||
retry: { limit: 3, delayMs: 10000 },
|
||||
concurrency: { limit: 1, timeoutMs: 600000 }
|
||||
},
|
||||
events: {
|
||||
"product_sync_update": {
|
||||
schemas: { payload: ProductSyncUpdatePayloadSchema, state: ProductSyncUpdateStateSchema },
|
||||
"product_sync_update": defineEventType({
|
||||
schemas: {
|
||||
payload: t.Object({
|
||||
session: t.Object({
|
||||
name: t.String(),
|
||||
id: t.String()
|
||||
}),
|
||||
filter: t.Optional(
|
||||
t.Object({
|
||||
pProductIds: t.Optional(t.Array(t.Number()))
|
||||
})
|
||||
)
|
||||
}),
|
||||
state: t.Union([
|
||||
t.Object({
|
||||
pProductIds: t.Optional(t.Array(t.Number()))
|
||||
}),
|
||||
t.Null()
|
||||
])
|
||||
},
|
||||
processor: async (id, payload, setState) => {
|
||||
await this.ProductSyncer.sync({
|
||||
filter: payload.filter,
|
||||
@@ -79,13 +64,16 @@ class ProductSyncService {
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
"product_sync_delete": {
|
||||
schemas: { payload: ProductSyncDeletePayloadSchema, state: ProductSyncDeleteStateSchema },
|
||||
}),
|
||||
"product_sync_delete": defineEventType({
|
||||
schemas: {
|
||||
payload: t.Object({ wProductId: t.String() }),
|
||||
state: t.Union([t.Object({}), t.Null()])
|
||||
},
|
||||
processor: async (id, payload, setState) => {
|
||||
await this.Webflow.Products.remove(payload.wProductId);
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -102,7 +90,7 @@ class ProductSyncService {
|
||||
.executeTakeFirst();
|
||||
if (!latestSync) return;
|
||||
|
||||
Value.Assert(ProductSyncUpdateStateSchema, latestSync.state);
|
||||
Value.Assert(this.Queue.events["product_sync_update"].schemas.state, latestSync.state);
|
||||
|
||||
return {
|
||||
startDate: latestSync.started_at,
|
||||
|
||||
Reference in New Issue
Block a user