HistoryManager.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. let HistoryManager
  2. const async = require('async')
  3. const logger = require('@overleaf/logger')
  4. const request = require('request')
  5. const Settings = require('@overleaf/settings')
  6. const ProjectHistoryRedisManager = require('./ProjectHistoryRedisManager')
  7. const metrics = require('./Metrics')
  8. module.exports = HistoryManager = {
  9. // flush changes in the background
  10. flushProjectChangesAsync(projectId) {
  11. HistoryManager.flushProjectChanges(
  12. projectId,
  13. { background: true },
  14. function () {}
  15. )
  16. },
  17. // flush changes and callback (for when we need to know the queue is flushed)
  18. flushProjectChanges(projectId, options, callback) {
  19. if (callback == null) {
  20. callback = function () {}
  21. }
  22. if (options.skip_history_flush) {
  23. logger.debug({ projectId }, 'skipping flush of project history')
  24. return callback()
  25. }
  26. metrics.inc('history-flush', 1, { status: 'project-history' })
  27. const url = `${Settings.apis.project_history.url}/project/${projectId}/flush`
  28. const qs = {}
  29. if (options.background) {
  30. qs.background = true
  31. } // pass on the background flush option if present
  32. logger.debug({ projectId, url, qs }, 'flushing doc in project history api')
  33. request.post({ url, qs }, function (error, res, body) {
  34. if (error) {
  35. logger.error({ error, projectId }, 'project history api request failed')
  36. callback(error)
  37. } else if (res.statusCode < 200 && res.statusCode >= 300) {
  38. logger.error(
  39. { projectId },
  40. `project history api returned a failure status code: ${res.statusCode}`
  41. )
  42. callback(error)
  43. } else {
  44. callback()
  45. }
  46. })
  47. },
  48. FLUSH_DOC_EVERY_N_OPS: 100,
  49. FLUSH_PROJECT_EVERY_N_OPS: 500,
  50. recordAndFlushHistoryOps(projectId, ops, projectOpsLength) {
  51. if (ops == null) {
  52. ops = []
  53. }
  54. if (ops.length === 0) {
  55. return
  56. }
  57. // record updates for project history
  58. if (
  59. HistoryManager.shouldFlushHistoryOps(
  60. projectOpsLength,
  61. ops.length,
  62. HistoryManager.FLUSH_PROJECT_EVERY_N_OPS
  63. )
  64. ) {
  65. // Do this in the background since it uses HTTP and so may be too
  66. // slow to wait for when processing a doc update.
  67. logger.debug(
  68. { projectOpsLength, projectId },
  69. 'flushing project history api'
  70. )
  71. HistoryManager.flushProjectChangesAsync(projectId)
  72. }
  73. },
  74. shouldFlushHistoryOps(length, opsLength, threshold) {
  75. if (!length) {
  76. return false
  77. } // don't flush unless we know the length
  78. // We want to flush every 100 ops, i.e. 100, 200, 300, etc
  79. // Find out which 'block' (i.e. 0-99, 100-199) we were in before and after pushing these
  80. // ops. If we've changed, then we've gone over a multiple of 100 and should flush.
  81. // (Most of the time, we will only hit 100 and then flushing will put us back to 0)
  82. const previousLength = length - opsLength
  83. const prevBlock = Math.floor(previousLength / threshold)
  84. const newBlock = Math.floor(length / threshold)
  85. return newBlock !== prevBlock
  86. },
  87. MAX_PARALLEL_REQUESTS: 4,
  88. resyncProjectHistory(projectId, projectHistoryId, docs, files, callback) {
  89. ProjectHistoryRedisManager.queueResyncProjectStructure(
  90. projectId,
  91. projectHistoryId,
  92. docs,
  93. files,
  94. function (error) {
  95. if (error) {
  96. return callback(error)
  97. }
  98. const DocumentManager = require('./DocumentManager')
  99. const resyncDoc = (doc, cb) => {
  100. DocumentManager.resyncDocContentsWithLock(
  101. projectId,
  102. doc.doc,
  103. doc.path,
  104. cb
  105. )
  106. }
  107. async.eachLimit(
  108. docs,
  109. HistoryManager.MAX_PARALLEL_REQUESTS,
  110. resyncDoc,
  111. callback
  112. )
  113. }
  114. )
  115. },
  116. }