ProjectPersistenceManager.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. (
  92. project_id => callback =>
  93. ProjectPersistenceManager.clearProjectFromCache(
  94. project_id,
  95. function (err) {
  96. if (err != null) {
  97. logger.error({ err, project_id }, 'error clearing project')
  98. }
  99. return callback()
  100. }
  101. )
  102. )(project_id)
  103. )
  104. return async.series(jobs, function (error) {
  105. if (error != null) {
  106. return callback(error)
  107. }
  108. return CompileManager.clearExpiredProjects(
  109. ProjectPersistenceManager.EXPIRY_TIMEOUT,
  110. error => callback()
  111. )
  112. })
  113. })
  114. }, // ignore any errors from deleting directories
  115. clearProject(project_id, user_id, callback) {
  116. if (callback == null) {
  117. callback = function (error) {}
  118. }
  119. logger.log({ project_id, user_id }, 'clearing project for user')
  120. return CompileManager.clearProject(project_id, user_id, function (error) {
  121. if (error != null) {
  122. return callback(error)
  123. }
  124. return ProjectPersistenceManager.clearProjectFromCache(
  125. project_id,
  126. function (error) {
  127. if (error != null) {
  128. return callback(error)
  129. }
  130. return callback()
  131. }
  132. )
  133. })
  134. },
  135. clearProjectFromCache(project_id, callback) {
  136. if (callback == null) {
  137. callback = function (error) {}
  138. }
  139. logger.log({ project_id }, 'clearing project from cache')
  140. return UrlCache.clearProject(project_id, function (error) {
  141. if (error != null) {
  142. logger.err({ error, project_id }, 'error clearing project from cache')
  143. return callback(error)
  144. }
  145. return ProjectPersistenceManager._clearProjectFromDatabase(
  146. project_id,
  147. function (error) {
  148. if (error != null) {
  149. logger.err(
  150. { error, project_id },
  151. 'error clearing project from database'
  152. )
  153. }
  154. return callback(error)
  155. }
  156. )
  157. })
  158. },
  159. _clearProjectFromDatabase(project_id, callback) {
  160. if (callback == null) {
  161. callback = function (error) {}
  162. }
  163. logger.log({ project_id }, 'clearing project from database')
  164. const job = cb =>
  165. db.Project.destroy({ where: { project_id } })
  166. .then(() => cb())
  167. .error(cb)
  168. return dbQueue.queue.push(job, callback)
  169. },
  170. _findExpiredProjectIds(callback) {
  171. if (callback == null) {
  172. callback = function (error, project_ids) {}
  173. }
  174. const job = function (cb) {
  175. const keepProjectsFrom = new Date(
  176. Date.now() - ProjectPersistenceManager.EXPIRY_TIMEOUT
  177. )
  178. const q = {}
  179. q[db.op.lt] = keepProjectsFrom
  180. return db.Project.findAll({ where: { lastAccessed: q } })
  181. .then(projects =>
  182. cb(
  183. null,
  184. projects.map(project => project.project_id)
  185. )
  186. )
  187. .error(cb)
  188. }
  189. return dbQueue.queue.push(job, callback)
  190. },
  191. }
  192. logger.log(
  193. { EXPIRY_TIMEOUT: ProjectPersistenceManager.EXPIRY_TIMEOUT },
  194. 'project assets kept timeout'
  195. )