packages feed

warp-systemd 0.1.1.0 → 0.2.0.0

raw patch · 3 files changed

+38/−11 lines, 3 filesdep ~base

Dependency ranges changed: base

Files

CHANGELOG.md view
@@ -1,5 +1,9 @@ # Revision history for warp-systemd +## 0.2.0.0 -- 2021-07-06++ * Allow using healthchecks as conditional to trigger heartbeat+ ## 0.1.1.0 -- 2020-07-09   * Update `network` package and use `withFdSocket` ([network#399](https://github.com/haskell/network/pull/399))
src/Network/Wai/Handler/Warp/Systemd.hs view
@@ -19,6 +19,9 @@   , heartbeatInterval   , setHeartbeatInterval +  , heartbeatCheck+  , setHeartbeatCheck+   , onBeginShutdown   , setOnBeginShutdown @@ -57,6 +60,7 @@   , _logWarn :: String -> IO ()   , _requireSocketActivation :: Bool   , _heartbeatInterval :: Maybe Int+  , _heartbeatCheck :: IO ()   , _dontOverrideInstallShutdownHandler :: Bool   , _onBeginShutdown :: IO ()   }@@ -68,6 +72,7 @@   , _logWarn = SIO.hPutStrLn SIO.stderr . ("WARNING: " ++)   , _requireSocketActivation = False   , _heartbeatInterval = Nothing+  , _heartbeatCheck = return ()   , _dontOverrideInstallShutdownHandler = False   , _onBeginShutdown = return ()   }@@ -98,6 +103,13 @@ heartbeatInterval :: Lens' SystemdSettings (Maybe Int) heartbeatInterval = lens _heartbeatInterval setHeartbeatInterval +-- | Run an action before emitting a hearbeat and if it throws an exception, print a warning+--   and skip systemd notification.+--+-- Default: @return ()@+heartbeatCheck :: Lens' SystemdSettings (IO ())+heartbeatCheck = lens _heartbeatCheck setHeartbeatCheck+ -- | If @True@, do not override 'Warp.Settings'' with -- 'setInstallShutdownHandler'. This lets you provide your own -- shutdown handler functionality. Enabling this setting will@@ -138,6 +150,10 @@ setHeartbeatInterval :: Maybe Int -> SystemdSettings -> SystemdSettings setHeartbeatInterval x s = s { _heartbeatInterval = x } +-- | See 'heartbeatCheck'+setHeartbeatCheck :: IO () -> SystemdSettings -> SystemdSettings+setHeartbeatCheck action s = s { _heartbeatCheck = action }+ -- | See 'dontOverrideInstallShutdownHandler' setDontOverrideInstallShutdownHandler :: Bool -> SystemdSettings -> SystemdSettings setDontOverrideInstallShutdownHandler x s = s { _dontOverrideInstallShutdownHandler = x }@@ -164,7 +180,7 @@ runSystemdWarp saSettings settings app = do    forM_ (_heartbeatInterval saSettings) $ \interval -> do-    forkIO (heartbeat (_logWarn saSettings) interval)+    forkIO (heartbeat (_logWarn saSettings) (_heartbeatCheck saSettings) interval)      socketActivationSockets <- Systemd.getActivatedSockets @@ -225,18 +241,25 @@     Nothing ->       runSettings settings' app -heartbeat :: (String -> IO ()) -> Int -> IO ()-heartbeat flogWarn delaySeconds = loop where+heartbeat :: (String -> IO ()) -> IO () -> Int -> IO ()+heartbeat flogWarn action delaySeconds = loop where   loop = do     let delayMicroSeconds = delaySeconds * 1000 * 1000-    r <- Systemd.notifyWatchdog-    case r of-      Nothing -> do-        flogWarn "Systemd heartbeat notification does not seem to arrive. Stopping heartbeat notifications."-        return ()-      Just _ -> do+    eitherCheck <- try action+    case eitherCheck of+      Left exc -> do+        flogWarn $ "Systemd heartbeat check failed: " <> displayException (exc :: SomeException)         threadDelay delayMicroSeconds         loop+      Right () -> do+        r <- Systemd.notifyWatchdog+        case r of+          Nothing -> do+            flogWarn "Systemd heartbeat notification does not seem to arrive. Stopping heartbeat notifications."+            return ()+          Just _ -> do+            threadDelay delayMicroSeconds+            loop  ---------------- Minimal dependency-free lens ---------------- 
warp-systemd.cabal view
@@ -1,5 +1,5 @@ name:                warp-systemd-version: 0.1.1.0+version: 0.2.0.0 synopsis:            Socket activation and other systemd integration for the Warp web server (WAI) homepage:            https://github.com/hercules-ci/warp-systemd license:             BSD3@@ -14,7 +14,7 @@  library   exposed-modules:     Network.Wai.Handler.Warp.Systemd-  build-depends:       base >=4.9 && < 4.14+  build-depends:       base >=4.9 && < 4.15                      , network >= 3.1 && < 3.2                      , systemd == 2.*                      , unix