ProjectPersistenceManager.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 async = require('async')
  18. const logger = require('logger-sharelatex')
  19. const oneDay = 24 * 60 * 60 * 1000
  20. const Settings = require('@overleaf/settings')
  21. const diskusage = require('diskusage')
  22. const { callbackify } = require('util')
  23. const Path = require('path')
  24. const fs = require('fs')
  25. // projectId -> timestamp mapping.
  26. const LAST_ACCESS = new Map()
  27. async function refreshExpiryTimeout() {
  28. const paths = [
  29. Settings.path.compilesDir,
  30. Settings.path.outputDir,
  31. Settings.path.clsiCacheDir,
  32. ]
  33. for (const path of paths) {
  34. try {
  35. const stats = await diskusage.check(path)
  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,
  42. newExpiryTimeoutInDays: (lowerExpiry / oneDay).toFixed(2),
  43. },
  44. 'disk running low on space, modifying EXPIRY_TIMEOUT'
  45. )
  46. ProjectPersistenceManager.EXPIRY_TIMEOUT = lowerExpiry
  47. break
  48. }
  49. } catch (err) {
  50. logger.err({ err, path }, 'error getting disk usage')
  51. }
  52. }
  53. }
  54. module.exports = ProjectPersistenceManager = {
  55. EXPIRY_TIMEOUT: Settings.project_cache_length_ms || oneDay * 2.5,
  56. promises: {
  57. refreshExpiryTimeout,
  58. },
  59. refreshExpiryTimeout: callbackify(refreshExpiryTimeout),
  60. init() {
  61. fs.readdir(Settings.path.compilesDir, (err, dirs) => {
  62. if (err) {
  63. logger.warn({ err }, 'cannot get project listing')
  64. dirs = []
  65. }
  66. async.eachLimit(
  67. dirs,
  68. 10,
  69. (projectAndUserId, cb) => {
  70. const compileDir = Path.join(
  71. Settings.path.compilesDir,
  72. projectAndUserId
  73. )
  74. const projectId = projectAndUserId.slice(0, 24)
  75. fs.stat(compileDir, (err, stats) => {
  76. if (err) {
  77. // Schedule for immediate cleanup
  78. LAST_ACCESS.set(projectId, 0)
  79. } else {
  80. // Cleanup eventually.
  81. LAST_ACCESS.set(projectId, stats.mtime.getTime())
  82. }
  83. cb()
  84. })
  85. },
  86. () => {
  87. setInterval(() => {
  88. ProjectPersistenceManager.refreshExpiryTimeout(() => {
  89. ProjectPersistenceManager.clearExpiredProjects()
  90. })
  91. }, 10 * 60 * 1000)
  92. }
  93. )
  94. })
  95. },
  96. markProjectAsJustAccessed(project_id, callback) {
  97. LAST_ACCESS.set(project_id, Date.now())
  98. callback()
  99. },
  100. clearExpiredProjects(callback) {
  101. if (callback == null) {
  102. callback = function (error) {}
  103. }
  104. return ProjectPersistenceManager._findExpiredProjectIds(function (
  105. error,
  106. project_ids
  107. ) {
  108. if (error != null) {
  109. return callback(error)
  110. }
  111. logger.log({ project_ids }, 'clearing expired projects')
  112. const jobs = Array.from(project_ids || []).map(project_id =>
  113. (
  114. project_id => callback =>
  115. ProjectPersistenceManager.clearProjectFromCache(
  116. project_id,
  117. function (err) {
  118. if (err != null) {
  119. logger.error({ err, project_id }, 'error clearing project')
  120. }
  121. return callback()
  122. }
  123. )
  124. )(project_id)
  125. )
  126. return async.series(jobs, function (error) {
  127. if (error != null) {
  128. return callback(error)
  129. }
  130. return CompileManager.clearExpiredProjects(
  131. ProjectPersistenceManager.EXPIRY_TIMEOUT,
  132. error => callback()
  133. )
  134. })
  135. })
  136. }, // ignore any errors from deleting directories
  137. clearProject(project_id, user_id, callback) {
  138. if (callback == null) {
  139. callback = function (error) {}
  140. }
  141. logger.log({ project_id, user_id }, 'clearing project for user')
  142. return CompileManager.clearProject(project_id, user_id, function (error) {
  143. if (error != null) {
  144. return callback(error)
  145. }
  146. return ProjectPersistenceManager.clearProjectFromCache(
  147. project_id,
  148. function (error) {
  149. if (error != null) {
  150. return callback(error)
  151. }
  152. return callback()
  153. }
  154. )
  155. })
  156. },
  157. clearProjectFromCache(project_id, callback) {
  158. if (callback == null) {
  159. callback = function (error) {}
  160. }
  161. logger.log({ project_id }, 'clearing project from cache')
  162. return UrlCache.clearProject(project_id, function (error) {
  163. if (error != null) {
  164. logger.err({ error, project_id }, 'error clearing project from cache')
  165. return callback(error)
  166. }
  167. return ProjectPersistenceManager._clearProjectFromDatabase(
  168. project_id,
  169. function (error) {
  170. if (error != null) {
  171. logger.err(
  172. { error, project_id },
  173. 'error clearing project from database'
  174. )
  175. }
  176. return callback(error)
  177. }
  178. )
  179. })
  180. },
  181. _clearProjectFromDatabase(project_id, callback) {
  182. LAST_ACCESS.delete(project_id)
  183. callback()
  184. },
  185. _findExpiredProjectIds(callback) {
  186. const expiredFrom = Date.now() - ProjectPersistenceManager.EXPIRY_TIMEOUT
  187. const expiredProjectsIds = []
  188. for (const [projectId, lastAccess] of LAST_ACCESS.entries()) {
  189. if (lastAccess < expiredFrom) {
  190. expiredProjectsIds.push(projectId)
  191. }
  192. }
  193. // ^ may be a fairly busy loop, continue detached.
  194. setTimeout(() => callback(null, expiredProjectsIds), 0)
  195. },
  196. }
  197. logger.log(
  198. { EXPIRY_TIMEOUT: ProjectPersistenceManager.EXPIRY_TIMEOUT },
  199. 'project assets kept timeout'
  200. )