2014-02-26 10:56:52 -05:00
|
|
|
settings = require "settings-sharelatex"
|
|
|
|
request = require "request"
|
|
|
|
logger = require "logger-sharelatex"
|
2014-02-28 13:29:05 -05:00
|
|
|
RedisManager = require "./RedisManager"
|
2014-02-28 14:09:29 -05:00
|
|
|
crypto = require("crypto")
|
2014-02-26 10:56:52 -05:00
|
|
|
|
2014-02-28 13:29:05 -05:00
|
|
|
module.exports = TrackChangesManager =
|
2015-11-30 10:17:11 -05:00
|
|
|
|
|
|
|
flushDocChangesIfNeeded: (project_id, doc_id, callback = (error) ->) ->
|
|
|
|
RedisManager.getUncompressedHistoryOpLength doc_id, (error, length) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
if length > 0
|
|
|
|
# only make request to track changes if there are queued ops
|
|
|
|
TrackChangesManager.flushDocChanges project_id, doc_id, callback
|
|
|
|
else
|
|
|
|
callback()
|
|
|
|
|
2014-03-19 11:56:44 -04:00
|
|
|
flushDocChanges: (project_id, doc_id, callback = (error) ->) ->
|
2014-02-26 10:56:52 -05:00
|
|
|
if !settings.apis?.trackchanges?
|
|
|
|
logger.warn doc_id: doc_id, "track changes API is not configured, so not flushing"
|
|
|
|
return callback()
|
|
|
|
|
2014-03-19 11:56:44 -04:00
|
|
|
url = "#{settings.apis.trackchanges.url}/project/#{project_id}/doc/#{doc_id}/flush"
|
|
|
|
logger.log project_id: project_id, doc_id: doc_id, url: url, "flushing doc in track changes api"
|
2014-02-26 10:56:52 -05:00
|
|
|
request.post url, (error, res, body)->
|
|
|
|
if error?
|
|
|
|
return callback(error)
|
|
|
|
else if res.statusCode >= 200 and res.statusCode < 300
|
|
|
|
return callback(null)
|
|
|
|
else
|
|
|
|
error = new Error("track changes api returned a failure status code: #{res.statusCode}")
|
|
|
|
return callback(error)
|
2014-02-28 13:29:05 -05:00
|
|
|
|
|
|
|
FLUSH_EVERY_N_OPS: 50
|
2014-03-19 11:56:44 -04:00
|
|
|
pushUncompressedHistoryOp: (project_id, doc_id, op, callback = (error) ->) ->
|
2014-03-21 08:41:05 -04:00
|
|
|
RedisManager.pushUncompressedHistoryOp project_id, doc_id, op, (error, length) ->
|
2014-02-28 14:09:29 -05:00
|
|
|
return callback(error) if error?
|
2014-03-21 08:41:05 -04:00
|
|
|
if length > 0 and length % TrackChangesManager.FLUSH_EVERY_N_OPS == 0
|
|
|
|
# Do this in the background since it uses HTTP and so may be too
|
|
|
|
# slow to wait for when processing a doc update.
|
|
|
|
logger.log length: length, doc_id: doc_id, project_id: project_id, "flushing track changes api"
|
|
|
|
TrackChangesManager.flushDocChanges project_id, doc_id, (error) ->
|
|
|
|
if error?
|
|
|
|
logger.error err: error, doc_id: doc_id, project_id: project_id, "error flushing doc to track changes api"
|
|
|
|
callback()
|
2014-02-28 14:09:29 -05:00
|
|
|
|