index.coffee 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. _ = require("underscore")
  2. async = require "async"
  3. module.exports = RedisSharelatex =
  4. createClient: (opts = {port: 6379, host: "localhost"})->
  5. if !opts.retry_max_delay?
  6. opts.retry_max_delay = 5000 # ms
  7. if opts.endpoints?
  8. standardOpts = _.clone(opts)
  9. delete standardOpts.endpoints
  10. delete standardOpts.masterName
  11. client = require("redis-sentinel").createClient opts.endpoints, opts.masterName, standardOpts
  12. client.healthCheck = RedisSharelatex.singleInstanceHealthCheckBuilder(client)
  13. else 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. healthCheck = (callback) ->
  35. jobs = client.nodes("all").map (node) =>
  36. (cb) => RedisSharelatex._checkClient(node, cb)
  37. async.parallel jobs, callback
  38. return healthCheck
  39. _checkClient: (client, callback) ->
  40. callback = _.once(callback)
  41. timer = setTimeout () ->
  42. error = new Error("redis client ping check timed out")
  43. console.error {
  44. err: error,
  45. key: client.options?.key # only present for cluster
  46. }, "client timed out"
  47. callback(error)
  48. , RedisSharelatex.HEARTBEAT_TIMEOUT
  49. client.ping (err) ->
  50. clearTimeout timer
  51. callback(err)
  52. _monkeyPatchIoredisExec: (client) ->
  53. _multi = client.multi
  54. client.multi = (args...) ->
  55. multi = _multi.call(client, args...)
  56. _exec = multi.exec
  57. multi.exec = (callback = () ->) ->
  58. _exec.call multi, (error, result) ->
  59. # ioredis exec returns an results like:
  60. # [ [null, 42], [null, "foo"] ]
  61. # where the first entries in each 2-tuple are
  62. # presumably errors for each individual command,
  63. # and the second entry is the result. We need to transform
  64. # this into the same result as the old redis driver:
  65. # [ 42, "foo" ]
  66. filtered_result = []
  67. for entry in result or []
  68. if entry[0]?
  69. return callback(entry[0])
  70. else
  71. filtered_result.push entry[1]
  72. callback error, filtered_result
  73. return multi