chore: workflow internals improvementss (#9455)
This commit is contained in:
@@ -22,7 +22,7 @@ describe("LocalEventBusService", () => {
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks()
|
||||
|
||||
eventBus = new LocalEventBusService(moduleDeps as any)
|
||||
eventBus = new LocalEventBusService(moduleDeps as any, {}, {} as any)
|
||||
eventEmitter = (eventBus as any).eventEmitter_
|
||||
})
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import {
|
||||
Event,
|
||||
EventBusTypes,
|
||||
InternalModuleDeclaration,
|
||||
Logger,
|
||||
MedusaContainer,
|
||||
Message,
|
||||
@@ -25,7 +26,11 @@ export default class LocalEventBusService extends AbstractEventBusModuleService
|
||||
protected readonly eventEmitter_: EventEmitter
|
||||
protected groupedEventsMap_: StagingQueueType
|
||||
|
||||
constructor({ logger }: MedusaContainer & InjectedDependencies) {
|
||||
constructor(
|
||||
{ logger }: MedusaContainer & InjectedDependencies,
|
||||
moduleOptions = {},
|
||||
moduleDeclaration: InternalModuleDeclaration
|
||||
) {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
@@ -54,16 +59,16 @@ export default class LocalEventBusService extends AbstractEventBusModuleService
|
||||
eventData.name
|
||||
)
|
||||
|
||||
if (eventListenersCount === 0) {
|
||||
continue
|
||||
}
|
||||
|
||||
if (!options.internal && !eventData.options?.internal) {
|
||||
this.logger_?.info(
|
||||
`Processing ${eventData.name} which has ${eventListenersCount} subscribers`
|
||||
)
|
||||
}
|
||||
|
||||
if (eventListenersCount === 0) {
|
||||
continue
|
||||
}
|
||||
|
||||
await this.groupOrEmitEvent(eventData)
|
||||
}
|
||||
}
|
||||
@@ -114,6 +119,10 @@ export default class LocalEventBusService extends AbstractEventBusModuleService
|
||||
}
|
||||
|
||||
subscribe(event: string | symbol, subscriber: Subscriber): this {
|
||||
if (!this.isWorkerMode) {
|
||||
return this
|
||||
}
|
||||
|
||||
const randId = ulid()
|
||||
this.storeSubscribers({ event, subscriberId: randId, subscriber })
|
||||
this.eventEmitter_.on(event, async (data: Event) => {
|
||||
@@ -133,6 +142,10 @@ export default class LocalEventBusService extends AbstractEventBusModuleService
|
||||
subscriber: Subscriber,
|
||||
context?: EventBusTypes.SubscriberContext
|
||||
): this {
|
||||
if (!this.isWorkerMode) {
|
||||
return this
|
||||
}
|
||||
|
||||
const existingSubscribers = this.eventToSubscribersMap_.get(event)
|
||||
|
||||
if (existingSubscribers?.length) {
|
||||
|
||||
@@ -60,8 +60,7 @@ export default class RedisEventBusService extends AbstractEventBusModuleService
|
||||
})
|
||||
|
||||
// Register our worker to handle emit calls
|
||||
const shouldStartWorker = moduleDeclaration.worker_mode !== "server"
|
||||
if (shouldStartWorker) {
|
||||
if (this.isWorkerMode) {
|
||||
this.bullWorker_ = new Worker(
|
||||
moduleOptions.queueName ?? "events-queue",
|
||||
this.worker_,
|
||||
@@ -116,7 +115,7 @@ export default class RedisEventBusService extends AbstractEventBusModuleService
|
||||
// options for a particular event
|
||||
...eventData.options,
|
||||
},
|
||||
}
|
||||
} as any
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
+2
-5
@@ -140,13 +140,10 @@ export class InMemoryDistributedTransactionStorage
|
||||
})
|
||||
}
|
||||
|
||||
const stringifiedData = JSON.stringify(data)
|
||||
const parsedData = JSON.parse(stringifiedData)
|
||||
|
||||
if (hasFinished && !retentionTime && !idempotent) {
|
||||
await this.deleteFromDb(parsedData)
|
||||
await this.deleteFromDb(data)
|
||||
} else {
|
||||
await this.saveToDb(parsedData)
|
||||
await this.saveToDb(data)
|
||||
}
|
||||
|
||||
if (hasFinished) {
|
||||
|
||||
@@ -236,7 +236,6 @@ export class RedisDistributedTransactionStorage
|
||||
}
|
||||
|
||||
const stringifiedData = JSON.stringify(data)
|
||||
const parsedData = JSON.parse(stringifiedData)
|
||||
|
||||
if (!hasFinished) {
|
||||
if (ttl) {
|
||||
@@ -247,9 +246,9 @@ export class RedisDistributedTransactionStorage
|
||||
}
|
||||
|
||||
if (hasFinished && !retentionTime && !idempotent) {
|
||||
await this.deleteFromDb(parsedData)
|
||||
await this.deleteFromDb(data)
|
||||
} else {
|
||||
await this.saveToDb(parsedData)
|
||||
await this.saveToDb(data)
|
||||
}
|
||||
|
||||
if (hasFinished) {
|
||||
|
||||
Reference in New Issue
Block a user