RealTimeRedisManager.coffee 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. Settings = require('settings-sharelatex')
  2. rclient = require("redis-sharelatex").createClient(Settings.redis.documentupdater)
  3. pubsubClient = require("redis-sharelatex").createClient(Settings.redis.pubsub)
  4. Keys = Settings.redis.documentupdater.key_schema
  5. logger = require('logger-sharelatex')
  6. os = require "os"
  7. crypto = require "crypto"
  8. HOST = os.hostname()
  9. RND = crypto.randomBytes(4).toString('hex') # generate a random key for this process
  10. COUNT = 0
  11. MAX_OPS_PER_ITERATION = 8 # process a limited number of ops for safety
  12. module.exports = RealTimeRedisManager =
  13. getPendingUpdatesForDoc : (doc_id, callback)->
  14. multi = rclient.multi()
  15. multi.lrange Keys.pendingUpdates({doc_id}), 0, (MAX_OPS_PER_ITERATION-1)
  16. multi.ltrim Keys.pendingUpdates({doc_id}), MAX_OPS_PER_ITERATION, -1
  17. multi.exec (error, replys) ->
  18. return callback(error) if error?
  19. jsonUpdates = replys[0]
  20. updates = []
  21. for jsonUpdate in jsonUpdates
  22. try
  23. update = JSON.parse jsonUpdate
  24. catch e
  25. return callback e
  26. updates.push update
  27. callback error, updates
  28. getUpdatesLength: (doc_id, callback)->
  29. rclient.llen Keys.pendingUpdates({doc_id}), callback
  30. sendData: (data) ->
  31. # create a unique message id using a counter
  32. message_id = "doc:#{HOST}:#{RND}-#{COUNT++}"
  33. data?._id = message_id
  34. # publish on separate channels for individual projects and docs when
  35. # configured (needs realtime to be configured for this too).
  36. if Settings.publishOnIndividualChannels
  37. pubsubClient.publish "applied-ops:#{data.doc_id}", JSON.stringify(data)
  38. else
  39. pubsubClient.publish "applied-ops", JSON.stringify(data)