mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-07 20:31:06 -05:00
Merge pull request #17065 from overleaf/dp-mongoose-callback-inactive-project-manager
Promisify InactiveProjectManager and InactiveProjectManagerTests GitOrigin-RevId: 985d0d4c80bfd1e46fa3c85c98203432459bdc84
This commit is contained in:
parent
62b4a19788
commit
148f6d6f96
2 changed files with 154 additions and 192 deletions
|
@ -1,17 +1,4 @@
|
||||||
/* 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
|
|
||||||
const OError = require('@overleaf/o-error')
|
const OError = require('@overleaf/o-error')
|
||||||
const async = require('async')
|
|
||||||
const _ = require('lodash')
|
|
||||||
const logger = require('@overleaf/logger')
|
const logger = require('@overleaf/logger')
|
||||||
const DocstoreManager = require('../Docstore/DocstoreManager')
|
const DocstoreManager = require('../Docstore/DocstoreManager')
|
||||||
const ProjectGetter = require('../Project/ProjectGetter')
|
const ProjectGetter = require('../Project/ProjectGetter')
|
||||||
|
@ -19,43 +6,45 @@ const ProjectUpdateHandler = require('../Project/ProjectUpdateHandler')
|
||||||
const { Project } = require('../../models/Project')
|
const { Project } = require('../../models/Project')
|
||||||
const { ObjectId } = require('mongodb')
|
const { ObjectId } = require('mongodb')
|
||||||
const { READ_PREFERENCE_SECONDARY } = require('../../infrastructure/mongodb')
|
const { READ_PREFERENCE_SECONDARY } = require('../../infrastructure/mongodb')
|
||||||
|
const { callbackifyAll } = require('@overleaf/promise-utils')
|
||||||
|
|
||||||
const MILISECONDS_IN_DAY = 86400000
|
const MILISECONDS_IN_DAY = 86400000
|
||||||
module.exports = InactiveProjectManager = {
|
const InactiveProjectManager = {
|
||||||
reactivateProjectIfRequired(projectId, callback) {
|
async reactivateProjectIfRequired(projectId) {
|
||||||
ProjectGetter.getProject(
|
let project
|
||||||
projectId,
|
try {
|
||||||
{ active: true },
|
project = await ProjectGetter.promises.getProject(projectId, {
|
||||||
function (err, project) {
|
active: true,
|
||||||
if (err != null) {
|
})
|
||||||
OError.tag(err, 'error getting project', {
|
} catch (err) {
|
||||||
project_id: projectId,
|
OError.tag(err, 'error getting project', {
|
||||||
})
|
project_id: projectId,
|
||||||
return callback(err)
|
})
|
||||||
}
|
throw err
|
||||||
logger.debug(
|
}
|
||||||
{ projectId, active: project.active },
|
|
||||||
'seeing if need to reactivate project'
|
|
||||||
)
|
|
||||||
|
|
||||||
if (project.active) {
|
logger.debug(
|
||||||
return callback()
|
{ projectId, active: project.active },
|
||||||
}
|
'seeing if need to reactivate project'
|
||||||
|
|
||||||
DocstoreManager.unarchiveProject(projectId, function (err) {
|
|
||||||
if (err != null) {
|
|
||||||
OError.tag(err, 'error reactivating project in docstore', {
|
|
||||||
project_id: projectId,
|
|
||||||
})
|
|
||||||
return callback(err)
|
|
||||||
}
|
|
||||||
ProjectUpdateHandler.markAsActive(projectId, callback)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if (project.active) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await DocstoreManager.promises.unarchiveProject(projectId)
|
||||||
|
} catch (err) {
|
||||||
|
OError.tag(err, 'error reactivating project in docstore', {
|
||||||
|
project_id: projectId,
|
||||||
|
})
|
||||||
|
throw err
|
||||||
|
}
|
||||||
|
|
||||||
|
await ProjectUpdateHandler.promises.markAsActive(projectId)
|
||||||
},
|
},
|
||||||
|
|
||||||
deactivateOldProjects(limit, daysOld, callback) {
|
async deactivateOldProjects(limit, daysOld) {
|
||||||
if (limit == null) {
|
if (limit == null) {
|
||||||
limit = 10
|
limit = 10
|
||||||
}
|
}
|
||||||
|
@ -63,60 +52,59 @@ module.exports = InactiveProjectManager = {
|
||||||
daysOld = 360
|
daysOld = 360
|
||||||
}
|
}
|
||||||
const oldProjectDate = new Date() - MILISECONDS_IN_DAY * daysOld
|
const oldProjectDate = new Date() - MILISECONDS_IN_DAY * daysOld
|
||||||
// use $not $gt to catch non-opened projects where lastOpened is null
|
|
||||||
Project.find({ lastOpened: { $not: { $gt: oldProjectDate } } })
|
let projects
|
||||||
.where('_id')
|
try {
|
||||||
.lt(ObjectId.createFromTime(oldProjectDate / 1000))
|
// use $not $gt to catch non-opened projects where lastOpened is null
|
||||||
.where('active')
|
projects = await Project.find({
|
||||||
.equals(true)
|
lastOpened: { $not: { $gt: oldProjectDate } },
|
||||||
.select('_id')
|
|
||||||
.sort({ _id: 1 })
|
|
||||||
.limit(limit)
|
|
||||||
.read(READ_PREFERENCE_SECONDARY)
|
|
||||||
.exec(function (err, projects) {
|
|
||||||
if (err != null) {
|
|
||||||
logger.err({ err }, 'could not get projects for deactivating')
|
|
||||||
}
|
|
||||||
const jobs = _.map(
|
|
||||||
projects,
|
|
||||||
project => cb =>
|
|
||||||
InactiveProjectManager.deactivateProject(
|
|
||||||
project._id,
|
|
||||||
function (err) {
|
|
||||||
if (err) {
|
|
||||||
logger.err(
|
|
||||||
{ projectId: project._id, err },
|
|
||||||
'unable to deactivate project'
|
|
||||||
)
|
|
||||||
}
|
|
||||||
cb()
|
|
||||||
}
|
|
||||||
)
|
|
||||||
)
|
|
||||||
logger.debug(
|
|
||||||
{ numberOfProjects: projects && projects.length },
|
|
||||||
'deactivating projects'
|
|
||||||
)
|
|
||||||
async.series(jobs, function (err) {
|
|
||||||
if (err != null) {
|
|
||||||
logger.warn({ err }, 'error deactivating projects')
|
|
||||||
}
|
|
||||||
callback(err, projects)
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
.where('_id')
|
||||||
|
.lt(ObjectId.createFromTime(oldProjectDate / 1000))
|
||||||
|
.where('active')
|
||||||
|
.equals(true)
|
||||||
|
.select('_id')
|
||||||
|
.sort({ _id: 1 })
|
||||||
|
.limit(limit)
|
||||||
|
.read(READ_PREFERENCE_SECONDARY)
|
||||||
|
.exec()
|
||||||
|
} catch (err) {
|
||||||
|
logger.err({ err }, 'could not get projects for deactivating')
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.debug(
|
||||||
|
{ numberOfProjects: projects && projects.length },
|
||||||
|
'deactivating projects'
|
||||||
|
)
|
||||||
|
|
||||||
|
for (const project of projects) {
|
||||||
|
try {
|
||||||
|
await InactiveProjectManager.deactivateProject(project._id)
|
||||||
|
} catch (err) {
|
||||||
|
logger.err(
|
||||||
|
{ projectId: project._id, err },
|
||||||
|
'unable to deactivate project'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return projects
|
||||||
},
|
},
|
||||||
|
|
||||||
deactivateProject(projectId, callback) {
|
async deactivateProject(projectId) {
|
||||||
logger.debug({ projectId }, 'deactivating inactive project')
|
logger.debug({ projectId }, 'deactivating inactive project')
|
||||||
const jobs = [
|
|
||||||
cb => DocstoreManager.archiveProject(projectId, cb),
|
try {
|
||||||
cb => ProjectUpdateHandler.markAsInactive(projectId, cb),
|
await DocstoreManager.promises.archiveProject(projectId)
|
||||||
]
|
await ProjectUpdateHandler.promises.markAsInactive(projectId)
|
||||||
async.series(jobs, function (err) {
|
} catch (err) {
|
||||||
if (err != null) {
|
logger.warn({ err, projectId }, 'error deactivating project')
|
||||||
logger.warn({ err, projectId }, 'error deactivating project')
|
throw err
|
||||||
}
|
}
|
||||||
callback(err)
|
|
||||||
})
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
...callbackifyAll(InactiveProjectManager),
|
||||||
|
promises: InactiveProjectManager,
|
||||||
|
}
|
||||||
|
|
|
@ -1,39 +1,29 @@
|
||||||
/* eslint-disable
|
|
||||||
n/handle-callback-err,
|
|
||||||
max-len,
|
|
||||||
no-return-assign,
|
|
||||||
no-unused-vars,
|
|
||||||
*/
|
|
||||||
// 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
|
|
||||||
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
||||||
*/
|
|
||||||
const SandboxedModule = require('sandboxed-module')
|
const SandboxedModule = require('sandboxed-module')
|
||||||
const assert = require('assert')
|
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
const sinon = require('sinon')
|
const sinon = require('sinon')
|
||||||
const modulePath = path.join(
|
const modulePath = path.join(
|
||||||
__dirname,
|
__dirname,
|
||||||
'../../../../app/src/Features/InactiveData/InactiveProjectManager'
|
'../../../../app/src/Features/InactiveData/InactiveProjectManager'
|
||||||
)
|
)
|
||||||
const { expect } = require('chai')
|
|
||||||
const { ObjectId, ReadPreference } = require('mongodb')
|
const { ObjectId, ReadPreference } = require('mongodb')
|
||||||
|
const { expect } = require('chai')
|
||||||
|
|
||||||
describe('InactiveProjectManager', function () {
|
describe('InactiveProjectManager', function () {
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
this.settings = {}
|
this.settings = {}
|
||||||
this.DocstoreManager = {
|
this.DocstoreManager = {
|
||||||
unarchiveProject: sinon.stub(),
|
promises: {
|
||||||
archiveProject: sinon.stub(),
|
unarchiveProject: sinon.stub(),
|
||||||
|
archiveProject: sinon.stub(),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
this.ProjectUpdateHandler = {
|
this.ProjectUpdateHandler = {
|
||||||
markAsActive: sinon.stub(),
|
promises: {
|
||||||
markAsInactive: sinon.stub(),
|
markAsActive: sinon.stub(),
|
||||||
|
markAsInactive: sinon.stub(),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
this.ProjectGetter = { getProject: sinon.stub() }
|
this.ProjectGetter = { promises: { getProject: sinon.stub() } }
|
||||||
this.InactiveProjectManager = SandboxedModule.require(modulePath, {
|
this.InactiveProjectManager = SandboxedModule.require(modulePath, {
|
||||||
requires: {
|
requires: {
|
||||||
mongodb: { ObjectId },
|
mongodb: { ObjectId },
|
||||||
|
@ -48,106 +38,90 @@ describe('InactiveProjectManager', function () {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
return (this.project_id = '1234')
|
this.project_id = '1234'
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('reactivateProjectIfRequired', function () {
|
describe('reactivateProjectIfRequired', function () {
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
this.project = { active: false }
|
this.project = { active: false }
|
||||||
this.ProjectGetter.getProject.callsArgWith(2, null, this.project)
|
this.ProjectGetter.promises.getProject.resolves(this.project)
|
||||||
return this.ProjectUpdateHandler.markAsActive.callsArgWith(1)
|
this.ProjectUpdateHandler.promises.markAsActive.resolves()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should call unarchiveProject', function (done) {
|
it('should call unarchiveProject', async function () {
|
||||||
this.DocstoreManager.unarchiveProject.callsArgWith(1)
|
this.DocstoreManager.promises.unarchiveProject.resolves()
|
||||||
return this.InactiveProjectManager.reactivateProjectIfRequired(
|
await this.InactiveProjectManager.promises.reactivateProjectIfRequired(
|
||||||
this.project_id,
|
this.project_id
|
||||||
err => {
|
|
||||||
this.DocstoreManager.unarchiveProject
|
|
||||||
.calledWith(this.project_id)
|
|
||||||
.should.equal(true)
|
|
||||||
this.ProjectUpdateHandler.markAsActive
|
|
||||||
.calledWith(this.project_id)
|
|
||||||
.should.equal(true)
|
|
||||||
return done()
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
this.DocstoreManager.promises.unarchiveProject
|
||||||
|
.calledWith(this.project_id)
|
||||||
|
.should.equal(true)
|
||||||
|
this.ProjectUpdateHandler.promises.markAsActive
|
||||||
|
.calledWith(this.project_id)
|
||||||
|
.should.equal(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should not mark project as active if error with unarchiving', function (done) {
|
it('should not mark project as active if error with unarchiving', async function () {
|
||||||
const error = new Error('error')
|
this.DocstoreManager.promises.unarchiveProject.rejects()
|
||||||
this.DocstoreManager.unarchiveProject.callsArgWith(1, error)
|
await expect(
|
||||||
return this.InactiveProjectManager.reactivateProjectIfRequired(
|
this.InactiveProjectManager.promises.reactivateProjectIfRequired(
|
||||||
this.project_id,
|
this.project_id
|
||||||
err => {
|
)
|
||||||
err.should.equal(error)
|
).to.be.rejected
|
||||||
this.DocstoreManager.unarchiveProject
|
|
||||||
.calledWith(this.project_id)
|
this.DocstoreManager.promises.unarchiveProject
|
||||||
.should.equal(true)
|
.calledWith(this.project_id)
|
||||||
this.ProjectUpdateHandler.markAsActive
|
.should.equal(true)
|
||||||
.calledWith(this.project_id)
|
this.ProjectUpdateHandler.promises.markAsActive
|
||||||
.should.equal(false)
|
.calledWith(this.project_id)
|
||||||
return done()
|
.should.equal(false)
|
||||||
}
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should not call unarchiveProject if it is active', function (done) {
|
it('should not call unarchiveProject if it is active', async function () {
|
||||||
this.project.active = true
|
this.project.active = true
|
||||||
this.DocstoreManager.unarchiveProject.callsArgWith(1)
|
this.DocstoreManager.promises.unarchiveProject.resolves()
|
||||||
return this.InactiveProjectManager.reactivateProjectIfRequired(
|
await this.InactiveProjectManager.promises.reactivateProjectIfRequired(
|
||||||
this.project_id,
|
this.project_id
|
||||||
err => {
|
|
||||||
this.DocstoreManager.unarchiveProject
|
|
||||||
.calledWith(this.project_id)
|
|
||||||
.should.equal(false)
|
|
||||||
this.ProjectUpdateHandler.markAsActive
|
|
||||||
.calledWith(this.project_id)
|
|
||||||
.should.equal(false)
|
|
||||||
return done()
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
this.DocstoreManager.promises.unarchiveProject
|
||||||
|
.calledWith(this.project_id)
|
||||||
|
.should.equal(false)
|
||||||
|
this.ProjectUpdateHandler.promises.markAsActive
|
||||||
|
.calledWith(this.project_id)
|
||||||
|
.should.equal(false)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('deactivateProject', function () {
|
describe('deactivateProject', function () {
|
||||||
it('should call unarchiveProject and markAsInactive', function (done) {
|
it('should call unarchiveProject and markAsInactive', async function () {
|
||||||
this.DocstoreManager.archiveProject.callsArgWith(1)
|
this.DocstoreManager.promises.archiveProject.resolves()
|
||||||
|
this.ProjectUpdateHandler.promises.markAsInactive.resolves()
|
||||||
|
|
||||||
this.ProjectUpdateHandler.markAsInactive.callsArgWith(1)
|
await this.InactiveProjectManager.promises.deactivateProject(
|
||||||
|
this.project_id
|
||||||
return this.InactiveProjectManager.deactivateProject(
|
|
||||||
this.project_id,
|
|
||||||
err => {
|
|
||||||
this.DocstoreManager.archiveProject
|
|
||||||
.calledWith(this.project_id)
|
|
||||||
.should.equal(true)
|
|
||||||
this.ProjectUpdateHandler.markAsInactive
|
|
||||||
.calledWith(this.project_id)
|
|
||||||
.should.equal(true)
|
|
||||||
return done()
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
this.DocstoreManager.promises.archiveProject
|
||||||
|
.calledWith(this.project_id)
|
||||||
|
.should.equal(true)
|
||||||
|
this.ProjectUpdateHandler.promises.markAsInactive
|
||||||
|
.calledWith(this.project_id)
|
||||||
|
.should.equal(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should not call markAsInactive if there was a problem archiving in docstore', function (done) {
|
it('should not call markAsInactive if there was a problem archiving in docstore', async function () {
|
||||||
this.DocstoreManager.archiveProject.callsArgWith(1, 'errorrr')
|
this.DocstoreManager.promises.archiveProject.rejects()
|
||||||
|
this.ProjectUpdateHandler.promises.markAsInactive.resolves()
|
||||||
|
|
||||||
this.ProjectUpdateHandler.markAsInactive.callsArgWith(1)
|
await expect(
|
||||||
|
this.InactiveProjectManager.promises.deactivateProject(this.project_id)
|
||||||
return this.InactiveProjectManager.deactivateProject(
|
).to.be.rejected
|
||||||
this.project_id,
|
this.DocstoreManager.promises.archiveProject
|
||||||
err => {
|
.calledWith(this.project_id)
|
||||||
err.should.equal('errorrr')
|
.should.equal(true)
|
||||||
this.DocstoreManager.archiveProject
|
this.ProjectUpdateHandler.promises.markAsInactive
|
||||||
.calledWith(this.project_id)
|
.calledWith(this.project_id)
|
||||||
.should.equal(true)
|
.should.equal(false)
|
||||||
this.ProjectUpdateHandler.markAsInactive
|
|
||||||
.calledWith(this.project_id)
|
|
||||||
.should.equal(false)
|
|
||||||
return done()
|
|
||||||
}
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue