2020-02-19 06:14:28 -05:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
handle-callback-err,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-19 06:14:14 -05:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-02-19 06:14:37 -05:00
|
|
|
let ProjectPersistenceManager
|
2021-06-15 04:08:32 -04:00
|
|
|
const Metrics = require('./Metrics')
|
2020-02-19 06:14:37 -05:00
|
|
|
const UrlCache = require('./UrlCache')
|
|
|
|
const CompileManager = require('./CompileManager')
|
|
|
|
const db = require('./db')
|
|
|
|
const dbQueue = require('./DbQueue')
|
|
|
|
const async = require('async')
|
|
|
|
const logger = require('logger-sharelatex')
|
|
|
|
const oneDay = 24 * 60 * 60 * 1000
|
2021-07-12 12:47:21 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2020-05-11 05:29:16 -04:00
|
|
|
const diskusage = require('diskusage')
|
2021-06-01 10:52:41 -04:00
|
|
|
const { callbackify } = require('util')
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2021-06-01 10:52:41 -04:00
|
|
|
async function refreshExpiryTimeout() {
|
|
|
|
const paths = [
|
|
|
|
Settings.path.compilesDir,
|
|
|
|
Settings.path.outputDir,
|
2021-07-13 07:04:48 -04:00
|
|
|
Settings.path.clsiCacheDir,
|
2021-06-01 10:52:41 -04:00
|
|
|
]
|
|
|
|
for (const path of paths) {
|
|
|
|
try {
|
|
|
|
const stats = await diskusage.check(path)
|
2020-05-11 05:29:16 -04:00
|
|
|
const lowDisk = stats.available / stats.total < 0.1
|
2021-06-01 10:52:41 -04:00
|
|
|
|
2020-05-11 05:29:16 -04:00
|
|
|
const lowerExpiry = ProjectPersistenceManager.EXPIRY_TIMEOUT * 0.9
|
|
|
|
if (lowDisk && Settings.project_cache_length_ms / 2 < lowerExpiry) {
|
|
|
|
logger.warn(
|
2021-05-12 04:47:35 -04:00
|
|
|
{
|
2021-06-01 10:52:41 -04:00
|
|
|
stats,
|
2021-07-13 07:04:48 -04:00
|
|
|
newExpiryTimeoutInDays: (lowerExpiry / oneDay).toFixed(2),
|
2021-05-12 04:47:35 -04:00
|
|
|
},
|
2020-05-11 05:29:16 -04:00
|
|
|
'disk running low on space, modifying EXPIRY_TIMEOUT'
|
|
|
|
)
|
|
|
|
ProjectPersistenceManager.EXPIRY_TIMEOUT = lowerExpiry
|
2021-06-01 10:52:41 -04:00
|
|
|
break
|
2020-05-11 05:29:16 -04:00
|
|
|
}
|
2021-06-01 10:52:41 -04:00
|
|
|
} catch (err) {
|
|
|
|
logger.err({ err, path }, 'error getting disk usage')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ProjectPersistenceManager = {
|
|
|
|
EXPIRY_TIMEOUT: Settings.project_cache_length_ms || oneDay * 2.5,
|
|
|
|
|
|
|
|
promises: {
|
2021-07-13 07:04:48 -04:00
|
|
|
refreshExpiryTimeout,
|
2020-05-11 05:29:16 -04:00
|
|
|
},
|
2021-06-01 10:52:41 -04:00
|
|
|
|
|
|
|
refreshExpiryTimeout: callbackify(refreshExpiryTimeout),
|
2020-02-19 06:14:37 -05:00
|
|
|
markProjectAsJustAccessed(project_id, callback) {
|
|
|
|
if (callback == null) {
|
2020-08-10 12:01:11 -04:00
|
|
|
callback = function (error) {}
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
2021-06-14 07:24:48 -04:00
|
|
|
const timer = new Metrics.Timer('db-bump-last-accessed')
|
2021-07-13 07:04:48 -04:00
|
|
|
const job = cb =>
|
2020-02-19 06:14:37 -05:00
|
|
|
db.Project.findOrCreate({ where: { project_id } })
|
|
|
|
.spread((project, created) =>
|
|
|
|
project
|
2020-03-16 11:31:02 -04:00
|
|
|
.update({ lastAccessed: new Date() })
|
2020-02-19 06:14:37 -05:00
|
|
|
.then(() => cb())
|
|
|
|
.error(cb)
|
|
|
|
)
|
|
|
|
.error(cb)
|
2021-07-13 07:04:48 -04:00
|
|
|
dbQueue.queue.push(job, error => {
|
2021-06-14 07:24:48 -04:00
|
|
|
timer.done()
|
|
|
|
callback(error)
|
|
|
|
})
|
2020-02-19 06:14:37 -05:00
|
|
|
},
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
clearExpiredProjects(callback) {
|
|
|
|
if (callback == null) {
|
2020-08-10 12:01:11 -04:00
|
|
|
callback = function (error) {}
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
2020-08-10 12:01:11 -04:00
|
|
|
return ProjectPersistenceManager._findExpiredProjectIds(function (
|
2020-02-19 06:14:37 -05:00
|
|
|
error,
|
|
|
|
project_ids
|
|
|
|
) {
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
logger.log({ project_ids }, 'clearing expired projects')
|
2021-07-13 07:04:48 -04:00
|
|
|
const jobs = Array.from(project_ids || []).map(project_id =>
|
|
|
|
(
|
|
|
|
project_id => callback =>
|
|
|
|
ProjectPersistenceManager.clearProjectFromCache(
|
|
|
|
project_id,
|
|
|
|
function (err) {
|
|
|
|
if (err != null) {
|
|
|
|
logger.error({ err, project_id }, 'error clearing project')
|
|
|
|
}
|
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)(project_id)
|
2020-02-19 06:14:37 -05:00
|
|
|
)
|
2020-08-10 12:01:11 -04:00
|
|
|
return async.series(jobs, function (error) {
|
2020-02-19 06:14:37 -05:00
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
return CompileManager.clearExpiredProjects(
|
|
|
|
ProjectPersistenceManager.EXPIRY_TIMEOUT,
|
2021-07-13 07:04:48 -04:00
|
|
|
error => callback()
|
2020-02-19 06:14:37 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}, // ignore any errors from deleting directories
|
2018-07-30 11:46:47 -04:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
clearProject(project_id, user_id, callback) {
|
|
|
|
if (callback == null) {
|
2020-08-10 12:01:11 -04:00
|
|
|
callback = function (error) {}
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
|
|
|
logger.log({ project_id, user_id }, 'clearing project for user')
|
2020-08-10 12:01:11 -04:00
|
|
|
return CompileManager.clearProject(project_id, user_id, function (error) {
|
2020-02-19 06:14:37 -05:00
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
return ProjectPersistenceManager.clearProjectFromCache(
|
|
|
|
project_id,
|
2020-08-10 12:01:11 -04:00
|
|
|
function (error) {
|
2020-02-19 06:14:37 -05:00
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
},
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
clearProjectFromCache(project_id, callback) {
|
|
|
|
if (callback == null) {
|
2020-08-10 12:01:11 -04:00
|
|
|
callback = function (error) {}
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
|
|
|
logger.log({ project_id }, 'clearing project from cache')
|
2020-08-10 12:01:11 -04:00
|
|
|
return UrlCache.clearProject(project_id, function (error) {
|
2020-02-19 06:14:37 -05:00
|
|
|
if (error != null) {
|
|
|
|
logger.err({ error, project_id }, 'error clearing project from cache')
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
return ProjectPersistenceManager._clearProjectFromDatabase(
|
|
|
|
project_id,
|
2020-08-10 12:01:11 -04:00
|
|
|
function (error) {
|
2020-02-19 06:14:37 -05:00
|
|
|
if (error != null) {
|
|
|
|
logger.err(
|
|
|
|
{ error, project_id },
|
|
|
|
'error clearing project from database'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
},
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
_clearProjectFromDatabase(project_id, callback) {
|
|
|
|
if (callback == null) {
|
2020-08-10 12:01:11 -04:00
|
|
|
callback = function (error) {}
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
|
|
|
logger.log({ project_id }, 'clearing project from database')
|
2021-07-13 07:04:48 -04:00
|
|
|
const job = cb =>
|
2020-02-19 06:14:37 -05:00
|
|
|
db.Project.destroy({ where: { project_id } })
|
|
|
|
.then(() => cb())
|
|
|
|
.error(cb)
|
|
|
|
return dbQueue.queue.push(job, callback)
|
|
|
|
},
|
2016-05-27 10:31:44 -04:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
_findExpiredProjectIds(callback) {
|
|
|
|
if (callback == null) {
|
2020-08-10 12:01:11 -04:00
|
|
|
callback = function (error, project_ids) {}
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
2020-08-10 12:01:11 -04:00
|
|
|
const job = function (cb) {
|
2020-02-19 06:14:37 -05:00
|
|
|
const keepProjectsFrom = new Date(
|
|
|
|
Date.now() - ProjectPersistenceManager.EXPIRY_TIMEOUT
|
|
|
|
)
|
|
|
|
const q = {}
|
|
|
|
q[db.op.lt] = keepProjectsFrom
|
|
|
|
return db.Project.findAll({ where: { lastAccessed: q } })
|
2021-07-13 07:04:48 -04:00
|
|
|
.then(projects =>
|
2020-02-19 06:14:37 -05:00
|
|
|
cb(
|
|
|
|
null,
|
2021-07-13 07:04:48 -04:00
|
|
|
projects.map(project => project.project_id)
|
2020-02-19 06:14:37 -05:00
|
|
|
)
|
|
|
|
)
|
|
|
|
.error(cb)
|
|
|
|
}
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
return dbQueue.queue.push(job, callback)
|
2021-07-13 07:04:48 -04:00
|
|
|
},
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
2018-07-30 11:46:47 -04:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
logger.log(
|
|
|
|
{ EXPIRY_TIMEOUT: ProjectPersistenceManager.EXPIRY_TIMEOUT },
|
|
|
|
'project assets kept timeout'
|
|
|
|
)
|