use LockManger.runWithLock

This commit is contained in:
Hayden Faulds 2018-01-09 16:00:08 +00:00
parent fe62db05a0
commit 817840840a
3 changed files with 27 additions and 26 deletions

View file

@ -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

View file

@ -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...

View file

@ -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