HistoryManager.coffee 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. settings = require "settings-sharelatex"
  2. request = require "request"
  3. logger = require "logger-sharelatex"
  4. async = require "async"
  5. WebRedisManager = require "./WebRedisManager"
  6. module.exports = HistoryManager =
  7. flushDocChanges: (project_id, doc_id, callback = (error) ->) ->
  8. if !settings.apis?.trackchanges?
  9. logger.warn doc_id: doc_id, "track changes API is not configured, so not flushing"
  10. return callback()
  11. url = "#{settings.apis.trackchanges.url}/project/#{project_id}/doc/#{doc_id}/flush"
  12. logger.log project_id: project_id, doc_id: doc_id, url: url, "flushing doc in track changes api"
  13. request.post url, (error, res, body)->
  14. if error?
  15. return callback(error)
  16. else if res.statusCode >= 200 and res.statusCode < 300
  17. return callback(null)
  18. else
  19. error = new Error("track changes api returned a failure status code: #{res.statusCode}")
  20. return callback(error)
  21. FLUSH_EVERY_N_OPS: 50
  22. pushUncompressedHistoryOps: (project_id, doc_id, ops = [], callback = (error) ->) ->
  23. if ops.length == 0
  24. return callback()
  25. WebRedisManager.pushUncompressedHistoryOps project_id, doc_id, ops, (error, length) ->
  26. return callback(error) if error?
  27. # We want to flush every 50 ops, i.e. 50, 100, 150, etc
  28. # Find out which 'block' (i.e. 0-49, 50-99) we were in before and after pushing these
  29. # ops. If we've changed, then we've gone over a multiple of 50 and should flush.
  30. # (Most of the time, we will only hit 50 and then flushing will put us back to 0)
  31. previousLength = length - ops.length
  32. prevBlock = Math.floor(previousLength / HistoryManager.FLUSH_EVERY_N_OPS)
  33. newBlock = Math.floor(length / HistoryManager.FLUSH_EVERY_N_OPS)
  34. if newBlock != prevBlock
  35. # Do this in the background since it uses HTTP and so may be too
  36. # slow to wait for when processing a doc update.
  37. logger.log length: length, doc_id: doc_id, project_id: project_id, "flushing track changes api"
  38. HistoryManager.flushDocChanges project_id, doc_id, (error) ->
  39. if error?
  40. logger.error err: error, doc_id: doc_id, project_id: project_id, "error flushing doc to track changes api"
  41. callback()