mranney/node_redis — Issue #138: Try if Redis server is running
Author: juriansluiman
Created: 2011-10-16 19:52:25+00:00
State: open

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. (/var/www/php-jobs/node_modules/redis/index.js:88:28)
at Socket.emit (events.js:64:17)
at Array. (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?