ProjectPersistenceManager.js 5.5 KB

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