HistoryManager.coffee 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. async = require "async"
  2. logger = require "logger-sharelatex"
  3. request = require "request"
  4. Settings = require "settings-sharelatex"
  5. HistoryRedisManager = require "./HistoryRedisManager"
  6. ProjectHistoryRedisManager = require "./ProjectHistoryRedisManager"
  7. RedisManager = require "./RedisManager"
  8. module.exports = HistoryManager =
  9. flushDocChangesAsync: (project_id, doc_id) ->
  10. if !Settings.apis?.trackchanges?
  11. logger.warn { doc_id }, "track changes API is not configured, so not flushing"
  12. return
  13. url = "#{Settings.apis.trackchanges.url}/project/#{project_id}/doc/#{doc_id}/flush"
  14. logger.log { project_id, doc_id, url }, "flushing doc in track changes api"
  15. request.post url, (error, res, body)->
  16. if error?
  17. logger.error { error, doc_id, project_id}, "track changes doc to track changes api"
  18. else if res.statusCode < 200 and res.statusCode >= 300
  19. logger.error { doc_id, project_id }, "track changes api returned a failure status code: #{res.statusCode}"
  20. flushProjectChangesAsync: (project_id) ->
  21. return if !Settings.apis?.project_history?.enabled
  22. url = "#{Settings.apis.project_history.url}/project/#{project_id}/flush"
  23. logger.log { project_id, url }, "flushing doc in project history api"
  24. request.post url, (error, res, body)->
  25. if error?
  26. logger.error { error, project_id}, "project history doc to track changes api"
  27. else if res.statusCode < 200 and res.statusCode >= 300
  28. logger.error { project_id }, "project history api returned a failure status code: #{res.statusCode}"
  29. FLUSH_DOC_EVERY_N_OPS: 100
  30. FLUSH_PROJECT_EVERY_N_OPS: 500
  31. recordAndFlushHistoryOps: (project_id, doc_id, ops = [], doc_ops_length, project_ops_length, callback = (error) ->) ->
  32. if ops.length == 0
  33. return callback()
  34. if Settings.apis?.project_history?.enabled
  35. if HistoryManager.shouldFlushHistoryOps(project_ops_length, ops.length, HistoryManager.FLUSH_PROJECT_EVERY_N_OPS)
  36. # Do this in the background since it uses HTTP and so may be too
  37. # slow to wait for when processing a doc update.
  38. logger.log { project_ops_length, project_id }, "flushing project history api"
  39. HistoryManager.flushProjectChangesAsync project_id
  40. HistoryRedisManager.recordDocHasHistoryOps project_id, doc_id, ops, (error) ->
  41. return callback(error) if error?
  42. if HistoryManager.shouldFlushHistoryOps(doc_ops_length, ops.length, HistoryManager.FLUSH_DOC_EVERY_N_OPS)
  43. # Do this in the background since it uses HTTP and so may be too
  44. # slow to wait for when processing a doc update.
  45. logger.log { doc_ops_length, doc_id, project_id }, "flushing track changes api"
  46. HistoryManager.flushDocChangesAsync project_id, doc_id
  47. callback()
  48. shouldFlushHistoryOps: (length, ops_length, threshold) ->
  49. return false if !length # don't flush unless we know the length
  50. # We want to flush every 100 ops, i.e. 100, 200, 300, etc
  51. # Find out which 'block' (i.e. 0-99, 100-199) we were in before and after pushing these
  52. # ops. If we've changed, then we've gone over a multiple of 100 and should flush.
  53. # (Most of the time, we will only hit 100 and then flushing will put us back to 0)
  54. previousLength = length - ops_length
  55. prevBlock = Math.floor(previousLength / threshold)
  56. newBlock = Math.floor(length / threshold)
  57. return newBlock != prevBlock
  58. resyncProjectHistory: (project_id, docs, files, callback) ->
  59. ProjectHistoryRedisManager.queueResyncProjectStructure project_id, docs, files, (error) ->
  60. return callback(error) if error?
  61. DocumentManager = require "./DocumentManager"
  62. resyncDoc = (doc, cb) ->
  63. DocumentManager.resyncDocContentsWithLock project_id, doc.doc, cb
  64. async.each docs, resyncDoc, callback