-- | Atomically modifies the state of temporary reservations in
-- WConfdMonad in the presence of possible errors.
modifyTempResStateErr
  :: (ConfigData -> StateT TempResState ErrorResult a) -> WConfdMonad a
modifyTempResStateErr f = do
  -- we use Compose to traverse the composition of applicative functors
  -- @ErrorResult@ and @(,) a@
  let f' ds = traverseOf2 dsTempResL
              (runStateT (f (csConfigData . dsConfigState $ ds))) ds
  dh <- daemonHandle
  r <- toErrorBase $ atomicModifyIORefErr (dhDaemonState dh)
                                          (liftM swap . f')
  -- logDebug $ "Current temporary reservations: " ++ J.encode tr
  logDebug "Triggering temporary reservations write"
  liftBase . triggerAndWait_ . dhSaveTempResWorker $ dh
  logDebug "Temporary reservations write finished"
  return r

-- | Atomically modifies the state of temporary reservations in
-- WConfdMonad.
modifyTempResStateErr
  :: (ConfigData -> StateT TempResState ErrorResult a) -> WConfdMonad a
modifyTempResStateErr f = do
  -- we use Compose to traverse the composition of applicative functors
  -- @ErrorResult@ and @(,) a@
  let f' ds = traverseOf2 dsTempResL
              (runStateT (f (csConfigData . dsConfigState $ ds))) ds
  dh <- daemonHandle
  r <- toErrorBase $ atomicModifyIORefErr (dhDaemonState dh)
                                          (liftM swap . f')
  -- logDebug $ "Current temporary reservations: " ++ J.encode tr
  logDebug "Triggering temporary reservations write"
  liftBase . triggerAndWait_ . dhSaveTempResWorker $ dh
  logDebug "Temporary reservations write finished"
  return r

-- | Atomically modifies the state of temporary reservations in
-- WConfdMonad.
modifyTempResStateErr f = do
  -- we use Compose to traverse the composition of applicative functors
  -- @ErrorResult@ and @(,) a@
  let f' ds = traverseOf2 dsTempResL
              (runStateT (f (csConfigData . dsConfigState $ ds))) ds
  dh <- daemonHandle
  r <- toErrorBase $ atomicModifyIORefErr (dhDaemonState dh)
                                          (liftM swap . f')
  -- logDebug $ "Current temporary reservations: " ++ J.encode tr
  logDebug "Triggering temporary reservations write"
  liftBase . triggerAndWait_ . dhSaveTempResWorker $ dh
  logDebug "Temporary reservations write finished"
  return r

-- | Atomically modifies the state of temporary reservations in
-- WConfdMonad.