ProjectPersistenceManager.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. * DS101: Remove unnecessary use of Array.from
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. let ProjectPersistenceManager
  15. const Metrics = require('./Metrics')
  16. const UrlCache = require('./UrlCache')
  17. const CompileManager = require('./CompileManager')
  18. const db = require('./db')
  19. const dbQueue = require('./DbQueue')
  20. const async = require('async')
  21. const logger = require('logger-sharelatex')
  22. const oneDay = 24 * 60 * 60 * 1000
  23. const Settings = require('@overleaf/settings')
  24. const diskusage = require('diskusage')
  25. const { callbackify } = require('util')
  26. async function refreshExpiryTimeout() {
  27. const paths = [
  28. Settings.path.compilesDir,
  29. Settings.path.outputDir,
  30. Settings.path.clsiCacheDir
  31. ]
  32. for (const path of paths) {
  33. try {
  34. const stats = await diskusage.check(path)
  35. const lowDisk = stats.available / stats.total < 0.1
  36. const lowerExpiry = ProjectPersistenceManager.EXPIRY_TIMEOUT * 0.9
  37. if (lowDisk && Settings.project_cache_length_ms / 2 < lowerExpiry) {
  38. logger.warn(
  39. {
  40. stats,
  41. newExpiryTimeoutInDays: (lowerExpiry / oneDay).toFixed(2)
  42. },
  43. 'disk running low on space, modifying EXPIRY_TIMEOUT'
  44. )
  45. ProjectPersistenceManager.EXPIRY_TIMEOUT = lowerExpiry
  46. break
  47. }
  48. } catch (err) {
  49. logger.err({ err, path }, 'error getting disk usage')
  50. }
  51. }
  52. }
  53. module.exports = ProjectPersistenceManager = {
  54. EXPIRY_TIMEOUT: Settings.project_cache_length_ms || oneDay * 2.5,
  55. promises: {
  56. refreshExpiryTimeout
  57. },
  58. refreshExpiryTimeout: callbackify(refreshExpiryTimeout),
  59. markProjectAsJustAccessed(project_id, callback) {
  60. if (callback == null) {
  61. callback = function (error) {}
  62. }
  63. const timer = new Metrics.Timer('db-bump-last-accessed')
  64. const job = (cb) =>
  65. db.Project.findOrCreate({ where: { project_id } })
  66. .spread((project, created) =>
  67. project
  68. .update({ lastAccessed: new Date() })
  69. .then(() => cb())
  70. .error(cb)
  71. )
  72. .error(cb)
  73. dbQueue.queue.push(job, (error) => {
  74. timer.done()
  75. callback(error)
  76. })
  77. },
  78. clearExpiredProjects(callback) {
  79. if (callback == null) {
  80. callback = function (error) {}
  81. }
  82. return ProjectPersistenceManager._findExpiredProjectIds(function (
  83. error,
  84. project_ids
  85. ) {
  86. if (error != null) {
  87. return callback(error)
  88. }
  89. logger.log({ project_ids }, 'clearing expired projects')
  90. const jobs = Array.from(project_ids || []).map((project_id) =>
  91. ((project_id) => (callback) =>
  92. ProjectPersistenceManager.clearProjectFromCache(project_id, function (
  93. err
  94. ) {
  95. if (err != null) {
  96. logger.error({ err, project_id }, 'error clearing project')
  97. }
  98. return callback()
  99. }))(project_id)
  100. )
  101. return async.series(jobs, function (error) {
  102. if (error != null) {
  103. return callback(error)
  104. }
  105. return CompileManager.clearExpiredProjects(
  106. ProjectPersistenceManager.EXPIRY_TIMEOUT,
  107. (error) => callback()
  108. )
  109. })
  110. })
  111. }, // ignore any errors from deleting directories
  112. clearProject(project_id, user_id, callback) {
  113. if (callback == null) {
  114. callback = function (error) {}
  115. }
  116. logger.log({ project_id, user_id }, 'clearing project for user')
  117. return CompileManager.clearProject(project_id, user_id, function (error) {
  118. if (error != null) {
  119. return callback(error)
  120. }
  121. return ProjectPersistenceManager.clearProjectFromCache(
  122. project_id,
  123. function (error) {
  124. if (error != null) {
  125. return callback(error)
  126. }
  127. return callback()
  128. }
  129. )
  130. })
  131. },
  132. clearProjectFromCache(project_id, callback) {
  133. if (callback == null) {
  134. callback = function (error) {}
  135. }
  136. logger.log({ project_id }, 'clearing project from cache')
  137. return UrlCache.clearProject(project_id, function (error) {
  138. if (error != null) {
  139. logger.err({ error, project_id }, 'error clearing project from cache')
  140. return callback(error)
  141. }
  142. return ProjectPersistenceManager._clearProjectFromDatabase(
  143. project_id,
  144. function (error) {
  145. if (error != null) {
  146. logger.err(
  147. { error, project_id },
  148. 'error clearing project from database'
  149. )
  150. }
  151. return callback(error)
  152. }
  153. )
  154. })
  155. },
  156. _clearProjectFromDatabase(project_id, callback) {
  157. if (callback == null) {
  158. callback = function (error) {}
  159. }
  160. logger.log({ project_id }, 'clearing project from database')
  161. const job = (cb) =>
  162. db.Project.destroy({ where: { project_id } })
  163. .then(() => cb())
  164. .error(cb)
  165. return dbQueue.queue.push(job, callback)
  166. },
  167. _findExpiredProjectIds(callback) {
  168. if (callback == null) {
  169. callback = function (error, project_ids) {}
  170. }
  171. const job = function (cb) {
  172. const keepProjectsFrom = new Date(
  173. Date.now() - ProjectPersistenceManager.EXPIRY_TIMEOUT
  174. )
  175. const q = {}
  176. q[db.op.lt] = keepProjectsFrom
  177. return db.Project.findAll({ where: { lastAccessed: q } })
  178. .then((projects) =>
  179. cb(
  180. null,
  181. projects.map((project) => project.project_id)
  182. )
  183. )
  184. .error(cb)
  185. }
  186. return dbQueue.queue.push(job, callback)
  187. }
  188. }
  189. logger.log(
  190. { EXPIRY_TIMEOUT: ProjectPersistenceManager.EXPIRY_TIMEOUT },
  191. 'project assets kept timeout'
  192. )