Release all db connections on ctrl-c

Fixes #268
This commit is contained in:
Joe Nelson
2015-12-07 16:54:59 -08:00
parent 24b7a7d3e9
commit 67a3194903
2 changed files with 12 additions and 1 deletions
+1
View File
@@ -61,6 +61,7 @@ executable postgrest
, HTTP, http-types
, MissingH
, Ranged-sets
, unix >= 2.7 && < 3
hs-source-dirs: src
other-modules: Paths_postgrest
+11 -1
View File
@@ -10,7 +10,9 @@ import PostgREST.Error (pgErrResponse, PgError)
import PostgREST.Middleware
import PostgREST.DbStructure
import Control.Monad (unless)
import Control.Concurrent (myThreadId)
import Control.Exception.Base (throwTo, AsyncException(..))
import Control.Monad (unless, void)
import Control.Monad.IO.Class (liftIO)
import Data.Aeson (encode)
import Data.Functor.Identity
@@ -26,6 +28,7 @@ import Network.Wai.Middleware.RequestLogger (logStdout)
import System.IO (BufferMode (..),
hSetBuffering, stderr,
stdin, stdout)
import System.Posix.Signals
import Web.JWT (secret)
isServerVersionSupported :: H.Session P.Postgres IO Bool
@@ -69,6 +72,13 @@ main = do
<> show minimumPgVersion)
) supportedOrError
tid <- myThreadId
void $ installHandler keyboardSignal (Catch $ do
putStrLn "\nReleasing connection pool..."
H.releasePool pool
throwTo tid UserInterrupt
) Nothing
let txSettings = Just (H.ReadCommitted, Just True)
dbOrError <- H.session pool $ H.tx txSettings $ getDbStructure (cs $ configSchema conf)
dbStructure <- either hasqlError return dbOrError