-
Notifications
You must be signed in to change notification settings - Fork 29
fix: send rabbitmq message to exchange and refactor #1872
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
0307a54
to
0072cfb
Compare
<!-- Follow semantic-release guidelines for the PR title, which is used in the changelog. Title should follow the format `<type>(<scope>): <subject>`, where - Type is one of: build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test|BREAKING CHANGE - Scope (optional) describes the place of the change (eg a particular milestone) and is usually omitted - subject should be a non-capitalized one-line description in present imperative tense and not ending with a period See https://github.com/angular/angular.js/blob/master/DEVELOPERS.md#-git-commit-guidelines for more details. --> ## Description <!-- Short description of the pull request --> Remove variables that are not needed in tests. ## Motivation <!-- Background on use case, changes needed --> This will allow to raise error when connection to rabbitmq fails from #1872 ## Fixes <!-- Please provide a list of the issues fixed by this PR --> * Remove variables that are not needed in tests. * ## Changes: <!-- Please provide a list of the changes implemented by this PR --> * changes made ## Tests included - [ ] Included for each change/fix? - [ ] Passing? <!-- Merge will not be approved unless tests pass --> ## Documentation - [ ] swagger documentation updated (required for API changes) - [ ] official documentation updated ### official documentation info <!-- If you have updated the official documentation, please provide PR # and URL of the updated pages -->
<!-- Follow semantic-release guidelines for the PR title, which is used in the changelog. Title should follow the format `<type>(<scope>): <subject>`, where - Type is one of: build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test|BREAKING CHANGE - Scope (optional) describes the place of the change (eg a particular milestone) and is usually omitted - subject should be a non-capitalized one-line description in present imperative tense and not ending with a period See https://github.com/angular/angular.js/blob/master/DEVELOPERS.md#-git-commit-guidelines for more details. --> ## Description <!-- Short description of the pull request --> Remove variables that are not needed in tests. ## Motivation <!-- Background on use case, changes needed --> This will allow to raise error when connection to rabbitmq fails from #1872 ## Fixes <!-- Please provide a list of the issues fixed by this PR --> * Remove variables that are not needed in tests. * ## Changes: <!-- Please provide a list of the changes implemented by this PR --> * changes made ## Tests included - [ ] Included for each change/fix? - [ ] Passing? <!-- Merge will not be approved unless tests pass --> ## Documentation - [ ] swagger documentation updated (required for API changes) - [ ] official documentation updated ### official documentation info <!-- If you have updated the official documentation, please provide PR # and URL of the updated pages -->
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was a bit hard to review because of all the refactoring. If I read it right the new features are
- Single channel for all messages which stays open throughout the nest lifetime
- Use
publish
instead ofsendToQueue
, which allows rabbitMQ to do routing.
The OnError decorator is a bit weird. You never use the "log" feature, and passing an error prefix via _error
doesn't seem typesafe. I would have done error handling differently, but I guess this works.
essentially the only change is the first commit, the rest is refactoring. The decorator param (log) was necessary before #1886 and I was lazy and did not change it after that was merged (which also meant the BE started fine if no connection could be established). The channel stayed open even before since it was created in the constructor, but lifecycle hooks are used here to make sure stuff is initialized before being used. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, why have the "log" option in onError
if it's never used? Apart from that I like the refactoring, looks good.
@OnError() | ||
private async connect(): Promise<void> { | ||
this._error = "Cannot connect to RabbitMQ"; | ||
this.connection = await amqplibConnect(this.connectionOptions); | ||
this._error = "Channel error in RabbitMQService: "; | ||
this.channel = await this.connection.createChannel(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my view, the OnError
decorator brings unnecessary complexities. I personally would use try catch
or just .catch()
here to make it easy to read. But the idea here is to make it DRY I guess.
constructor(private readonly configService: ConfigService) { | ||
Logger.log("Initializing RabbitMQService.", "RabbitMQService"); | ||
|
||
const hostname = this.configService.get<string>("rabbitMq.hostname"); | ||
const port = this.configService.get<number>("rabbitMq.port"); | ||
const username = this.configService.get<string>("rabbitMq.username"); | ||
const password = this.configService.get<string>("rabbitMq.password"); | ||
constructor(private readonly configService: ConfigService) {} | ||
|
||
if (!hostname || !port || !username || !password) { | ||
Logger.error( | ||
"RabbitMQ is enabled but missing one or more config variables.", | ||
"RabbitMQService", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer the old code, as you can see the required fields immediately, but I'm not against having it programmatically either
Description
Send message to channel rather than to queue, which bypassed the bound exchange. Class refactored to use async await.
Motivation
Using the
channel.publish
method allows sending to the specified exchange and persists the message to the bound queue.Fixes
channel.publish
instead ofchannel.sendToQueue
Changes:
Tests included
Documentation
official documentation info