-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@context/shared): rabbitMQ consumer and tests
- Loading branch information
Showing
5 changed files
with
169 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 17 additions & 5 deletions
22
src/contexts/shared/src/infrastructure/eventBus/rabbitmq/rabbitmqEnvironmentArranger.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/contexts/shared/src/infrastructure/eventBus/rabbitmq/rabbitmqEventBusConsumer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import DomainEvent from '@src/domain/eventBus/domainEvent'; | ||
import { DomainEventSubscriber } from '@src/domain/eventBus/domainEventSubscriber'; | ||
import { DomainEventUnmarshaller } from '@src/domain/eventBus/domainEventUnmarshaller'; | ||
import { EventBusConsumer } from '@src/domain/eventBus/eventBusConsumer'; | ||
import RabbitmqConnection from '@src/infrastructure/eventBus/rabbitmq/rabbitmqConnection'; | ||
import { ConsumeMessage } from 'amqplib'; | ||
|
||
export default class RabbitmqEventBusConsumer implements EventBusConsumer { | ||
private readonly connection: RabbitmqConnection; | ||
|
||
private readonly subscribers: DomainEventSubscriber<DomainEvent>[]; | ||
|
||
private readonly unmarshaller: DomainEventUnmarshaller; | ||
|
||
constructor(connection: RabbitmqConnection, subscribers: DomainEventSubscriber<DomainEvent>[], unmarshaller: DomainEventUnmarshaller) { | ||
this.connection = connection; | ||
this.subscribers = subscribers; | ||
this.unmarshaller = unmarshaller; | ||
} | ||
|
||
async start(): Promise<void> { | ||
await this.connection.connect(); | ||
|
||
await Promise.all(this.subscribers.map((s) => this.connection.consume(s.name(), this.consume(s)))); | ||
} | ||
|
||
private consume(subscriber: DomainEventSubscriber<DomainEvent>) { | ||
return async (message: ConsumeMessage): Promise<void> => { | ||
try { | ||
const event = this.unmarshaller.unmarshall(message.content.toString()); | ||
|
||
await subscriber.on(event); | ||
} catch { | ||
// TODO: ignore errors from unmashalling as it means the service is not interested in the event | ||
await this.connection.handleError(message, subscriber.name()); | ||
} finally { | ||
await this.connection.ack(message); | ||
} | ||
}; | ||
} | ||
} |