From e133c7101e4b9e5d1f904c9732dfc2261dd2ec7f Mon Sep 17 00:00:00 2001 From: Henry Oswald Date: Wed, 19 Nov 2014 23:18:56 +0000 Subject: [PATCH] incremented version and added basic health check function in --- libraries/redis-wrapper/index.coffee | 37 +++++++++++++++++++++++++++- libraries/redis-wrapper/package.json | 2 +- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/libraries/redis-wrapper/index.coffee b/libraries/redis-wrapper/index.coffee index fd71b221a3..fa85c9f457 100644 --- a/libraries/redis-wrapper/index.coffee +++ b/libraries/redis-wrapper/index.coffee @@ -18,4 +18,39 @@ module.exports = RedisSharelatex = delete standardOpts.port delete standardOpts.host client = require("redis").createClient opts.port, opts.host, standardOpts - return client \ No newline at end of file + return client + + + activeHealthCheckRedis: (connectionInfo)-> + sub = RedisSharelatex.createClient(connectionInfo) + pub = RedisSharelatex.createClient(connectionInfo) + + heartbeatInterval = 2000 #ms + isAliveTimeout = 10000 #ms + + id = require("crypto").pseudoRandomBytes(16).toString("hex") + heartbeatChannel = "heartbeat-#{id}" + lastHeartbeat = Date.now() + + sub.subscribe heartbeatChannel, (error) -> + if error? + console.error "ERROR: failed to subscribe to #{heartbeatChannel} channel", error + sub.on "message", (channel, message) -> + if channel == heartbeatChannel + lastHeartbeat = Date.now() + + setInterval -> + pub.publish heartbeatChannel, "PING" + , heartbeatInterval + + isAlive = -> + timeSinceLastHeartbeat = Date.now() - lastHeartbeat + if timeSinceLastHeartbeat > isAliveTimeout + console.error "heartbeat from redis timed out" + return false + else + return true + + return { + isAlive:isAlive + } diff --git a/libraries/redis-wrapper/package.json b/libraries/redis-wrapper/package.json index 3569f57082..d23cd89692 100644 --- a/libraries/redis-wrapper/package.json +++ b/libraries/redis-wrapper/package.json @@ -1,6 +1,6 @@ { "name": "redis-sharelatex", - "version": "0.0.6", + "version": "0.0.7", "description": "redis wrapper for node which will either use sentinal or normal redis", "main": "index.js", "author": "henry oswald @ sharelatex",