2015-08-13 17:40:28 -04:00
|
|
|
async = require("async")
|
2015-08-14 04:42:27 -04:00
|
|
|
_ = require("underscore")
|
2015-08-13 17:40:28 -04:00
|
|
|
logger = require("logger-sharelatex")
|
|
|
|
DocstoreManager = require("../Docstore/DocstoreManager")
|
|
|
|
ProjectGetter = require("../Project/ProjectGetter")
|
|
|
|
ProjectUpdateHandler = require("../Project/ProjectUpdateHandler")
|
|
|
|
Project = require("../../models/Project").Project
|
|
|
|
|
|
|
|
MILISECONDS_IN_DAY = 86400000
|
|
|
|
module.exports = InactiveProjectManager =
|
|
|
|
|
|
|
|
reactivateProjectIfRequired: (project_id, callback)->
|
2015-08-19 06:54:30 -04:00
|
|
|
ProjectGetter.getProject project_id, {active:true}, (err, project)->
|
2015-08-13 17:40:28 -04:00
|
|
|
if err?
|
|
|
|
logger.err err:err, project_id:project_id, "error getting project"
|
|
|
|
return callback(err)
|
2015-08-19 06:54:30 -04:00
|
|
|
logger.log project_id:project_id, active:project.active, "seeing if need to reactivate project"
|
2015-08-13 17:40:28 -04:00
|
|
|
|
2015-08-19 06:54:30 -04:00
|
|
|
if project.active
|
2015-08-13 17:40:28 -04:00
|
|
|
return callback()
|
|
|
|
|
|
|
|
DocstoreManager.unarchiveProject project_id, (err)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, project_id:project_id, "error reactivating project in docstore"
|
|
|
|
return callback(err)
|
|
|
|
ProjectUpdateHandler.markAsActive project_id, callback
|
|
|
|
|
2015-08-14 09:11:53 -04:00
|
|
|
deactivateOldProjects: (limit = 10, daysOld = 360, callback)->
|
|
|
|
oldProjectDate = new Date() - (MILISECONDS_IN_DAY * daysOld)
|
|
|
|
logger.log oldProjectDate:oldProjectDate, limit:limit, daysOld:daysOld, "starting process of deactivating old projects"
|
2015-08-13 17:40:28 -04:00
|
|
|
Project.find()
|
2015-08-14 09:11:53 -04:00
|
|
|
.where("lastOpened").lt(oldProjectDate)
|
2015-08-19 06:54:30 -04:00
|
|
|
.where("active").equals(true)
|
2015-08-13 17:40:28 -04:00
|
|
|
.select("_id")
|
|
|
|
.limit(limit)
|
|
|
|
.exec (err, projects)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, "could not get projects for deactivating"
|
|
|
|
jobs = _.map projects, (project)->
|
|
|
|
return (cb)->
|
|
|
|
InactiveProjectManager.deactivateProject project._id, cb
|
|
|
|
logger.log numberOfProjects:projects?.length, "deactivating projects"
|
2015-08-14 09:11:53 -04:00
|
|
|
async.series jobs, (err)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, "error deactivating projects"
|
|
|
|
callback err, projects
|
2015-08-13 17:40:28 -04:00
|
|
|
|
|
|
|
|
|
|
|
deactivateProject: (project_id, callback)->
|
|
|
|
logger.log project_id:project_id, "deactivating inactive project"
|
2015-10-19 10:44:43 -04:00
|
|
|
jobs = [
|
|
|
|
(cb)-> DocstoreManager.archiveProject project_id, cb
|
|
|
|
(cb)-> ProjectUpdateHandler.markAsInactive project_id, cb
|
|
|
|
]
|
|
|
|
async.series jobs, (err)->
|
2015-08-13 17:40:28 -04:00
|
|
|
if err?
|
2015-10-19 10:44:43 -04:00
|
|
|
logger.err err:err, project_id:project_id, "error deactivating project"
|
|
|
|
callback(err)
|
2015-08-13 17:40:28 -04:00
|
|
|
|