HistoryManager.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // TODO: This file was created by bulk-decaffeinate.
  2. // Fix any style issues and re-enable lint.
  3. /*
  4. * decaffeinate suggestions:
  5. * DS102: Remove unnecessary code created because of implicit returns
  6. * DS207: Consider shorter variations of null checks
  7. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  8. */
  9. let HistoryManager
  10. const async = require('async')
  11. const logger = require('@overleaf/logger')
  12. const request = require('request')
  13. const Settings = require('@overleaf/settings')
  14. const HistoryRedisManager = require('./HistoryRedisManager')
  15. const ProjectHistoryRedisManager = require('./ProjectHistoryRedisManager')
  16. const RedisManager = require('./RedisManager')
  17. const metrics = require('./Metrics')
  18. module.exports = HistoryManager = {
  19. flushDocChangesAsync(projectId, docId) {
  20. if (
  21. (Settings.apis != null ? Settings.apis.trackchanges : undefined) == null
  22. ) {
  23. logger.warn(
  24. { docId },
  25. 'track changes API is not configured, so not flushing'
  26. )
  27. return
  28. }
  29. if (Settings.disableTrackChanges) {
  30. logger.debug({ docId }, 'track changes is disabled, so not flushing')
  31. return
  32. }
  33. return RedisManager.getHistoryType(
  34. docId,
  35. function (err, projectHistoryType) {
  36. if (err != null) {
  37. logger.warn({ err, docId }, 'error getting history type')
  38. }
  39. // if there's an error continue and flush to track-changes for safety
  40. if (
  41. Settings.disableDoubleFlush &&
  42. projectHistoryType === 'project-history'
  43. ) {
  44. return logger.debug(
  45. { docId, projectHistoryType },
  46. 'skipping track-changes flush'
  47. )
  48. } else {
  49. metrics.inc('history-flush', 1, { status: 'track-changes' })
  50. const url = `${Settings.apis.trackchanges.url}/project/${projectId}/doc/${docId}/flush`
  51. logger.debug(
  52. { projectId, docId, url, projectHistoryType },
  53. 'flushing doc in track changes api'
  54. )
  55. return request.post(url, function (error, res, body) {
  56. if (error != null) {
  57. return logger.error(
  58. { error, docId, projectId },
  59. 'track changes doc to track changes api'
  60. )
  61. } else if (res.statusCode < 200 && res.statusCode >= 300) {
  62. return logger.error(
  63. { docId, projectId },
  64. `track changes api returned a failure status code: ${res.statusCode}`
  65. )
  66. }
  67. })
  68. }
  69. }
  70. )
  71. },
  72. // flush changes in the background
  73. flushProjectChangesAsync(projectId) {
  74. if (!Settings.apis?.project_history?.enabled) {
  75. return
  76. }
  77. return HistoryManager.flushProjectChanges(
  78. projectId,
  79. { background: true },
  80. function () {}
  81. )
  82. },
  83. // flush changes and callback (for when we need to know the queue is flushed)
  84. flushProjectChanges(projectId, options, callback) {
  85. if (callback == null) {
  86. callback = function () {}
  87. }
  88. if (!Settings.apis?.project_history?.enabled) {
  89. return callback()
  90. }
  91. if (options.skip_history_flush) {
  92. logger.debug({ projectId }, 'skipping flush of project history')
  93. return callback()
  94. }
  95. metrics.inc('history-flush', 1, { status: 'project-history' })
  96. const url = `${Settings.apis.project_history.url}/project/${projectId}/flush`
  97. const qs = {}
  98. if (options.background) {
  99. qs.background = true
  100. } // pass on the background flush option if present
  101. logger.debug({ projectId, url, qs }, 'flushing doc in project history api')
  102. return request.post({ url, qs }, function (error, res, body) {
  103. if (error != null) {
  104. logger.error(
  105. { error, projectId },
  106. 'project history doc to track changes api'
  107. )
  108. return callback(error)
  109. } else if (res.statusCode < 200 && res.statusCode >= 300) {
  110. logger.error(
  111. { projectId },
  112. `project history api returned a failure status code: ${res.statusCode}`
  113. )
  114. return callback(error)
  115. } else {
  116. return callback()
  117. }
  118. })
  119. },
  120. FLUSH_DOC_EVERY_N_OPS: 100,
  121. FLUSH_PROJECT_EVERY_N_OPS: 500,
  122. recordAndFlushHistoryOps(
  123. projectId,
  124. docId,
  125. ops,
  126. docOpsLength,
  127. projectOpsLength,
  128. callback
  129. ) {
  130. if (ops == null) {
  131. ops = []
  132. }
  133. if (callback == null) {
  134. callback = function () {}
  135. }
  136. if (ops.length === 0) {
  137. return callback()
  138. }
  139. // record updates for project history
  140. if (Settings.apis?.project_history?.enabled) {
  141. if (
  142. HistoryManager.shouldFlushHistoryOps(
  143. projectOpsLength,
  144. ops.length,
  145. HistoryManager.FLUSH_PROJECT_EVERY_N_OPS
  146. )
  147. ) {
  148. // Do this in the background since it uses HTTP and so may be too
  149. // slow to wait for when processing a doc update.
  150. logger.debug(
  151. { projectOpsLength, projectId },
  152. 'flushing project history api'
  153. )
  154. HistoryManager.flushProjectChangesAsync(projectId)
  155. }
  156. }
  157. // if the doc_ops_length is undefined it means the project is not using track-changes
  158. // so we can bail out here
  159. if (Settings.disableTrackChanges || typeof docOpsLength === 'undefined') {
  160. logger.debug(
  161. { projectId, docId },
  162. 'skipping flush to track-changes, only using project-history'
  163. )
  164. return callback()
  165. }
  166. // record updates for track-changes
  167. return HistoryRedisManager.recordDocHasHistoryOps(
  168. projectId,
  169. docId,
  170. ops,
  171. function (error) {
  172. if (error != null) {
  173. return callback(error)
  174. }
  175. if (
  176. HistoryManager.shouldFlushHistoryOps(
  177. docOpsLength,
  178. ops.length,
  179. HistoryManager.FLUSH_DOC_EVERY_N_OPS
  180. )
  181. ) {
  182. // Do this in the background since it uses HTTP and so may be too
  183. // slow to wait for when processing a doc update.
  184. logger.debug(
  185. { docOpsLength, docId, projectId },
  186. 'flushing track changes api'
  187. )
  188. HistoryManager.flushDocChangesAsync(projectId, docId)
  189. }
  190. return callback()
  191. }
  192. )
  193. },
  194. shouldFlushHistoryOps(length, opsLength, threshold) {
  195. if (!length) {
  196. return false
  197. } // don't flush unless we know the length
  198. // We want to flush every 100 ops, i.e. 100, 200, 300, etc
  199. // Find out which 'block' (i.e. 0-99, 100-199) we were in before and after pushing these
  200. // ops. If we've changed, then we've gone over a multiple of 100 and should flush.
  201. // (Most of the time, we will only hit 100 and then flushing will put us back to 0)
  202. const previousLength = length - opsLength
  203. const prevBlock = Math.floor(previousLength / threshold)
  204. const newBlock = Math.floor(length / threshold)
  205. return newBlock !== prevBlock
  206. },
  207. MAX_PARALLEL_REQUESTS: 4,
  208. resyncProjectHistory(projectId, projectHistoryId, docs, files, callback) {
  209. return ProjectHistoryRedisManager.queueResyncProjectStructure(
  210. projectId,
  211. projectHistoryId,
  212. docs,
  213. files,
  214. function (error) {
  215. if (error != null) {
  216. return callback(error)
  217. }
  218. const DocumentManager = require('./DocumentManager')
  219. const resyncDoc = (doc, cb) => {
  220. DocumentManager.resyncDocContentsWithLock(
  221. projectId,
  222. doc.doc,
  223. doc.path,
  224. cb
  225. )
  226. }
  227. return async.eachLimit(
  228. docs,
  229. HistoryManager.MAX_PARALLEL_REQUESTS,
  230. resyncDoc,
  231. callback
  232. )
  233. }
  234. )
  235. },
  236. }