DeleteQueueManager.js 4.9 KB

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