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

View file

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