HistoryManager.js 7.8 KB

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