ProjectPersistenceManager.js 5.7 KB

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