2019-05-29 05:21:06 -04:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
max-len,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
let InactiveProjectManager
|
2020-08-11 05:35:08 -04:00
|
|
|
const OError = require('@overleaf/o-error')
|
2019-05-29 05:21:06 -04:00
|
|
|
const async = require('async')
|
|
|
|
const _ = require('underscore')
|
2021-11-10 08:40:18 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2019-05-29 05:21:06 -04:00
|
|
|
const DocstoreManager = require('../Docstore/DocstoreManager')
|
|
|
|
const ProjectGetter = require('../Project/ProjectGetter')
|
|
|
|
const ProjectUpdateHandler = require('../Project/ProjectUpdateHandler')
|
|
|
|
const { Project } = require('../../models/Project')
|
2020-11-09 10:48:49 -05:00
|
|
|
const { ObjectId } = require('mongodb')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
|
|
|
const MILISECONDS_IN_DAY = 86400000
|
|
|
|
module.exports = InactiveProjectManager = {
|
|
|
|
reactivateProjectIfRequired(project_id, callback) {
|
2021-04-14 09:17:21 -04:00
|
|
|
return ProjectGetter.getProject(
|
|
|
|
project_id,
|
|
|
|
{ active: true },
|
|
|
|
function (err, project) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err != null) {
|
2021-04-14 09:17:21 -04:00
|
|
|
OError.tag(err, 'error getting project', {
|
2021-04-27 03:52:58 -04:00
|
|
|
project_id,
|
2020-08-11 05:35:08 -04:00
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
return callback(err)
|
|
|
|
}
|
2021-04-14 09:17:21 -04:00
|
|
|
logger.log(
|
|
|
|
{ project_id, active: project.active },
|
|
|
|
'seeing if need to reactivate project'
|
|
|
|
)
|
|
|
|
|
|
|
|
if (project.active) {
|
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
|
|
|
|
return DocstoreManager.unarchiveProject(project_id, function (err) {
|
|
|
|
if (err != null) {
|
|
|
|
OError.tag(err, 'error reactivating project in docstore', {
|
2021-04-27 03:52:58 -04:00
|
|
|
project_id,
|
2021-04-14 09:17:21 -04:00
|
|
|
})
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
return ProjectUpdateHandler.markAsActive(project_id, callback)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
deactivateOldProjects(limit, daysOld, callback) {
|
|
|
|
if (limit == null) {
|
|
|
|
limit = 10
|
|
|
|
}
|
|
|
|
if (daysOld == null) {
|
|
|
|
daysOld = 360
|
|
|
|
}
|
|
|
|
const oldProjectDate = new Date() - MILISECONDS_IN_DAY * daysOld
|
2020-11-09 10:48:49 -05:00
|
|
|
// use $not $gt to catch non-opened projects where lastOpened is null
|
|
|
|
Project.find({ lastOpened: { $not: { $gt: oldProjectDate } } })
|
|
|
|
.where('_id')
|
|
|
|
.lt(ObjectId.createFromTime(oldProjectDate / 1000))
|
2019-05-29 05:21:06 -04:00
|
|
|
.where('active')
|
|
|
|
.equals(true)
|
|
|
|
.select('_id')
|
2020-11-09 10:48:49 -05:00
|
|
|
.sort({ _id: 1 })
|
2019-05-29 05:21:06 -04:00
|
|
|
.limit(limit)
|
2021-01-26 11:02:13 -05:00
|
|
|
.read('secondary')
|
2021-04-14 09:17:21 -04:00
|
|
|
.exec(function (err, projects) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err != null) {
|
|
|
|
logger.err({ err }, 'could not get projects for deactivating')
|
|
|
|
}
|
2022-01-10 05:23:05 -05:00
|
|
|
const jobs = _.map(
|
|
|
|
projects,
|
|
|
|
project => cb =>
|
|
|
|
InactiveProjectManager.deactivateProject(
|
|
|
|
project._id,
|
|
|
|
function (err) {
|
|
|
|
if (err) {
|
|
|
|
logger.err(
|
|
|
|
{ project_id: project._id, err: err },
|
|
|
|
'unable to deactivate project'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
cb()
|
|
|
|
}
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
logger.log(
|
2020-11-09 10:48:49 -05:00
|
|
|
{ numberOfProjects: projects && projects.length },
|
2019-05-29 05:21:06 -04:00
|
|
|
'deactivating projects'
|
|
|
|
)
|
2021-04-14 09:17:21 -04:00
|
|
|
async.series(jobs, function (err) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err != null) {
|
2019-07-01 09:48:09 -04:00
|
|
|
logger.warn({ err }, 'error deactivating projects')
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-11-09 10:48:49 -05:00
|
|
|
callback(err, projects)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
deactivateProject(project_id, callback) {
|
|
|
|
logger.log({ project_id }, 'deactivating inactive project')
|
|
|
|
const jobs = [
|
|
|
|
cb => DocstoreManager.archiveProject(project_id, cb),
|
2021-04-27 03:52:58 -04:00
|
|
|
cb => ProjectUpdateHandler.markAsInactive(project_id, cb),
|
2019-05-29 05:21:06 -04:00
|
|
|
]
|
2021-04-14 09:17:21 -04:00
|
|
|
return async.series(jobs, function (err) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err != null) {
|
2019-07-01 09:48:09 -04:00
|
|
|
logger.warn({ err, project_id }, 'error deactivating project')
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
return callback(err)
|
|
|
|
})
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|