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

Try if Redis server is running #138

Closed
juriansluiman opened this issue Oct 16, 2011 · 3 comments
Closed

Try if Redis server is running #138

juriansluiman opened this issue Oct 16, 2011 · 3 comments
Labels

Comments

@juriansluiman
Copy link

@juriansluiman juriansluiman commented Oct 16, 2011

I have a node.js server starting at boot with Ubuntu's upstart. Because the node.js server starts before Redis is up and running, redis.createClient() throws an exception:

node.js:134
        throw e; // process.nextTick error, or 'error' event on first tick
        ^
Error: Redis connection to 127.0.0.1:6379 failed - ECONNREFUSED, Connection refused
    at Socket.<anonymous> (/var/www/php-jobs/node_modules/redis/index.js:88:28)
    at Socket.emit (events.js:64:17)
    at Array.<anonymous> (net.js:830:27)
    at EventEmitter._tickCallback (node.js:126:26)

Of course I try to wrap it in a try/catch statement. The start of my server:

var redis  = require("redis");

function initializeRedis(callback) {
    (function createClient(){
        var client;
        try {
            client = redis.createClient();
        } catch (e) {
            setTimeout(createClient, 1000);
        }
        callback(client);
    })();
};

initializeRedis(function(client) {
  // Do things with client
});

However, this doesn't make any difference, I still got an exception. What can be the case and how can I make sure my server waits before Redis is there?

@mranney
Copy link
Contributor

@mranney mranney commented Nov 17, 2011

This is happening because the client is emitting an "error" event. In node, events named "error" are special. If you don't listen for them, they get converted into exceptions. So get a listener for "error" on your client, and you won't crash. We automatically retry on connection refused, so this should work itself out once you listen for errors.

I'm going to rework the way reconnections and errors are handled though, because this very common scenario ends up being confusing to a lot of people.

@t3ndai
Copy link

@t3ndai t3ndai commented Jan 24, 2018

Has any of this been handled! I'm trying to catch the errors in a try catch block. But, it's not working.

@12mse0306
Copy link

@12mse0306 12mse0306 commented Jul 23, 2018

const client = redis.createClient({
host: process.env.redis_hostname,
port: process.env.redis_port
})
client.on('error', (error) => {
logger.error(error.message);
})
client.on('connect',()=>{
logger.info('Successfully connected to redis');
})

image
So this will keep on polling to connect to redis server and when it finds a connection it will automatically connect. Thanks @mranney for a quick solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants