Skip to content

Commit

Permalink
Merge pull request #9751 from delucca-workspaces/fix/server-transport…
Browse files Browse the repository at this point in the history
…-connection-hanging

fix(microservices): adds feedback message when RabbitMQ server connection hangs
  • Loading branch information
kamilmysliwiec committed Jun 14, 2022
2 parents 52cf0b7 + c9f5f01 commit 279f6fa
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/microservices/constants.ts
Expand Up @@ -52,3 +52,6 @@ export const MQTT_WILDCARD_ALL = '#';
export const ECONNREFUSED = 'ECONNREFUSED';
export const CONN_ERR = 'CONN_ERR';
export const EADDRINUSE = 'EADDRINUSE';

export const CONNECTION_FAILED_MESSAGE =
'Connection to transport failed. Trying to reconnect...';
8 changes: 7 additions & 1 deletion packages/microservices/server/server-rmq.ts
Expand Up @@ -5,8 +5,10 @@ import {
} from '@nestjs/common/utils/shared.utils';
import {
CONNECT_EVENT,
DISCONNECTED_RMQ_MESSAGE,
CONNECT_FAILED_EVENT,
CONNECTION_FAILED_MESSAGE,
DISCONNECT_EVENT,
DISCONNECTED_RMQ_MESSAGE,
NO_MESSAGE_HANDLER,
RQM_DEFAULT_IS_GLOBAL_PREFETCH_COUNT,
RQM_DEFAULT_NOACK,
Expand Down Expand Up @@ -95,6 +97,10 @@ export class ServerRMQ extends Server implements CustomTransportStrategy {
this.logger.error(DISCONNECTED_RMQ_MESSAGE);
this.logger.error(err);
});
this.server.on(CONNECT_FAILED_EVENT, (err: any) => {
this.logger.error(CONNECTION_FAILED_MESSAGE);
this.logger.error(err);
});
}

public createClient<T = any>(): T {
Expand Down
4 changes: 4 additions & 0 deletions packages/microservices/test/server/server-rmq.spec.ts
Expand Up @@ -52,6 +52,10 @@ describe('ServerRMQ', () => {
server.listen(callbackSpy);
expect(onStub.getCall(1).args[0]).to.be.equal('disconnect');
});
it('should bind "connectFailed" event to handler', () => {
server.listen(callbackSpy);
expect(onStub.getCall(2).args[0]).to.be.equal('connectFailed');
});
describe('when "start" throws an exception', () => {
it('should call callback with a thrown error as an argument', () => {
const error = new Error('random error');
Expand Down

0 comments on commit 279f6fa

Please sign in to comment.