HistoryManager.js 7.5 KB

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