Skip to content
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

Catch if Rabbit server is down #1

Closed
ebadia opened this issue Sep 6, 2018 · 8 comments
Closed

Catch if Rabbit server is down #1

ebadia opened this issue Sep 6, 2018 · 8 comments
Assignees
Labels
bug Something isn't working

Comments

@ebadia
Copy link

ebadia commented Sep 6, 2018

How can I check if the RabbitMQ server is down?
In the client, I shutdown RabbitMQ local server to check if I can catch the error but all I receive is:

(node:90094) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:5672
    at Object._errnoException (util.js:992:11)
    at _exceptionWithHostPort (util.js:1014:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1186:14)
(node:90094) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:90094) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:90094) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:5672
    at Object._errnoException (util.js:992:11)
    at _exceptionWithHostPort (util.js:1014:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1186:14)
(node:90094) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)

This happens as a response to

    this.client = new ClientRMQ({
      url: `${process.env.CLOUDAMQP_URL}`,
      queue: 'ampanova_mails',
      queueOptions: { durable: false }
    })
@AlariCode
Copy link
Owner

@ebadia, thanks for report! I'll look into this issue. I think I'll need to add additional check in package.

@ebadia
Copy link
Author

ebadia commented Sep 7, 2018

Thanks for making this work so well with nestjs! Just as an idea, I saw that in connect you only return the resolve case, perhaps rejecting the promise could help.

@AlariCode
Copy link
Owner

AlariCode commented Sep 7, 2018

@ebadia, I updated package to 0.1.1 with better connection management system.

Now you can catch error on client:

try {
       return this.client.send<string, string>({ cmd: 'test' }, msg);
} catch (err) {
      console.log('Error:' + err);
 }

And server and client will reconnect when RMQ server will be available (if it was down):
image

Furthermore, I added support for clustering. You can specify more then one url on client or server:

client = new ClientRMQ({
    urls: [`amqp://login:password@host`],
    queue: 'test',
    queueOptions: { durable: false }
}); 

Please note that now you need to use urls option instead of url and pass string[] instead of string.

@AlariCode AlariCode self-assigned this Sep 7, 2018
@AlariCode AlariCode added the bug Something isn't working label Sep 7, 2018
@ebadia
Copy link
Author

ebadia commented Sep 8, 2018

That's great, I'll give it a try. Thanks.

@ebadia
Copy link
Author

ebadia commented Sep 8, 2018

@AlariCode this solves the error problem when sending the message. And for sure it's great. But the system still returns an error message when I try to do the new ClientRMQ

(node:54282) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:5672

It seems that we have an unhandled promise rejection there too. This does not stops the app and the try/catch you have added helps a lot. But perhaps this "warning" should be considered for the future.

@AlariCode
Copy link
Owner

@ebadia this is strange. It connects only when first message is sent. This code:

const client = new ClientRMQ({
            urls: [`amqp://${config.default.localhost.amqp.login}:${config.default.localhost.amqp.password}@${config.default.localhost.amqp.host}`],
            queue: 'test',
            queueOptions: { durable: false },
        });

doesn't cause client to connect. And this one does:

this.client.send<string, string>({ cmd: 'test' }, msg);

Could you please show you code with error? This example catches an error perfectly:

@Controller()
export class AppController {
    client = new ClientRMQ({
        urls: [`amqp://${config.default.localhost.amqp.login}:${config.default.localhost.amqp.password}@${config.default.localhost.amqp.host}`],
        queue: 'test',
        queueOptions: { durable: false },
    });

    @Get('a')
    a(): Observable<string> {
        try {
            return this.client.send<string, string>({ cmd: 'test' }, 'test');
        } catch (err) {
            console.log('Got error: ' + err);
        }
    }
}

@ebadia
Copy link
Author

ebadia commented Sep 10, 2018

@AlariCode Sorry, my fault... I did not upgrade the package to incude the changes... Now the error message has disapeared. Thanks!

@AlariCode
Copy link
Owner

Thanks for bug report! I’m closing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants