overleaf/services/clsi/app/coffee/ProjectPersistenceManager.coffee

85 lines
3 KiB
CoffeeScript
Raw Normal View History

2014-02-12 12:27:43 -05:00
UrlCache = require "./UrlCache"
CompileManager = require "./CompileManager"
db = require "./db"
dbQueue = require "./DbQueue"
2014-02-12 12:27:43 -05:00
async = require "async"
logger = require "logger-sharelatex"
oneDay = 24 * 60 * 60 * 1000
2016-05-19 11:51:50 -04:00
Settings = require "settings-sharelatex"
2014-02-12 12:27:43 -05:00
module.exports = ProjectPersistenceManager =
2016-05-19 11:51:50 -04:00
EXPIRY_TIMEOUT: Settings.project_cache_length_ms || oneDay * 2.5
2014-02-12 12:27:43 -05:00
markProjectAsJustAccessed: (project_id, callback = (error) ->) ->
job = (cb)->
db.Project.findOrCreate(where: {project_id: project_id})
.spread(
(project, created) ->
project.updateAttributes(lastAccessed: new Date())
.then(() -> cb())
.error cb
)
.error cb
dbQueue.queue.push(job, callback)
2014-02-12 12:27:43 -05:00
clearExpiredProjects: (callback = (error) ->) ->
ProjectPersistenceManager._findExpiredProjectIds (error, project_ids) ->
return callback(error) if error?
logger.log project_ids: project_ids, "clearing expired projects"
jobs = for project_id in (project_ids or [])
do (project_id) ->
(callback) ->
ProjectPersistenceManager.clearProjectFromCache project_id, (err) ->
2014-02-12 12:27:43 -05:00
if err?
logger.error err: err, project_id: project_id, "error clearing project"
callback()
async.series jobs, (error) ->
return callback(error) if error?
CompileManager.clearExpiredProjects ProjectPersistenceManager.EXPIRY_TIMEOUT, (error) ->
callback() # ignore any errors from deleting directories
2014-02-12 12:27:43 -05:00
clearProject: (project_id, user_id, callback = (error) ->) ->
2016-05-31 11:12:55 -04:00
logger.log project_id: project_id, user_id:user_id, "clearing project for user"
CompileManager.clearProject project_id, user_id, (error) ->
2014-02-12 12:27:43 -05:00
return callback(error) if error?
ProjectPersistenceManager.clearProjectFromCache project_id, (error) ->
2014-02-12 12:27:43 -05:00
return callback(error) if error?
callback()
clearProjectFromCache: (project_id, callback = (error) ->) ->
logger.log project_id: project_id, "clearing project from cache"
UrlCache.clearProject project_id, (error) ->
2018-08-19 06:38:27 -04:00
if error?
logger.err error:error, project_id: project_id, "error clearing project from cache"
return callback(error)
ProjectPersistenceManager._clearProjectFromDatabase project_id, (error) ->
2018-08-19 06:38:27 -04:00
if error?
logger.err error:error, project_id:project_id, "error clearing project from database"
callback(error)
2014-02-12 12:27:43 -05:00
_clearProjectFromDatabase: (project_id, callback = (error) ->) ->
2018-08-19 06:38:27 -04:00
logger.log project_id:project_id, "clearing project from database"
job = (cb)->
db.Project.destroy(where: {project_id: project_id})
2018-07-30 12:01:59 -04:00
.then(() -> cb())
.error cb
dbQueue.queue.push(job, callback)
2014-02-12 12:27:43 -05:00
_findExpiredProjectIds: (callback = (error, project_ids) ->) ->
job = (cb)->
2018-07-30 12:37:30 -04:00
keepProjectsFrom = new Date(Date.now() - ProjectPersistenceManager.EXPIRY_TIMEOUT)
q = {}
q[db.op.lt] = keepProjectsFrom
2018-07-30 12:37:30 -04:00
db.Project.findAll(where:{lastAccessed:q})
.then((projects) ->
2018-07-30 12:01:59 -04:00
cb null, projects.map((project) -> project.project_id)
).error cb
2018-07-30 12:37:30 -04:00
dbQueue.queue.push(job, callback)
2016-05-19 11:51:50 -04:00
2016-05-20 05:22:45 -04:00
logger.log {EXPIRY_TIMEOUT: ProjectPersistenceManager.EXPIRY_TIMEOUT}, "project assets kept timeout"