Debugging for socket bind

This commit is contained in:
Gregor Kleen 2019-04-26 13:22:20 +02:00
parent fbe98adfde
commit 53db7803b7
2 changed files with 29 additions and 12 deletions

View File

@ -122,6 +122,8 @@ dependencies:
- mono-traversable - mono-traversable
- lens-aeson - lens-aeson
- systemd - systemd
- lifted-async
- streaming-commons
other-extensions: other-extensions:
- GeneralizedNewtypeDeriving - GeneralizedNewtypeDeriving

View File

@ -24,9 +24,10 @@ import Language.Haskell.TH.Syntax (qLocation)
import Network.Wai (Middleware) import Network.Wai (Middleware)
import Network.Wai.Handler.Warp (Settings, defaultSettings, import Network.Wai.Handler.Warp (Settings, defaultSettings,
defaultShouldDisplayException, defaultShouldDisplayException,
runSettings, runSettingsSocket, setHost, runSettingsSocket, setHost,
setBeforeMainLoop, setBeforeMainLoop,
setOnException, setPort, getPort) setOnException, setPort, getPort)
import Data.Streaming.Network (bindPortTCP)
import Network.Wai.Middleware.RequestLogger (Destination (Logger), import Network.Wai.Middleware.RequestLogger (Destination (Logger),
IPAddrSource (..), IPAddrSource (..),
OutputFormat (..), destination, OutputFormat (..), destination,
@ -74,6 +75,7 @@ import System.Exit (exitFailure)
import qualified Database.Memcached.Binary.IO as Memcached import qualified Database.Memcached.Binary.IO as Memcached
import qualified System.Systemd.Daemon as Systemd import qualified System.Systemd.Daemon as Systemd
import Control.Concurrent.Async.Lifted.Safe (async, waitAnyCancel)
-- Import all relevant handler modules here. -- Import all relevant handler modules here.
-- (HPack takes care to add new modules to our cabal file nowadays.) -- (HPack takes care to add new modules to our cabal file nowadays.)
@ -292,9 +294,9 @@ makeLogWare app = do
-- | Warp settings for the given foundation value. -- | Warp settings for the given foundation value.
warpSettings :: UniWorX -> Settings warpSettings :: UniWorX -> Settings
warpSettings foundation = defaultSettings warpSettings foundation = defaultSettings
& setPort (foundation ^. _appPort)
& setHost (foundation ^. _appHost)
& setBeforeMainLoop (void Systemd.notifyReady) & setBeforeMainLoop (void Systemd.notifyReady)
& setHost (foundation ^. _appHost)
& setPort (foundation ^. _appPort)
& setOnException (\_req e -> & setOnException (\_req e ->
when (defaultShouldDisplayException e) $ do when (defaultShouldDisplayException e) $ do
logger <- readTVarIO . snd $ appLogger foundation logger <- readTVarIO . snd $ appLogger foundation
@ -338,16 +340,29 @@ appMain = runResourceT $ do
-- Generate the foundation from the settings -- Generate the foundation from the settings
foundation <- makeFoundation settings foundation <- makeFoundation settings
-- Generate a WAI Application from the foundation let logFunc loc src lvl str = do
app <- makeApplication foundation f <- messageLoggerSource foundation <$> readTVarIO (snd $ foundation ^. _appLogger)
f loc src lvl str
-- Run the application with Warp flip runLoggingT logFunc $ do
activatedSockets <- liftIO Systemd.getActivatedSockets -- Generate a WAI Application from the foundation
liftIO $ case activatedSockets of app <- makeApplication foundation
Just [sock]
-> runSettingsSocket (warpSettings foundation) sock app -- Run the application with Warp
_other activatedSockets <- liftIO Systemd.getActivatedSocketsWithNames
-> runSettings (warpSettings foundation) app sockets <- case activatedSockets of
Just socks@(_ : _) -> do
$logInfoS "bind" [st|Ignoring configuration and listening on #{tshow (fmap snd socks)}|]
return $ fmap fst socks
_other -> do
let
host = foundation ^. _appHost
port = foundation ^. _appPort
$logInfoS "bind" [st|Listening on #{tshow host} port #{tshow port} as per configuration|]
liftIO $ pure <$> bindPortTCP port host
let runWarp socket = runSettingsSocket (warpSettings foundation) socket app
liftIO $ void . waitAnyCancel =<< mapM (async . runWarp) sockets
-------------------------------------------------------------- --------------------------------------------------------------