{-# LANGUAGE ScopedTypeVariables #-} module HomeAssistant.Runtime.Supervisor ( supervised , Fatal(..) ) where import Control.Exception.Annotated ( Exception , Handler (..) , SomeException , checkpoint, Annotation (..) ) import Data.Text (Text) import Data.Void (Void) import Control.Monad.IO.Class (MonadIO) import Control.Monad.Catch (MonadCatch, MonadMask) import Katip (KatipContext, logFM, ls) import Control.Retry (RetryStatus(..), recovering, fullJitterBackoff, capDelay, skipAsyncExceptions) import Control.Monad (when) import Katip.Core (Severity(..)) -- | A failure that cannot be fixed by restarting; propagates out of -- 'supervised' and terminates the process. newtype Fatal = Fatal Text deriving (Show) instance Exception Fatal data SupervisorException = RetriesExhausted deriving Show instance Exception SupervisorException -- | Runs the action forever, restarting it with backoff after synchronous -- exceptions; async exceptions propagate. 'Fatal' is rethrown (crashing the -- caller) rather than restarted. The action must never return normally and -- must not be wrapped in checkpoints around 'Fatal'-throwing code: a -- doubly-wrapped 'Fatal' is indistinguishable from a crash and would be -- restarted instead of escalated. supervised :: (MonadIO m, MonadCatch m, KatipContext m, MonadMask m) => Text -> m Void -> m Void supervised name action = checkpoint (Annotation name) $ do recovering retryPolicy (skipAsyncExceptions ++ handlers) $ \RetryStatus{rsIterNumber} -> do when (rsIterNumber > 0) $ logFM WarningS $ ls $ "Child crashed, retry no " <> show rsIterNumber action where handlers = [ \_retryStatus -> Handler $ \(_ :: Fatal) -> pure False , \_retryStatus -> Handler $ \(_ :: SomeException) -> pure True ] retryPolicy = capDelay (15 * 1_000_000) (fullJitterBackoff 50_000)