| ... | @@ -6,7 +6,18 @@ import type { | ... | @@ -6,7 +6,18 @@ import type { |
| 6 | OrchestrationReadModel, | 6 | OrchestrationReadModel, |
| 7 | } from "@t3tools/contracts"; | 7 | } from "@t3tools/contracts"; |
| 8 | import { OrchestrationCommandSchema } from "@t3tools/contracts"; | 8 | import { OrchestrationCommandSchema } from "@t3tools/contracts"; |
| 9 | import { PubSub, Queue, Schema, Stream, Effect, Fiber, Runtime, Either } from "effect"; | 9 | import { |
| | 10 | PubSub, |
| | 11 | Queue, |
| | 12 | Schema, |
| | 13 | Stream, |
| | 14 | Effect, |
| | 15 | Fiber, |
| | 16 | Runtime, |
| | 17 | Either, |
| | 18 | Deferred, |
| | 19 | Cause, |
| | 20 | } from "effect"; |
| 10 | | 21 | |
| 11 | import { createLogger } from "../logger"; | 22 | import { createLogger } from "../logger"; |
| 12 | import type { OrchestrationEventStore } from "./eventStore"; | 23 | import type { OrchestrationEventStore } from "./eventStore"; |
| ... | @@ -16,10 +27,13 @@ import { UI_ENTITY_CONTRACTS } from "./uiContractInventory"; | ... | @@ -16,10 +27,13 @@ import { UI_ENTITY_CONTRACTS } from "./uiContractInventory"; |
| 16 | | 27 | |
| 17 | type CommandEnvelope = { | 28 | type CommandEnvelope = { |
| 18 | command: OrchestrationCommand; | 29 | command: OrchestrationCommand; |
| 19 | resolve: (result: { sequence: number }) => void; | 30 | result: Deferred.Deferred<{ sequence: number }, Error>; |
| 20 | reject: (error: Error) => void; | | |
| 21 | }; | 31 | }; |
| 22 | | 32 | |
| | 33 | function asError(error: unknown, fallbackMessage: string): Error { |
| | 34 | return error instanceof Error ? error : new Error(fallbackMessage); |
| | 35 | } |
| | 36 | |
| 23 | function mapCommandToEvent(command: OrchestrationCommand): Omit<OrchestrationEvent, "sequence"> { | 37 | function mapCommandToEvent(command: OrchestrationCommand): Omit<OrchestrationEvent, "sequence"> { |
| 24 | const eventId = crypto.randomUUID(); | 38 | const eventId = crypto.randomUUID(); |
| 25 | switch (command.type) { | 39 | switch (command.type) { |
| ... | @@ -218,43 +232,57 @@ export class OrchestrationEngine { | ... | @@ -218,43 +232,57 @@ export class OrchestrationEngine { |
| 218 | this.eventPubSub = Runtime.runSync(this.runtime)(PubSub.unbounded<OrchestrationEvent>()); | 232 | this.eventPubSub = Runtime.runSync(this.runtime)(PubSub.unbounded<OrchestrationEvent>()); |
| 219 | } | 233 | } |
| 220 | | 234 | |
| | 235 | private processEnvelope(envelope: CommandEnvelope): Effect.Effect<void> { |
| | 236 | return Effect.gen(this, function* () { |
| | 237 | const eventBase = mapCommandToEvent(envelope.command); |
| | 238 | const savedEvent = yield* this.eventStore.append(eventBase); |
| | 239 | |
| | 240 | yield* Effect.sync(() => { |
| | 241 | this.readModel = reduceEvent(this.readModel, savedEvent); |
| | 242 | }); |
| | 243 | |
| | 244 | const snapshot = this.readModel; |
| | 245 | yield* Effect.all([ |
| | 246 | PubSub.publish(this.eventPubSub, savedEvent), |
| | 247 | PubSub.publish(this.readModelPubSub, snapshot), |
| | 248 | ]); |
| | 249 | |
| | 250 | yield* Effect.sync(() => { |
| | 251 | for (const listener of this.domainEventListeners) { |
| | 252 | listener(savedEvent); |
| | 253 | } |
| | 254 | for (const listener of this.readModelListeners) { |
| | 255 | listener(snapshot); |
| | 256 | } |
| | 257 | }); |
| | 258 | |
| | 259 | yield* Deferred.succeed(envelope.result, { sequence: savedEvent.sequence }); |
| | 260 | }).pipe( |
| | 261 | Effect.catchAllCause((cause) => |
| | 262 | Deferred.fail( |
| | 263 | envelope.result, |
| | 264 | asError(Cause.squash(cause), "Unknown command processing error"), |
| | 265 | ).pipe(Effect.asVoid), |
| | 266 | ), |
| | 267 | ); |
| | 268 | } |
| | 269 | |
| | 270 | private bootstrapReadModel(): Effect.Effect<void> { |
| | 271 | return Effect.gen(this, function* () { |
| | 272 | const existingEvents = yield* this.eventStore.readAll(); |
| | 273 | yield* Effect.sync(() => { |
| | 274 | for (const event of existingEvents) { |
| | 275 | this.readModel = reduceEvent(this.readModel, event); |
| | 276 | } |
| | 277 | }); |
| | 278 | }); |
| | 279 | } |
| | 280 | |
| 221 | async start(): Promise<void> { | 281 | async start(): Promise<void> { |
| 222 | const existingEvents = await Runtime.runPromise(this.runtime)(this.eventStore.readAll()); | 282 | await Runtime.runPromise(this.runtime)(this.bootstrapReadModel()); |
| 223 | for (const event of existingEvents) { | | |
| 224 | this.readModel = reduceEvent(this.readModel, event); | | |
| 225 | } | | |
| 226 | | 283 | |
| 227 | const worker = Stream.fromQueue(this.commandQueue).pipe( | 284 | const worker = Stream.fromQueue(this.commandQueue).pipe( |
| 228 | Stream.runForEach((envelope) => | 285 | Stream.runForEach((envelope) => this.processEnvelope(envelope)), |
| 229 | Effect.tryPromise({ | | |
| 230 | try: async () => { | | |
| 231 | const eventBase = mapCommandToEvent(envelope.command); | | |
| 232 | const savedEvent = await Runtime.runPromise(this.runtime)( | | |
| 233 | this.eventStore.append(eventBase), | | |
| 234 | ); | | |
| 235 | this.readModel = reduceEvent(this.readModel, savedEvent); | | |
| 236 | await Promise.all([ | | |
| 237 | Runtime.runPromise(this.runtime)(PubSub.publish(this.eventPubSub, savedEvent)), | | |
| 238 | Runtime.runPromise(this.runtime)( | | |
| 239 | PubSub.publish(this.readModelPubSub, this.readModel), | | |
| 240 | ), | | |
| 241 | ]); | | |
| 242 | for (const listener of this.domainEventListeners) { | | |
| 243 | listener(savedEvent); | | |
| 244 | } | | |
| 245 | for (const listener of this.readModelListeners) { | | |
| 246 | listener(this.readModel); | | |
| 247 | } | | |
| 248 | envelope.resolve({ sequence: savedEvent.sequence }); | | |
| 249 | }, | | |
| 250 | catch: (error) => { | | |
| 251 | const message = | | |
| 252 | error instanceof Error ? error.message : "Unknown command processing error"; | | |
| 253 | envelope.reject(new Error(message)); | | |
| 254 | return undefined; | | |
| 255 | }, | | |
| 256 | }), | | |
| 257 | ), | | |
| 258 | ); | 286 | ); |
| 259 | | 287 | |
| 260 | this.workerFiber = Runtime.runFork(this.runtime)(worker); | 288 | this.workerFiber = Runtime.runFork(this.runtime)(worker); |
| ... | @@ -301,13 +329,13 @@ export class OrchestrationEngine { | ... | @@ -301,13 +329,13 @@ export class OrchestrationEngine { |
| 301 | } | 329 | } |
| 302 | | 330 | |
| 303 | async dispatch(command: OrchestrationCommand): Promise<{ sequence: number }> { | 331 | async dispatch(command: OrchestrationCommand): Promise<{ sequence: number }> { |
| 304 | return new Promise<{ sequence: number }>((resolve, reject) => { | 332 | const program = Effect.gen(this, function* () { |
| 305 | Runtime.runPromise(this.runtime)( | 333 | const result = yield* Deferred.make<{ sequence: number }, Error>(); |
| 306 | Queue.offer(this.commandQueue, { command, resolve, reject }), | 334 | yield* Queue.offer(this.commandQueue, { command, result }); |
| 307 | ).catch((error) => { | 335 | return yield* Deferred.await(result); |
| 308 | const message = error instanceof Error ? error.message : "Queue offer failed"; | 336 | }); |
| 309 | reject(new Error(message)); | 337 | return Runtime.runPromise(this.runtime)(program).catch((error) => { |
| 310 | }); | 338 | throw asError(error, "Queue offer failed"); |
| 311 | }); | 339 | }); |
| 312 | } | 340 | } |
| 313 | | 341 | |