#Common EventBus between modules

15 messages · Page 1 of 1 (latest)

naive cedar
#

I have two modules BanUserModule and SlashRequestModule. CqrsModule imported in each module. When I publish an event from UserModule, handler of that event which is located in PostModule not handling event. What should I do?

vocal gulch
#

The handler is most likely not in any module's providers array and thus not instantiated, otherwise it should work.

naive cedar
naive cedar
vocal gulch
#

Then you'll need to show some code

#

Also handlers can't be request scoped

naive cedar
#

thank you for your attention btw

naive cedar
# vocal gulch Then you'll need to show some code

Here is my Event try to handle:

export class DiscordBanUserEvent extends BaseDiscordEvent {
  constructor(props: EventProps<DiscordBanUserEvent>, correlationId: string) {
    super(correlationId);
    this.userId = props.userId;
    this.duration = props.duration;
  }
  userId?: BasePropType<string>;
  duration?: BasePropType<string | DurationEnums>;
}

Here is my Event Handler:

@EventsHandler(DiscordBanUserEvent)
export class BanUserEventHandler implements IEventHandler<DiscordBanUserEvent> {
  private readonly logger = new Logger(BanUserEventHandler.name);
  constructor(
    private readonly prismaService: PrismaService,
    private readonly configService: ConfigService,
    private readonly responseAdapter: DiscordResponseAdapter,
  ) {}

  async handle(event: DiscordBanUserEvent) {
    ...
  }

Here is the command handler which is working and publishing the DiscordBanUserEvent from eventbus.

@CommandHandler(DiscordRequestCommand)
export class DiscordRequestCommandHandler implements ICommandHandler<DiscordRequestCommand> {
  private readonly logger = new Logger(DiscordRequestCommandHandler.name);
  constructor(private readonly openaiAdapter: OpenAiAdapter, private eventBus: EventBus) {}
  async execute(command: DiscordRequestCommand): Promise<any> {
    ...
    const event = await this.apiAdapter.getCorrespondingDiscordEvent(command.commandInput, command.correlationId);
    this.eventBus.publish(event);
  }
}

When I debug I can see this.eventBus.publish() called with proper event.

Here is SlashRequestModule:

@Module({
  imports: [CqrsModule],
  providers: [DiscordRequestCommandHandler, SlashRequestController],
})
export class SlashRequestModule {}

Here is the BanUserModule:

@Module({
  providers: [BanUserEventHandler, ...controllers],
  imports: [InfrastructureModule, CqrsModule],
})
export class BanUserModule {}
#

there is no error at all

#

but BanUserEventHandler is not executing

#

okay I understand the problem

#

finally

#

lmao