Created
February 7, 2012 21:08
-
-
Save FGRibreau/1761963 to your computer and use it in GitHub Desktop.
Node-redis - auto-reconnect
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
redis = require("redis") | |
cli = null | |
# 1/ Helpers | |
RedisOnConnected = (err, redis) -> | |
# Update the global reference to the redis client | |
cli = redis | |
RedisDoConnect = (cbConnected) -> | |
client = require("redis").createClient() | |
redis.once('ready', () -> | |
console.info('Redis ready') | |
cbConnected(null, client) | |
) | |
redis.on('error', (err) -> | |
console.error("Redis ERROR", err) | |
) | |
redis.once('end', () -> | |
console.error("Redis connection closed, reconnecting") | |
process.nextTick(() -> | |
RedisDoConnect(RedisOnConnected) | |
) | |
) | |
# 2/ Init the connection | |
RedisDoConnect((err, redis) -> | |
RedisOnConnected(err, redis) | |
# 3/ Do something... | |
# redis.hgetall ... | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment