Skip to main content

Class: EventBusService

Can keep track of multiple subscribers to different events and run the subscribers when events happen. Events will run asynchronously.

Constructors

constructor

new EventBusService(__namedParameters, config, singleton?)

Parameters

NameTypeDefault value
__namedParametersInjectedDependenciesundefined
configConfigModuleundefined
singletonbooleantrue

Defined in

packages/medusa/src/services/event-bus.ts:75

Properties

config_

Protected Readonly config_: ConfigModule

Defined in

packages/medusa/src/services/event-bus.ts:59


enqueue_

Protected enqueue_: Promise<void>

Defined in

packages/medusa/src/services/event-bus.ts:73


eventToSubscribersMap_

Protected Readonly eventToSubscribersMap_: Map<string | symbol, SubscriberDescriptor[]>

Defined in

packages/medusa/src/services/event-bus.ts:64


jobSchedulerService_

Protected Readonly jobSchedulerService_: default

Defined in

packages/medusa/src/services/event-bus.ts:63


logger_

Protected Readonly logger_: Logger

Defined in

packages/medusa/src/services/event-bus.ts:61


manager_

Protected Readonly manager_: EntityManager

Defined in

packages/medusa/src/services/event-bus.ts:60


queue_

Protected queue_: Bull

Defined in

packages/medusa/src/services/event-bus.ts:70


redisClient_

Protected Readonly redisClient_: Redis

Defined in

packages/medusa/src/services/event-bus.ts:68


redisSubscriber_

Protected Readonly redisSubscriber_: Redis

Defined in

packages/medusa/src/services/event-bus.ts:69


shouldEnqueuerRun

Protected shouldEnqueuerRun: boolean

Defined in

packages/medusa/src/services/event-bus.ts:71


stagedJobRepository_

Protected Readonly stagedJobRepository_: typeof StagedJobRepository

Defined in

packages/medusa/src/services/event-bus.ts:62


transactionManager_

Protected transactionManager_: undefined | EntityManager

Defined in

packages/medusa/src/services/event-bus.ts:72

Methods

createCronJob

createCronJob<T>(eventName, data, cron, handler, options?): Promise<void>

Registers a cron job.

Deprecated

All cron job logic has been refactored to the JobSchedulerService. This method will be removed in a future release.

Type parameters

Name
T

Parameters

NameTypeDescription
eventNamestringthe name of the event
dataTthe data to be sent with the event
cronstringthe cron pattern
handlerSubscriber<unknown>the handler to call on each cron job
options?CreateJobOptions-

Returns

Promise<void>

void

Defined in

packages/medusa/src/services/event-bus.ts:421


emit

emit<T>(eventName, data, options?): Promise<void | StagedJob>

Calls all subscribers when an event occurs.

Type parameters

Name
T

Parameters

NameTypeDescription
eventNamestringthe name of the event to be process.
dataTthe data to send to the subscriber.
optionsanyoptions to add the job with

Returns

Promise<void | StagedJob>

the job from our queue

Defined in

packages/medusa/src/services/event-bus.ts:225


enqueuer_

enqueuer_(): Promise<void>

Returns

Promise<void>

Defined in

packages/medusa/src/services/event-bus.ts:285


startEnqueuer

startEnqueuer(): void

Returns

void

Defined in

packages/medusa/src/services/event-bus.ts:275


stopEnqueuer

stopEnqueuer(): Promise<void>

Returns

Promise<void>

Defined in

packages/medusa/src/services/event-bus.ts:280


subscribe

subscribe(event, subscriber, context?): EventBusService

Adds a function to a list of event subscribers.

Parameters

NameTypeDescription
eventstring | symbolthe event that the subscriber will listen for.
subscriberSubscriber<unknown>the function to be called when a certain event
context?SubscriberContextcontext to use when attaching subscriber happens. Subscribers must return a Promise.

Returns

EventBusService

this

Defined in

packages/medusa/src/services/event-bus.ts:155


unsubscribe

unsubscribe(event, subscriber): EventBusService

Adds a function to a list of event subscribers.

Parameters

NameTypeDescription
eventstring | symbolthe event that the subscriber will listen for.
subscriberSubscriber<unknown>the function to be called when a certain event happens. Subscribers must return a Promise.

Returns

EventBusService

this

Defined in

packages/medusa/src/services/event-bus.ts:198


withTransaction

withTransaction(transactionManager): EventBusService

Parameters

NameType
transactionManagerany

Returns

EventBusService

Defined in

packages/medusa/src/services/event-bus.ts:123


worker_

worker_<T>(job): Promise<unknown>

Handles incoming jobs.

Type parameters

Name
T

Parameters

NameTypeDescription
jobBullJob<T>The job object

Returns

Promise<unknown>

resolves to the results of the subscriber calls.

Defined in

packages/medusa/src/services/event-bus.ts:320