write files incrementally

This commit is contained in:
Brian Gough 2017-08-01 14:35:55 +01:00
parent 2ac4f7ac44
commit da86a094a8
4 changed files with 44 additions and 3 deletions

View file

@ -15,7 +15,10 @@ module.exports = CompileController =
ProjectPersistenceManager.markProjectAsJustAccessed request.project_id, (error) ->
return next(error) if error?
CompileManager.doCompile request, (error, outputFiles = []) ->
if error?.terminated
if error?.message is "invalid state"
code = 409 # Http 409 Conflict
status = "retry"
else if error?.terminated
status = "terminated"
else if error?.validate
status = "validation-#{error.validate}"

View file

@ -31,7 +31,7 @@ module.exports = CompileManager =
timer = new Metrics.Timer("write-to-disk")
logger.log project_id: request.project_id, user_id: request.user_id, "syncing resources to disk"
ResourceWriter.syncResourcesToDisk request.project_id, request.resources, compileDir, (error) ->
ResourceWriter.syncResourcesToDisk request, compileDir, (error) ->
if error?
logger.err err:error, project_id: request.project_id, user_id: request.user_id, "error writing resources to disk"
return callback(error)

View file

@ -31,6 +31,12 @@ module.exports = RequestParser =
response.check = @_parseAttribute "check",
compile.options.check,
type: "string"
response.incremental = @_parseAttribute "incremental",
compile.options.incremental,
type: "string"
response.state = @_parseAttribute "state",
compile.options.state,
type: "string"
if response.timeout > RequestParser.MAX_TIMEOUT
response.timeout = RequestParser.MAX_TIMEOUT

View file

@ -11,7 +11,39 @@ settings = require("settings-sharelatex")
parallelFileDownloads = settings.parallelFileDownloads or 1
module.exports = ResourceWriter =
syncResourcesToDisk: (project_id, resources, basePath, callback = (error) ->) ->
syncResourcesToDisk: (request, basePath, callback = (error) ->) ->
if request.incremental?
ResourceWriter.checkState request.incremental, basePath, (error, ok) ->
logger.log state: request.state, result:ok, "checked state on incremental request"
return callback new Error("invalid state") if not ok
ResourceWriter.saveIncrementalResourcesToDisk request.project_id, request.resources, basePath, callback
else
@saveAllResourcesToDisk request.project_id, request.resources, basePath, (error) ->
return callback(error) if error?
ResourceWriter.storeState request.state, basePath, callback
storeState: (state, basePath, callback) ->
logger.log state:state, basePath:basePath, "writing state"
fs.writeFile Path.join(basePath, ".resource-state"), state, {encoding: 'ascii'}, callback
checkState: (state, basePath, callback) ->
fs.readFile Path.join(basePath, ".resource-state"), {encoding:'ascii'}, (err, oldState) ->
logger.log state:state, oldState: oldState, basePath:basePath, err:err, "checking state"
if state is oldState
return callback(null, true)
else
return callback(null, false)
saveIncrementalResourcesToDisk: (project_id, resources, basePath, callback = (error) ->) ->
@_createDirectory basePath, (error) =>
return callback(error) if error?
jobs = for resource in resources
do (resource) =>
(callback) => @_writeResourceToDisk(project_id, resource, basePath, callback)
async.parallelLimit jobs, parallelFileDownloads, callback
saveAllResourcesToDisk: (project_id, resources, basePath, callback = (error) ->) ->
@_createDirectory basePath, (error) =>
return callback(error) if error?
@_removeExtraneousFiles resources, basePath, (error) =>