From 2950c01130e3bd6bc4373307affe371328c86287 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Wed, 9 Aug 2017 15:10:24 +0100 Subject: [PATCH] add comment about syncType/syncState --- services/clsi/app/coffee/RequestParser.coffee | 14 ++++++++++ .../clsi/app/coffee/ResourceWriter.coffee | 26 ++++++++++++++++--- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/services/clsi/app/coffee/RequestParser.coffee b/services/clsi/app/coffee/RequestParser.coffee index 8b8de76e62..596b52995c 100644 --- a/services/clsi/app/coffee/RequestParser.coffee +++ b/services/clsi/app/coffee/RequestParser.coffee @@ -31,10 +31,24 @@ module.exports = RequestParser = response.check = @_parseAttribute "check", compile.options.check, type: "string" + + # The syncType specifies whether the request contains all + # resources (full) or only those resources to be updated + # in-place (incremental). response.syncType = @_parseAttribute "syncType", compile.options.syncType, validValues: ["full", "incremental"] type: "string" + + # The syncState is an identifier passed in with the request + # which has the property that it changes when any resource is + # added, deleted, moved or renamed. + # + # on syncType full the syncState identifier is passed in and + # stored + # + # on syncType incremental the syncState identifier must match + # the stored value response.syncState = @_parseAttribute "syncState", compile.options.syncState, type: "string" diff --git a/services/clsi/app/coffee/ResourceWriter.coffee b/services/clsi/app/coffee/ResourceWriter.coffee index ad14b917e4..9a78671ee6 100644 --- a/services/clsi/app/coffee/ResourceWriter.coffee +++ b/services/clsi/app/coffee/ResourceWriter.coffee @@ -24,8 +24,23 @@ module.exports = ResourceWriter = return callback(error) if error? ResourceWriter.storeSyncState request.syncState, basePath, callback + # The sync state is an identifier which must match for an + # incremental update to be allowed. + # + # The initial value is passed in and stored on a full + # compile. + # + # Subsequent incremental compiles must come with the same value - if + # not they will be rejected with a 409 Conflict response. + # + # An incremental compile can only update existing files with new + # content. The sync state identifier must change if any docs or + # files are moved, added, deleted or renamed. + + SYNC_STATE_FILE: ".project-sync-state" + storeSyncState: (state, basePath, callback) -> - stateFile = Path.join(basePath, ".resource-sync-state") + stateFile = Path.join(basePath, @SYNC_STATE_FILE) if not state? # remove the file if no state passed in logger.log state:state, basePath:basePath, "clearing sync state" fs.unlink stateFile, (err) -> @@ -38,10 +53,13 @@ module.exports = ResourceWriter = fs.writeFile stateFile, state, {encoding: 'ascii'}, callback checkSyncState: (state, basePath, callback) -> - stateFile = Path.join(basePath, ".resource-sync-state") + stateFile = Path.join(basePath, @SYNC_STATE_FILE) fs.readFile stateFile, {encoding:'ascii'}, (err, oldState) -> - # ignore errors, return true if state matches, false otherwise (including errors) - return callback(null, if state is oldState then true else false) + if err? and err.code isnt 'ENOENT' + return callback(err) + else + # return true if state matches, false otherwise (including file not existing) + callback(null, if state is oldState then true else false) saveIncrementalResourcesToDisk: (project_id, resources, basePath, callback = (error) ->) -> @_createDirectory basePath, (error) =>