ProjectPersistenceManager.js 5.7 KB

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