2019-05-29 05:21:06 -04:00
|
|
|
/* eslint-disable
|
|
|
|
max-len,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* 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 = {
|
2023-03-21 09:21:57 -04:00
|
|
|
reactivateProjectIfRequired(projectId, callback) {
|
2023-02-27 07:41:53 -05:00
|
|
|
ProjectGetter.getProject(
|
2023-03-21 09:21:57 -04:00
|
|
|
projectId,
|
2021-04-14 09:17:21 -04:00
|
|
|
{ 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', {
|
2023-03-21 09:21:57 -04:00
|
|
|
project_id: projectId,
|
2020-08-11 05:35:08 -04:00
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
return callback(err)
|
|
|
|
}
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug(
|
2023-03-21 09:21:57 -04:00
|
|
|
{ projectId, active: project.active },
|
2021-04-14 09:17:21 -04:00
|
|
|
'seeing if need to reactivate project'
|
|
|
|
)
|
|
|
|
|
|
|
|
if (project.active) {
|
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
|
2023-03-21 09:21:57 -04:00
|
|
|
DocstoreManager.unarchiveProject(projectId, function (err) {
|
2021-04-14 09:17:21 -04:00
|
|
|
if (err != null) {
|
|
|
|
OError.tag(err, 'error reactivating project in docstore', {
|
2023-03-21 09:21:57 -04:00
|
|
|
project_id: projectId,
|
2021-04-14 09:17:21 -04:00
|
|
|
})
|
|
|
|
return callback(err)
|
|
|
|
}
|
2023-03-21 09:21:57 -04:00
|
|
|
ProjectUpdateHandler.markAsActive(projectId, callback)
|
2021-04-14 09:17:21 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
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(
|
2023-03-21 09:21:57 -04:00
|
|
|
{ projectId: project._id, err },
|
2022-01-10 05:23:05 -05:00
|
|
|
'unable to deactivate project'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
cb()
|
|
|
|
}
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug(
|
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
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2023-03-21 09:21:57 -04:00
|
|
|
deactivateProject(projectId, callback) {
|
|
|
|
logger.debug({ projectId }, 'deactivating inactive project')
|
2019-05-29 05:21:06 -04:00
|
|
|
const jobs = [
|
2023-03-21 09:21:57 -04:00
|
|
|
cb => DocstoreManager.archiveProject(projectId, cb),
|
|
|
|
cb => ProjectUpdateHandler.markAsInactive(projectId, cb),
|
2019-05-29 05:21:06 -04:00
|
|
|
]
|
2023-02-27 07:41:53 -05:00
|
|
|
async.series(jobs, function (err) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err != null) {
|
2023-03-21 09:21:57 -04:00
|
|
|
logger.warn({ err, projectId }, 'error deactivating project')
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2023-02-27 07:41:53 -05:00
|
|
|
callback(err)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|