Merge pull request #1595 from sharelatex/bg-remove-old-project-version-request

remove old project version request

GitOrigin-RevId: 1c0a36ddfde4852b4cae90f5f4c4cfcb727eff77
This commit is contained in:
Brian Gough 2019-03-13 09:00:40 +00:00 committed by sharelatex
parent 0c5346460f
commit 0f22a626c8
2 changed files with 59 additions and 40 deletions

View file

@ -132,39 +132,28 @@ module.exports = DocumentUpdaterHandler =
updateProjectStructure: (project_id, projectHistoryId, userId, changes, callback = (error) ->)->
return callback() if !settings.apis.project_history?.sendProjectStructureOps
Project.findOne {_id: project_id}, {version:true}, (err, currentProject) ->
return callback(err) if err?
return callback new Error("project not found") if !currentProject?
docUpdates = DocumentUpdaterHandler._getUpdates('doc', changes.oldDocs, changes.newDocs)
fileUpdates = DocumentUpdaterHandler._getUpdates('file', changes.oldFiles, changes.newFiles)
projectVersion = changes?.newProject?.version
docUpdates = DocumentUpdaterHandler._getUpdates('doc', changes.oldDocs, changes.newDocs)
fileUpdates = DocumentUpdaterHandler._getUpdates('file', changes.oldFiles, changes.newFiles)
projectVersion = changes?.newProject?.version
return callback() if (docUpdates.length + fileUpdates.length) < 1
return callback() if (docUpdates.length + fileUpdates.length) < 1
if !projectVersion?
logger.error {project_id, changes, projectVersion}, "did not receive project version in changes"
return callback(new Error("did not receive project version in changes"))
# FIXME: remove this check and the request to get the project structure version above
# when we are confident in the use of $inc to increment the project structure version
# in all cases.
if projectVersion? && currentProject.version == projectVersion
logger.log {project_id, projectVersion}, "got project version in changes"
else if projectVersion? && currentProject.version != projectVersion
logger.error {project_id, changes, projectVersion, currentProject: currentProject.version}, "project version from db was different from changes (broken lock?)"
else
projectVersion = currentProject.version
logger.warn {project_id, changes, projectVersion}, "did not receive project version in changes"
logger.log {project_id}, "updating project structure in doc updater"
DocumentUpdaterHandler._makeRequest {
path: "/project/#{project_id}"
json: {
docUpdates,
fileUpdates,
userId,
version: projectVersion
projectHistoryId
}
method: "POST"
}, project_id, "update-project-structure", callback
logger.log {project_id}, "updating project structure in doc updater"
DocumentUpdaterHandler._makeRequest {
path: "/project/#{project_id}"
json: {
docUpdates,
fileUpdates,
userId,
version: projectVersion
projectHistoryId
}
method: "POST"
}, project_id, "update-project-structure", callback
_makeRequest: (options, project_id, metricsKey, callback) ->
timer = new metrics.Timer(metricsKey)

View file

@ -405,7 +405,6 @@ describe 'DocumentUpdaterHandler', ->
beforeEach ->
@user_id = 1234
@version = 999
@Project.findOne = sinon.stub().callsArgWith(2,null, {_id: @project_id, version:@version})
describe "with project history disabled", ->
beforeEach ->
@ -438,6 +437,7 @@ describe 'DocumentUpdaterHandler', ->
{ path: '/old_a', doc: _id: new ObjectId(@docIdA.toString()) }
{ path: '/new_b', doc: _id: new ObjectId(@docIdB.toString()) }
]
newProject: {version: @version}
}
docUpdates = [
@ -458,9 +458,12 @@ describe 'DocumentUpdaterHandler', ->
describe "when a doc has been added", ->
it 'should send the structure update to the document updater', (done) ->
@docId = new ObjectId()
@changes = newDocs: [
{ path: '/foo', docLines: 'a\nb', doc: _id: @docId }
]
@changes = {
newDocs: [
{ path: '/foo', docLines: 'a\nb', doc: _id: @docId }
]
newProject: {version: @version}
}
docUpdates = [
id: @docId.toString(),
@ -480,9 +483,12 @@ describe 'DocumentUpdaterHandler', ->
describe "when a file has been added", ->
it 'should send the structure update to the document updater', (done) ->
@fileId = new ObjectId()
@changes = newFiles: [
{ path: '/bar', url: 'filestore.example.com/file', file: _id: @fileId }
]
@changes = {
newFiles: [
{ path: '/bar', url: 'filestore.example.com/file', file: _id: @fileId }
]
newProject: {version: @version}
}
fileUpdates = [
id: @fileId.toString(),
@ -502,9 +508,12 @@ describe 'DocumentUpdaterHandler', ->
describe "when an entity has been deleted", ->
it 'should end the structure update to the document updater', (done) ->
@docId = new ObjectId()
@changes = oldDocs: [
{ path: '/foo', docLines: 'a\nb', doc: _id: @docId }
]
@changes = {
oldDocs: [
{ path: '/foo', docLines: 'a\nb', doc: _id: @docId }
]
newProject: {version: @version}
}
docUpdates = [
id: @docId.toString(),
@ -519,3 +528,24 @@ describe 'DocumentUpdaterHandler', ->
json: {docUpdates, fileUpdates: [], userId: @user_id, @version, @projectHistoryId}
).should.equal true
done()
describe "when the project version is missing", ->
it 'should call the callback with an error', () ->
@docId = new ObjectId()
@changes = {
oldDocs: [
{ path: '/foo', docLines: 'a\nb', doc: _id: @docId }
]
}
docUpdates = [
id: @docId.toString(),
pathname: '/foo',
newPathname: ''
]
@handler.updateProjectStructure @project_id, @projectHistoryId, @user_id, @changes, @callback
@callback.calledWith(new Error()).should.equal true
firstCallArgs = @callback.args[0]
firstCallArgs[0].message.should.equal "did not receive project version in changes"