HistoryManager.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. const async = require('async')
  2. const logger = require('@overleaf/logger')
  3. const { promisifyAll } = require('@overleaf/promise-utils')
  4. const request = require('request')
  5. const Settings = require('@overleaf/settings')
  6. const ProjectHistoryRedisManager = require('./ProjectHistoryRedisManager')
  7. const metrics = require('./Metrics')
  8. const 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(
  89. projectId,
  90. projectHistoryId,
  91. docs,
  92. files,
  93. opts,
  94. callback
  95. ) {
  96. ProjectHistoryRedisManager.queueResyncProjectStructure(
  97. projectId,
  98. projectHistoryId,
  99. docs,
  100. files,
  101. function (error) {
  102. if (error) {
  103. return callback(error)
  104. }
  105. const DocumentManager = require('./DocumentManager')
  106. const resyncDoc = (doc, cb) => {
  107. DocumentManager.resyncDocContentsWithLock(
  108. projectId,
  109. doc.doc,
  110. doc.path,
  111. opts,
  112. cb
  113. )
  114. }
  115. async.eachLimit(
  116. docs,
  117. HistoryManager.MAX_PARALLEL_REQUESTS,
  118. resyncDoc,
  119. callback
  120. )
  121. }
  122. )
  123. },
  124. }
  125. module.exports = HistoryManager
  126. module.exports.promises = promisifyAll(HistoryManager, {
  127. without: [
  128. 'flushProjectChangesAsync',
  129. 'recordAndFlushHistoryOps',
  130. 'shouldFlushHistoryOps',
  131. ],
  132. })