Procházet zdrojové kódy

incremented version and added basic health check function in

Henry Oswald před 11 roky
rodič
revize
e133c7101e

+ 36 - 1
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
+		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
+		}

+ 1 - 1
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",