WebRedisManager.coffee 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. Settings = require('settings-sharelatex')
  2. rclient = require("redis-sharelatex").createClient(Settings.redis.web)
  3. async = require "async"
  4. module.exports = WebRedisManager =
  5. getPendingUpdatesForDoc : (doc_id, callback)->
  6. multi = rclient.multi()
  7. multi.lrange "PendingUpdates:#{doc_id}", 0 , -1
  8. multi.del "PendingUpdates:#{doc_id}"
  9. multi.exec (error, replys) ->
  10. return callback(error) if error?
  11. jsonUpdates = replys[0]
  12. updates = []
  13. for jsonUpdate in jsonUpdates
  14. try
  15. update = JSON.parse jsonUpdate
  16. catch e
  17. return callback e
  18. updates.push update
  19. callback error, updates
  20. getUpdatesLength: (doc_id, callback)->
  21. rclient.llen "PendingUpdates:#{doc_id}", callback
  22. pushUncompressedHistoryOps: (project_id, doc_id, ops = [], callback = (error, length) ->) ->
  23. if ops.length == 0
  24. return callback(new Error("cannot push no ops")) # This should never be called with no ops, but protect against a redis error if we sent an empty array to rpush
  25. jsonOps = ops.map (op) -> JSON.stringify op
  26. async.parallel [
  27. (cb) -> rclient.rpush "UncompressedHistoryOps:#{doc_id}", jsonOps..., cb
  28. (cb) -> rclient.sadd "DocsWithHistoryOps:#{project_id}", doc_id, cb
  29. ], (error, results) ->
  30. return callback(error) if error?
  31. [length, _] = results
  32. callback(error, length)
  33. sendData: (data) ->
  34. rclient.publish "applied-ops", JSON.stringify(data)