Pause updates while purging or restarting apps, and ensure an applyTarget is triggered after the actions run

Signed-off-by: Pablo Carranza Velez <pablo@resin.io>
This commit is contained in:
Pablo Carranza Velez 2018-01-25 12:53:54 -08:00
parent 82d5a16c8c
commit 484a688dbd
2 changed files with 29 additions and 9 deletions

View File

@ -61,10 +61,13 @@ class ApplicationManagerRouter
stoppedApp = _.cloneDeep(app)
stoppedApp.services = []
currentState.local.apps[appId] = stoppedApp
@deviceState.applyIntermediateTarget(currentState, { skipLock: true })
.then =>
currentState.local.apps[appId] = app
@deviceState.pausingApply =>
@deviceState.applyIntermediateTarget(currentState, { skipLock: true })
.then =>
currentState.local.apps[appId] = app
@deviceState.applyIntermediateTarget(currentState, { skipLock: true })
.finally =>
@deviceState.triggerApplyTarget()
doPurge = (appId, force) =>
@logger.logSystemMessage("Purging data for app #{appId}", { appId }, 'Purge data')
@ -76,10 +79,13 @@ class ApplicationManagerRouter
purgedApp.services = []
purgedApp.volumes = {}
currentState.local.apps[appId] = purgedApp
@deviceState.applyIntermediateTarget(currentState, { skipLock: true })
.then =>
currentState.local.apps[appId] = app
@deviceState.pausingApply =>
@deviceState.applyIntermediateTarget(currentState, { skipLock: true })
.then =>
currentState.local.apps[appId] = app
@deviceState.applyIntermediateTarget(currentState, { skipLock: true })
.finally =>
@deviceState.triggerApplyTarget()
.tap =>
@logger.logSystemMessage('Purged data', { appId }, 'Purge data success')
.catch (err) =>

View File

@ -533,12 +533,26 @@ module.exports = class DeviceState extends EventEmitter
updateContext.applyContinueScheduled = false
@applyTarget({ force, initial, intermediate }, updateContext)
pauseNextApply: =>
@applyBlocker = new Promise (resolve) =>
@applyUnblocker = resolve
pausingApply: (fn) =>
lock = =>
@_writeLock('pause').disposer (release) ->
release()
pause = =>
Promise.try =>
res = null
@applyBlocker = new Promise (resolve) ->
res = resolve
return res
.disposer (resolve) ->
resolve()
Promise.using lock(), ->
Promise.using pause(), ->
fn()
resumeNextApply: =>
@applyUnblocker?()
return
triggerApplyTarget: ({ force = false, delay = 0, initial = false } = {}) =>
if @applyInProgress