2016-06-17 07:17:22 -04:00
Settings = require ( ' settings-sharelatex ' )
rclient = require ( " redis-sharelatex " ) . createClient ( Settings . redis . web )
async = require " async "
2017-03-30 06:20:41 -04:00
logger = require ( ' logger-sharelatex ' )
2016-06-17 07:17:22 -04:00
module.exports = WebRedisManager =
getPendingUpdatesForDoc : (doc_id, callback)->
multi = rclient . multi ( )
multi . lrange " PendingUpdates: #{ doc_id } " , 0 , - 1
multi . del " PendingUpdates: #{ doc_id } "
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)->
rclient . llen " PendingUpdates: #{ doc_id } " , callback
2016-08-23 11:00:46 -04:00
pushUncompressedHistoryOps: ( project_id , doc_id , ops = [ ] , callback = (error, length) -> ) ->
2016-09-08 06:22:54 -04:00
if ops . length == 0
2016-09-08 06:41:59 -04:00
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
2017-03-30 06:20:41 -04:00
opVersions = ops . map (op) -> op ? . v
logger . log project_id: project_id , doc_id: doc_id , op_versions: opVersions , " pushing uncompressed history ops "
2016-08-23 11:00:46 -04:00
jsonOps = ops . map (op) -> JSON . stringify op
2016-06-17 07:17:22 -04:00
async . parallel [
2016-08-23 11:00:46 -04:00
(cb) -> rclient . rpush " UncompressedHistoryOps: #{ doc_id } " , jsonOps . . . , cb
2016-06-17 07:17:22 -04:00
(cb) -> rclient . sadd " DocsWithHistoryOps: #{ project_id } " , doc_id , cb
] , (error, results) ->
return callback ( error ) if error ?
[ length , _ ] = results
2016-11-28 05:14:42 -05:00
callback ( error , length )
sendData: (data) ->
rclient . publish " applied-ops " , JSON . stringify ( data )