diff --git a/src/application-manager.coffee b/src/application-manager.coffee index d95427a4..cd9385a4 100644 --- a/src/application-manager.coffee +++ b/src/application-manager.coffee @@ -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) => diff --git a/src/device-state.coffee b/src/device-state.coffee index 1804f20b..9b73b5ef 100644 --- a/src/device-state.coffee +++ b/src/device-state.coffee @@ -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