HistoryManager.js 7.6 KB

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