add comment about syncType/syncState

This commit is contained in:
Brian Gough
2017-08-09 15:10:24 +01:00
parent 86fa940c97
commit c25e96bbc3
2 changed files with 36 additions and 4 deletions

View File

@@ -31,10 +31,24 @@ module.exports = RequestParser =
response.check = @_parseAttribute "check", response.check = @_parseAttribute "check",
compile.options.check, compile.options.check,
type: "string" 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", response.syncType = @_parseAttribute "syncType",
compile.options.syncType, compile.options.syncType,
validValues: ["full", "incremental"] validValues: ["full", "incremental"]
type: "string" 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", response.syncState = @_parseAttribute "syncState",
compile.options.syncState, compile.options.syncState,
type: "string" type: "string"

View File

@@ -24,8 +24,23 @@ module.exports = ResourceWriter =
return callback(error) if error? return callback(error) if error?
ResourceWriter.storeSyncState request.syncState, basePath, callback 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) -> 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 if not state? # remove the file if no state passed in
logger.log state:state, basePath:basePath, "clearing sync state" logger.log state:state, basePath:basePath, "clearing sync state"
fs.unlink stateFile, (err) -> fs.unlink stateFile, (err) ->
@@ -38,10 +53,13 @@ module.exports = ResourceWriter =
fs.writeFile stateFile, state, {encoding: 'ascii'}, callback fs.writeFile stateFile, state, {encoding: 'ascii'}, callback
checkSyncState: (state, basePath, 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) -> fs.readFile stateFile, {encoding:'ascii'}, (err, oldState) ->
# ignore errors, return true if state matches, false otherwise (including errors) if err? and err.code isnt 'ENOENT'
return callback(null, if state is oldState then true else false) 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) ->) -> saveIncrementalResourcesToDisk: (project_id, resources, basePath, callback = (error) ->) ->
@_createDirectory basePath, (error) => @_createDirectory basePath, (error) =>