DeleteQueueManager.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* eslint-disable
  2. no-unused-vars,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS101: Remove unnecessary use of Array.from
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. let DeleteQueueManager
  14. const Settings = require('@overleaf/settings')
  15. const { promisifyAll } = require('@overleaf/promise-utils')
  16. const RedisManager = require('./RedisManager')
  17. const ProjectManager = require('./ProjectManager')
  18. const logger = require('@overleaf/logger')
  19. const metrics = require('./Metrics')
  20. // Maintain a sorted set of project flushAndDelete requests, ordered by timestamp
  21. // (ZADD), and process them from oldest to newest. A flushAndDelete request comes
  22. // from real-time and is triggered when a user leaves a project.
  23. //
  24. // The aim is to remove the project from redis 5 minutes after the last request
  25. // if there has been no activity (document updates) in that time. If there is
  26. // activity we can expect a further flushAndDelete request when the editing user
  27. // leaves the project.
  28. //
  29. // If a new flushAndDelete request comes in while an existing request is already
  30. // in the queue we update the timestamp as we can postpone flushing further.
  31. //
  32. // Documents are processed by checking the queue, seeing if the first entry is
  33. // older than 5 minutes, and popping it from the queue in that case.
  34. module.exports = DeleteQueueManager = {
  35. flushAndDeleteOldProjects(options, callback) {
  36. const startTime = Date.now()
  37. const cutoffTime =
  38. startTime - options.min_delete_age + 100 * (Math.random() - 0.5)
  39. let count = 0
  40. const flushProjectIfNotModified = (projectId, flushTimestamp, cb) =>
  41. ProjectManager.getProjectDocsTimestamps(
  42. projectId,
  43. function (err, timestamps) {
  44. if (err != null) {
  45. return callback(err)
  46. }
  47. if (timestamps.length === 0) {
  48. logger.debug(
  49. { projectId },
  50. 'skipping flush of queued project - no timestamps'
  51. )
  52. return cb()
  53. }
  54. // are any of the timestamps newer than the time the project was flushed?
  55. for (const timestamp of Array.from(timestamps)) {
  56. if (timestamp > flushTimestamp) {
  57. metrics.inc('queued-delete-skipped')
  58. logger.debug(
  59. { projectId, timestamps, flushTimestamp },
  60. 'found newer timestamp, will skip delete'
  61. )
  62. return cb()
  63. }
  64. }
  65. logger.debug({ projectId, flushTimestamp }, 'flushing queued project')
  66. return ProjectManager.flushAndDeleteProjectWithLocks(
  67. projectId,
  68. { skip_history_flush: false },
  69. function (err) {
  70. if (err != null) {
  71. logger.err({ projectId, err }, 'error flushing queued project')
  72. }
  73. metrics.inc('queued-delete-completed')
  74. return cb(null, true)
  75. }
  76. )
  77. }
  78. )
  79. function flushNextProject() {
  80. const now = Date.now()
  81. if (now - startTime > options.timeout) {
  82. logger.debug('hit time limit on flushing old projects')
  83. return callback(null, count)
  84. }
  85. if (count > options.limit) {
  86. logger.debug('hit count limit on flushing old projects')
  87. return callback(null, count)
  88. }
  89. return RedisManager.getNextProjectToFlushAndDelete(
  90. cutoffTime,
  91. function (err, projectId, flushTimestamp, queueLength) {
  92. if (err != null) {
  93. return callback(err, count)
  94. }
  95. if (projectId == null) {
  96. return callback(null, count)
  97. }
  98. logger.debug({ projectId, queueLength }, 'flushing queued project')
  99. metrics.globalGauge('queued-flush-backlog', queueLength)
  100. return flushProjectIfNotModified(
  101. projectId,
  102. flushTimestamp,
  103. function (err, flushed) {
  104. if (err) {
  105. // Do not stop processing the queue in case the flush fails.
  106. // Slowing down the processing can fill up redis.
  107. metrics.inc('queued-delete-error')
  108. }
  109. if (flushed) {
  110. count++
  111. }
  112. return flushNextProject()
  113. }
  114. )
  115. }
  116. )
  117. }
  118. return flushNextProject()
  119. },
  120. startBackgroundFlush() {
  121. const SHORT_DELAY = 10
  122. const LONG_DELAY = 1000
  123. function doFlush() {
  124. if (Settings.shuttingDown) {
  125. logger.info('discontinuing background flush due to shutdown')
  126. return
  127. }
  128. return DeleteQueueManager.flushAndDeleteOldProjects(
  129. {
  130. timeout: 1000,
  131. min_delete_age: 3 * 60 * 1000,
  132. limit: 1000, // high value, to ensure we always flush enough projects
  133. },
  134. (_err, flushed) =>
  135. setTimeout(doFlush, flushed > 10 ? SHORT_DELAY : LONG_DELAY)
  136. )
  137. }
  138. return doFlush()
  139. },
  140. }
  141. DeleteQueueManager.promises = promisifyAll(DeleteQueueManager)