Skip to content

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

Merged
merged 6 commits into from
May 14, 2025
Merged

Conversation

minottic
Copy link
Contributor

@minottic minottic commented May 5, 2025

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

  • allows sending to exchange set in config
  • channel.publish instead of channel.sendToQueue
  • uses async await and onModuleInit to make sure rabbitmq is set before sending message

Changes:

  • tests added
  • try catch decorator for reusability
  • config parser helper based on const list

Tests included

  • Included for each change/fix?
  • Passing?

Documentation

  • swagger documentation updated (required for API changes)
  • official documentation updated

official documentation info

@minottic minottic requested review from sbliven, nitrosx and Junjiequan May 5, 2025 07:04
@minottic minottic changed the title fix:send rabbitmq message to channel and refactor fix:send rabbitmq message to exchange and refactor May 5, 2025
@minottic minottic changed the title fix:send rabbitmq message to exchange and refactor fix: send rabbitmq message to exchange and refactor May 5, 2025
@minottic minottic force-pushed the send_to_exchange branch 5 times, most recently from 0307a54 to 0072cfb Compare May 5, 2025 09:18
@sbliven sbliven requested a review from despadam May 5, 2025 13:55
@minottic minottic mentioned this pull request May 6, 2025
4 tasks
minottic added a commit that referenced this pull request May 6, 2025
<!--
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 -->
totopoloco pushed a commit that referenced this pull request May 7, 2025
<!--
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 -->
@minottic minottic force-pushed the send_to_exchange branch from 0072cfb to bd7c5a0 Compare May 7, 2025 13:03
Copy link
Member

@sbliven sbliven left a 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 of sendToQueue, 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.

@minottic
Copy link
Contributor Author

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.

Copy link
Member

@despadam despadam left a 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.

Comment on lines +93 to +98
@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();
Copy link
Member

@Junjiequan Junjiequan May 14, 2025

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.

Comment on lines -19 to -30
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",
Copy link
Member

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

@minottic minottic merged commit 3e1cae0 into master May 14, 2025
13 checks passed
@minottic minottic deleted the send_to_exchange branch May 14, 2025 14:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants