fix(admin): log admin server exceptions and close admin socket
Admin server was crashing silently and requests hang indefinitely. With this fix, we are now logging the admin server exceptions and also close the socket afterwards so admin request don't hang. Signed-off-by: Taimoor Zaeem <taimoorzaeem@gmail.com>
This commit is contained in:
committed by
steve-chavez
parent
c98a8c2a29
commit
ae65768569
+13
-3
@@ -11,7 +11,8 @@ import Control.Monad.Extra (whenJust)
|
||||
import Network.Socket hiding (addrFamily)
|
||||
import Network.Socket.ByteString
|
||||
|
||||
import PostgREST.AppState (AppState)
|
||||
import PostgREST.AppState (AppState, getConfig)
|
||||
import PostgREST.Config (AppConfig (..))
|
||||
import PostgREST.MediaType (MediaType (..), toContentType)
|
||||
import PostgREST.Metrics (metricsToText)
|
||||
import PostgREST.Network (resolveSocketToAddress)
|
||||
@@ -24,13 +25,22 @@ import Protolude
|
||||
|
||||
runAdmin :: AppState -> Maybe NS.Socket -> NS.Socket -> Warp.Settings -> IO ()
|
||||
runAdmin appState maybeAdminSocket socketREST settings = do
|
||||
conf <- getConfig appState
|
||||
whenJust maybeAdminSocket $ \adminSocket -> do
|
||||
address <- resolveSocketToAddress adminSocket
|
||||
observer $ AdminStartObs address
|
||||
void . forkIO $ Warp.runSettingsSocket settings adminSocket adminApp
|
||||
void . forkIO $ handle (onError adminSocket) $
|
||||
Warp.runSettingsSocket (adminServerSettings conf address) adminSocket adminApp
|
||||
where
|
||||
adminApp = admin appState socketREST
|
||||
observer = AppState.getObserver appState
|
||||
adminServerSettings config addr =
|
||||
settings
|
||||
& Warp.setBeforeMainLoop (observer $ AdminStartObs addr)
|
||||
& maybe identity Warp.setPort (configAdminServerPort config)
|
||||
|
||||
onError adminSock ex = do
|
||||
observer $ AdminServerCrashedObs ex
|
||||
NS.close adminSock -- we close the socket so request doesn't hang
|
||||
|
||||
-- | PostgREST admin application
|
||||
admin :: AppState.AppState -> NS.Socket -> Wai.Application
|
||||
|
||||
Reference in New Issue
Block a user