2014-02-12 12:27:43 -05:00
|
|
|
UrlCache = require "./UrlCache"
|
|
|
|
CompileManager = require "./CompileManager"
|
|
|
|
db = require "./db"
|
|
|
|
async = require "async"
|
|
|
|
logger = require "logger-sharelatex"
|
2016-03-10 05:30:37 -05:00
|
|
|
oneDay = 24 * 60 * 60 * 1000
|
2014-02-12 12:27:43 -05:00
|
|
|
|
|
|
|
module.exports = ProjectPersistenceManager =
|
2016-03-10 05:30:37 -05:00
|
|
|
|
|
|
|
EXPIRY_TIMEOUT: oneDay * 2.5
|
2014-02-12 12:27:43 -05:00
|
|
|
|
|
|
|
markProjectAsJustAccessed: (project_id, callback = (error) ->) ->
|
2015-05-15 09:05:28 -04:00
|
|
|
db.Project.findOrCreate(where: {project_id: project_id})
|
|
|
|
.spread(
|
|
|
|
(project, created) ->
|
2014-02-12 12:27:43 -05:00
|
|
|
project.updateAttributes(lastAccessed: new Date())
|
2015-05-15 09:05:28 -04:00
|
|
|
.then(() -> callback())
|
2014-02-12 12:27:43 -05:00
|
|
|
.error callback
|
|
|
|
)
|
|
|
|
.error callback
|
|
|
|
|
|
|
|
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.clearProject project_id, (err) ->
|
|
|
|
if err?
|
|
|
|
logger.error err: err, project_id: project_id, "error clearing project"
|
|
|
|
callback()
|
|
|
|
async.series jobs, callback
|
|
|
|
|
|
|
|
clearProject: (project_id, callback = (error) ->) ->
|
|
|
|
logger.log project_id: project_id, "clearing project"
|
|
|
|
CompileManager.clearProject project_id, (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
UrlCache.clearProject project_id, (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
ProjectPersistenceManager._clearProjectFromDatabase project_id, (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
callback()
|
|
|
|
|
|
|
|
_clearProjectFromDatabase: (project_id, callback = (error) ->) ->
|
2015-05-15 09:05:28 -04:00
|
|
|
db.Project.destroy(where: {project_id: project_id})
|
|
|
|
.then(() -> callback())
|
2014-02-12 12:27:43 -05:00
|
|
|
.error callback
|
|
|
|
|
|
|
|
_findExpiredProjectIds: (callback = (error, project_ids) ->) ->
|
|
|
|
db.Project.findAll(where: ["lastAccessed < ?", new Date(Date.now() - ProjectPersistenceManager.EXPIRY_TIMEOUT)])
|
2015-05-15 09:05:28 -04:00
|
|
|
.then((projects) ->
|
|
|
|
callback null, projects.map((project) -> project.project_id)
|
|
|
|
).error callback
|