stream request to file outside of project lock

This commit is contained in:
Hayden Faulds 2018-01-29 16:08:41 +00:00
parent 9496480a29
commit a9be50ebcd
2 changed files with 26 additions and 34 deletions

View file

@ -11,25 +11,20 @@ LockManager = require("../../infrastructure/LockManager")
module.exports = UpdateMerger =
mergeUpdate: (user_id, project_id, path, updateRequest, source, callback = (error) ->)->
logger.log project_id:project_id, path:path, "merging update from tpds"
LockManager.runWithLock project_id,
(cb) => UpdateMerger.mergeUpdateWithoutLock(user_id, project_id, path, updateRequest, source, cb)
callback
mergeUpdateWithoutLock: (user_id, project_id, path, updateRequest, source, callback = (error) ->)->
projectLocator.findElementByPath project_id, path, (err, element)=>
# Returns an error if the element is not found
#return callback(err) if err?
logger.log project_id:project_id, path:path, "found element by path for merging update from tpds"
elementId = undefined
if element?
elementId = element._id
UpdateMerger.p.writeStreamToDisk project_id, elementId, updateRequest, (err, fsPath)->
callback = _.wrap callback, (cb, arg) ->
fs.unlink fsPath, (err) ->
if err?
logger.err project_id:project_id, fsPath:fsPath, "error deleting file"
cb(arg)
UpdateMerger.p.writeStreamToDisk project_id, updateRequest, (err, fsPath)->
return callback(err) if err?
LockManager.runWithLock project_id,
(cb) => UpdateMerger.mergeUpdateWithoutLock user_id, project_id, path, fsPath, source, cb
(mergeErr) ->
fs.unlink fsPath, (deleteErr) ->
if deleteErr?
logger.err project_id:project_id, fsPath:fsPath, "error deleting file"
callback mergeErr
mergeUpdateWithoutLock: (user_id, project_id, path, fsPath, source, callback = (error) ->)->
projectLocator.findElementByPath project_id, path, (err, element)=>
logger.log {project_id, path, fsPath}, "found element by path for merging update from tpds"
elementId = element?._id
FileTypeManager.isBinary path, fsPath, (err, isFile)->
return callback(err) if err?
if isFile
@ -83,25 +78,23 @@ module.exports = UpdateMerger =
else
editorController.addFileWithoutLock project_id, folder?._id, fileName, fsPath, source, user_id, finish
writeStreamToDisk: (project_id, file_id, stream, callback = (err, fsPath)->)->
if !file_id?
file_id = uuid.v4()
dumpPath = "#{Settings.path.dumpFolder}/#{project_id}_#{file_id}"
writeStreamToDisk: (project_id, stream, callback = (err, fsPath)->)->
dumpPath = "#{Settings.path.dumpFolder}/#{project_id}_#{uuid.v4()}"
writeStream = fs.createWriteStream(dumpPath)
stream.pipe(writeStream)
stream.on 'error', (err)->
logger.err err:err, project_id:project_id, file_id:file_id, dumpPath:dumpPath,
logger.err {err, project_id, dumpPath},
"something went wrong with incoming tpds update stream"
writeStream.on 'error', (err)->
logger.err err:err, project_id:project_id, file_id:file_id, dumpPath:dumpPath,
logger.err {err, project_id, dumpPath},
"something went wrong with writing tpds update to disk"
stream.on 'end', ->
logger.log project_id:project_id, file_id:file_id, dumpPath:dumpPath, "incoming tpds update stream ended"
logger.log {project_id, dumpPath}, "incoming tpds update stream ended"
writeStream.on "finish", ->
logger.log project_id:project_id, file_id:file_id, dumpPath:dumpPath, "tpds update write stream finished"
logger.log {project_id, dumpPath}, "tpds update write stream finished"
callback null, dumpPath
readFileIntoTextArray = (path, callback)->

View file

@ -40,7 +40,7 @@ describe 'UpdateMerger :', ->
beforeEach ->
@path = "/doc1"
@fsPath = "file/system/path.tex"
@updateMerger.p.writeStreamToDisk = sinon.stub().callsArgWith(3, null, @fsPath)
@updateMerger.p.writeStreamToDisk = sinon.stub().callsArgWith(2, null, @fsPath)
@FileTypeManager.isBinary = sinon.stub()
describe "doc updates", () ->
@ -159,7 +159,6 @@ describe 'UpdateMerger :', ->
@editorController.replaceFileWithoutLock = sinon.stub().callsArg(5)
@editorController.deleteEntityWithoutLock = sinon.stub()
@editorController.mkdirpWithoutLock = sinon.stub().withArgs(@project_id).callsArgWith(2, null, [@folder], @folder)
@updateMerger.p.writeStreamToDisk = sinon.stub().withArgs(@project_id, @file_id, @update).callsArgWith(3, null, @fsPath)
it 'should replace file if the file already exists', (done)->
@updateMerger.p.processFile @project_id, @file_id, @fsPath, @path, @source, @user_id, =>