Skip to content

Instantly share code, notes, and snippets.

@FGRibreau
Created February 7, 2012 21:08
Show Gist options
  • Save FGRibreau/1761963 to your computer and use it in GitHub Desktop.
Save FGRibreau/1761963 to your computer and use it in GitHub Desktop.
Node-redis - auto-reconnect
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