2016-06-17 07:17:22 -04:00
|
|
|
Settings = require('settings-sharelatex')
|
2017-05-02 10:38:33 -04:00
|
|
|
rclient = require("redis-sharelatex").createClient(Settings.redis.realtime)
|
|
|
|
Keys = Settings.redis.realtime.key_schema
|
2017-03-30 06:20:41 -04:00
|
|
|
logger = require('logger-sharelatex')
|
2016-06-17 07:17:22 -04:00
|
|
|
|
2017-05-12 08:11:04 -04:00
|
|
|
MAX_OPS_PER_ITERATION = 8 # process a limited number of ops for safety
|
|
|
|
|
2017-05-02 10:38:33 -04:00
|
|
|
module.exports = RealTimeRedisManager =
|
2016-06-17 07:17:22 -04:00
|
|
|
getPendingUpdatesForDoc : (doc_id, callback)->
|
|
|
|
multi = rclient.multi()
|
2017-05-12 08:11:04 -04:00
|
|
|
multi.lrange Keys.pendingUpdates({doc_id}), 0, (MAX_OPS_PER_ITERATION-1)
|
|
|
|
multi.ltrim Keys.pendingUpdates({doc_id}), MAX_OPS_PER_ITERATION, -1
|
2016-06-17 07:17:22 -04:00
|
|
|
multi.exec (error, replys) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
jsonUpdates = replys[0]
|
|
|
|
updates = []
|
|
|
|
for jsonUpdate in jsonUpdates
|
|
|
|
try
|
|
|
|
update = JSON.parse jsonUpdate
|
|
|
|
catch e
|
|
|
|
return callback e
|
|
|
|
updates.push update
|
|
|
|
callback error, updates
|
|
|
|
|
|
|
|
getUpdatesLength: (doc_id, callback)->
|
2017-04-13 12:00:42 -04:00
|
|
|
rclient.llen Keys.pendingUpdates({doc_id}), callback
|
2016-06-17 07:17:22 -04:00
|
|
|
|
2016-11-28 05:14:42 -05:00
|
|
|
sendData: (data) ->
|
2017-04-13 12:00:42 -04:00
|
|
|
rclient.publish "applied-ops", JSON.stringify(data)
|