HistoryManager.coffee 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. # flush changes in the background
  21. flushProjectChangesAsync: (project_id) ->
  22. return if !Settings.apis?.project_history?.enabled
  23. HistoryManager.flushProjectChanges project_id, ->
  24. # flush changes and callback (for when we need to know the queue is flushed)
  25. flushProjectChanges: (project_id, callback = (error) ->) ->
  26. return callback() if !Settings.apis?.project_history?.enabled
  27. url = "#{Settings.apis.project_history.url}/project/#{project_id}/flush"
  28. logger.log { project_id, url }, "flushing doc in project history api"
  29. request.post url, (error, res, body)->
  30. if error?
  31. logger.error { error, project_id}, "project history doc to track changes api"
  32. return callback(error)
  33. else if res.statusCode < 200 and res.statusCode >= 300
  34. logger.error { project_id }, "project history api returned a failure status code: #{res.statusCode}"
  35. return callback(error)
  36. else
  37. return callback()
  38. FLUSH_DOC_EVERY_N_OPS: 100
  39. FLUSH_PROJECT_EVERY_N_OPS: 500
  40. recordAndFlushHistoryOps: (project_id, doc_id, ops = [], doc_ops_length, project_ops_length, callback = (error) ->) ->
  41. if ops.length == 0
  42. return callback()
  43. if Settings.apis?.project_history?.enabled
  44. if HistoryManager.shouldFlushHistoryOps(project_ops_length, ops.length, HistoryManager.FLUSH_PROJECT_EVERY_N_OPS)
  45. # Do this in the background since it uses HTTP and so may be too
  46. # slow to wait for when processing a doc update.
  47. logger.log { project_ops_length, project_id }, "flushing project history api"
  48. HistoryManager.flushProjectChangesAsync project_id
  49. HistoryRedisManager.recordDocHasHistoryOps project_id, doc_id, ops, (error) ->
  50. return callback(error) if error?
  51. if HistoryManager.shouldFlushHistoryOps(doc_ops_length, ops.length, HistoryManager.FLUSH_DOC_EVERY_N_OPS)
  52. # Do this in the background since it uses HTTP and so may be too
  53. # slow to wait for when processing a doc update.
  54. logger.log { doc_ops_length, doc_id, project_id }, "flushing track changes api"
  55. HistoryManager.flushDocChangesAsync project_id, doc_id
  56. callback()
  57. shouldFlushHistoryOps: (length, ops_length, threshold) ->
  58. return false if !length # don't flush unless we know the length
  59. # We want to flush every 100 ops, i.e. 100, 200, 300, etc
  60. # Find out which 'block' (i.e. 0-99, 100-199) we were in before and after pushing these
  61. # ops. If we've changed, then we've gone over a multiple of 100 and should flush.
  62. # (Most of the time, we will only hit 100 and then flushing will put us back to 0)
  63. previousLength = length - ops_length
  64. prevBlock = Math.floor(previousLength / threshold)
  65. newBlock = Math.floor(length / threshold)
  66. return newBlock != prevBlock
  67. MAX_PARALLEL_REQUESTS: 4
  68. resyncProjectHistory: (project_id, projectHistoryId, docs, files, callback) ->
  69. ProjectHistoryRedisManager.queueResyncProjectStructure project_id, projectHistoryId, docs, files, (error) ->
  70. return callback(error) if error?
  71. DocumentManager = require "./DocumentManager"
  72. resyncDoc = (doc, cb) ->
  73. DocumentManager.resyncDocContentsWithLock project_id, doc.doc, cb
  74. async.eachLimit docs, HistoryManager.MAX_PARALLEL_REQUESTS, resyncDoc, callback