Node-redis: Try if Redis server is running

Created on 16 Oct 2011  路  3Comments  路  Source: NodeRedis/node-redis

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?

question

Most helpful comment

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

All 3 comments

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.

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

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

michaelwittig picture michaelwittig  路  3Comments

betimer picture betimer  路  5Comments

jackycchen picture jackycchen  路  4Comments

strumwolf picture strumwolf  路  4Comments

yuany picture yuany  路  4Comments