index.coffee 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. _ = require("underscore")
  2. os = require('os')
  3. crypto = require('crypto')
  4. # generate unique values for health check
  5. HOST = os.hostname()
  6. PID = process.pid
  7. RND = crypto.randomBytes(4).toString('hex')
  8. COUNT = 0
  9. module.exports = RedisSharelatex =
  10. createClient: (opts = {port: 6379, host: "localhost"})->
  11. if !opts.retry_max_delay?
  12. opts.retry_max_delay = 5000 # ms
  13. if opts.cluster?
  14. Redis = require("ioredis")
  15. standardOpts = _.clone(opts)
  16. delete standardOpts.cluster
  17. delete standardOpts.key_schema
  18. client = new Redis.Cluster(opts.cluster, standardOpts)
  19. client.healthCheck = RedisSharelatex.clusterHealthCheckBuilder(client)
  20. RedisSharelatex._monkeyPatchIoredisExec(client)
  21. else
  22. standardOpts = _.clone(opts)
  23. ioredis = require("ioredis")
  24. client = new ioredis(standardOpts)
  25. RedisSharelatex._monkeyPatchIoredisExec(client)
  26. client.healthCheck = RedisSharelatex.singleInstanceHealthCheckBuilder(client)
  27. return client
  28. HEARTBEAT_TIMEOUT: 2000
  29. singleInstanceHealthCheckBuilder: (client) ->
  30. healthCheck = (callback) ->
  31. RedisSharelatex._checkClient(client, callback)
  32. return healthCheck
  33. clusterHealthCheckBuilder: (client) ->
  34. return RedisSharelatex.singleInstanceHealthCheckBuilder(client)
  35. _checkClient: (client, callback) ->
  36. callback = _.once(callback)
  37. # check the redis connection by storing and retrieving a unique key/value pair
  38. uniqueToken = "host=#{HOST}:pid=#{PID}:random=#{RND}:time=#{Date.now()}:count=#{COUNT++}"
  39. timer = setTimeout () ->
  40. error = new Error("redis client health check timed out #{client?.options?.host}")
  41. console.error {
  42. err: error,
  43. key: client.options?.key # only present for cluster
  44. clientOptions: client.options
  45. uniqueToken: uniqueToken
  46. }, "client timed out"
  47. callback(error)
  48. , RedisSharelatex.HEARTBEAT_TIMEOUT
  49. healthCheckKey = "_redis-wrapper:healthCheckKey:{#{uniqueToken}}"
  50. healthCheckValue = "_redis-wrapper:healthCheckValue:{#{uniqueToken}}"
  51. # set the unique key/value pair
  52. multi = client.multi()
  53. multi.set healthCheckKey, healthCheckValue, "EX", 60
  54. multi.exec (err, reply) ->
  55. if err?
  56. clearTimeout timer
  57. return callback(err)
  58. # check that we can retrieve the unique key/value pair
  59. multi = client.multi()
  60. multi.get healthCheckKey
  61. multi.del healthCheckKey
  62. multi.exec (err, reply) ->
  63. clearTimeout timer
  64. return callback(err) if err?
  65. return callback(new Error("bad response from redis health check")) if reply?[0] isnt healthCheckValue or reply?[1] isnt 1
  66. return callback()
  67. _monkeyPatchIoredisExec: (client) ->
  68. _multi = client.multi
  69. client.multi = (args...) ->
  70. multi = _multi.call(client, args...)
  71. _exec = multi.exec
  72. multi.exec = (callback = () ->) ->
  73. _exec.call multi, (error, result) ->
  74. # ioredis exec returns an results like:
  75. # [ [null, 42], [null, "foo"] ]
  76. # where the first entries in each 2-tuple are
  77. # presumably errors for each individual command,
  78. # and the second entry is the result. We need to transform
  79. # this into the same result as the old redis driver:
  80. # [ 42, "foo" ]
  81. filtered_result = []
  82. for entry in result or []
  83. if entry[0]?
  84. return callback(entry[0])
  85. else
  86. filtered_result.push entry[1]
  87. callback error, filtered_result
  88. return multi