diff --git a/services/web/app/coffee/Features/Uploads/ProjectUploadController.coffee b/services/web/app/coffee/Features/Uploads/ProjectUploadController.coffee index 523a4ca8a5..1d81fdb831 100644 --- a/services/web/app/coffee/Features/Uploads/ProjectUploadController.coffee +++ b/services/web/app/coffee/Features/Uploads/ProjectUploadController.coffee @@ -39,23 +39,20 @@ module.exports = ProjectUploadController = logger.log folder_id:folder_id, project_id:project_id, "getting upload file request" user_id = AuthenticationController.getLoggedInUserId(req) - LockManager.getLock project_id, (err)-> - if err? - logger.err err:err, project_id:project_id, source:source, "could not get lock to uploadFile" - return callback(err) - FileSystemImportManager.addEntity user_id, project_id, folder_id, name, path, true, (error, entity) -> - LockManager.releaseLock project_id, -> - fs.unlink path, -> - timer.done() - if error? - logger.error - err: error, project_id: project_id, file_path: path, - file_name: name, folder_id: folder_id, - "error uploading file" - res.send success: false - else - logger.log - project_id: project_id, file_path: path, file_name: name, folder_id: folder_id - "uploaded file" - res.send success: true, entity_id: entity?._id, entity_type: entity?.type + LockManager.runWithLock project_id, + (cb) -> FileSystemImportManager.addEntity user_id, project_id, folder_id, name, path, true, cb + (error, entity) -> + fs.unlink path, -> + timer.done() + if error? + logger.error + err: error, project_id: project_id, file_path: path, + file_name: name, folder_id: folder_id, + "error uploading file" + res.send success: false + else + logger.log + project_id: project_id, file_path: path, file_name: name, folder_id: folder_id + "uploaded file" + res.send success: true, entity_id: entity?._id, entity_type: entity?.type diff --git a/services/web/app/coffee/infrastructure/LockManager.coffee b/services/web/app/coffee/infrastructure/LockManager.coffee index 3064fffabf..5532d5bd94 100644 --- a/services/web/app/coffee/infrastructure/LockManager.coffee +++ b/services/web/app/coffee/infrastructure/LockManager.coffee @@ -51,4 +51,11 @@ module.exports = LockManager = releaseLock: (key, callback)-> rclient.del LockManager._blockingKey(key), callback - + runWithLock: (key, runner = ( (releaseLock = (error) ->) -> ), callback = ( (error) -> )) -> + LockManager.getLock key, (error) -> + return callback(error) if error? + runner (error1, values...) -> + LockManager.releaseLock key, (error2) -> + error = error1 or error2 + return callback(error) if error? + callback null, values... diff --git a/services/web/test/unit/coffee/Uploads/ProjectUploadControllerTests.coffee b/services/web/test/unit/coffee/Uploads/ProjectUploadControllerTests.coffee index 24ff97b30f..488250b647 100644 --- a/services/web/test/unit/coffee/Uploads/ProjectUploadControllerTests.coffee +++ b/services/web/test/unit/coffee/Uploads/ProjectUploadControllerTests.coffee @@ -18,8 +18,8 @@ describe "ProjectUploadController", -> @AuthenticationController = getLoggedInUserId: sinon.stub().returns(@user_id) @LockManager = - getLock : sinon.stub().yields() - releaseLock : sinon.stub().yields() + runWithLock : sinon.spy((key, runner, callback) -> runner(callback)) + @ProjectUploadController = SandboxedModule.require modulePath, requires: "./ProjectUploadManager" : @ProjectUploadManager = {} "./FileSystemImportManager" : @FileSystemImportManager = {} @@ -130,16 +130,13 @@ describe "ProjectUploadController", -> @ProjectUploadController.uploadFile @req, @res it "should take the lock", -> - @LockManager.getLock.calledWith(@project_id).should.equal true + @LockManager.runWithLock.calledWith(@project_id).should.equal true it "should insert the file", -> @FileSystemImportManager.addEntity .calledWith(@user_id, @project_id, @folder_id, @name, @path) .should.equal true - it "should release the lock", -> - @LockManager.releaseLock.calledWith(@project_id).should.equal true - it "should return a successful response to the FileUploader client", -> expect(@res.body).to.deep.equal success: true