HistoryManager.js 7.9 KB

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