{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module Network.Wait.PostgreSQL (
waitPostgreSql,
waitPostgreSqlVerbose,
waitPostgreSqlVerboseFormat,
waitPostgreSqlWith
) where
import Control.Monad
import Control.Monad.Catch
import Control.Monad.IO.Class
import Control.Retry
import Database.PostgreSQL.Simple
import Database.PostgreSQL.Simple.Internal
import Network.Wait
waitPostgreSql
:: (MonadIO m, MonadMask m)
=> RetryPolicyM m -> ConnectInfo -> m Connection
waitPostgreSql :: RetryPolicyM m -> ConnectInfo -> m Connection
waitPostgreSql = [RetryStatus -> Handler m Bool]
-> RetryPolicyM m -> ConnectInfo -> m Connection
forall (m :: * -> *).
(MonadIO m, MonadMask m) =>
[RetryStatus -> Handler m Bool]
-> RetryPolicyM m -> ConnectInfo -> m Connection
waitPostgreSqlWith []
waitPostgreSqlVerbose
:: (MonadIO m, MonadMask m)
=> (String -> m ()) -> RetryPolicyM m -> ConnectInfo -> m Connection
waitPostgreSqlVerbose :: (String -> m ()) -> RetryPolicyM m -> ConnectInfo -> m Connection
waitPostgreSqlVerbose String -> m ()
out =
forall e (m :: * -> *).
(MonadIO m, MonadMask m, Exception e) =>
(Bool -> e -> RetryStatus -> m ())
-> RetryPolicyM m -> ConnectInfo -> m Connection
forall (m :: * -> *).
(MonadIO m, MonadMask m, Exception SomeException) =>
(Bool -> SomeException -> RetryStatus -> m ())
-> RetryPolicyM m -> ConnectInfo -> m Connection
waitPostgreSqlVerboseFormat @SomeException ((Bool -> SomeException -> RetryStatus -> m ())
-> RetryPolicyM m -> ConnectInfo -> m Connection)
-> (Bool -> SomeException -> RetryStatus -> m ())
-> RetryPolicyM m
-> ConnectInfo
-> m Connection
forall a b. (a -> b) -> a -> b
$
\Bool
b SomeException
ex RetryStatus
st -> String -> m ()
out (String -> m ()) -> String -> m ()
forall a b. (a -> b) -> a -> b
$ Bool -> SomeException -> RetryStatus -> String
forall e. Exception e => Bool -> e -> RetryStatus -> String
defaultLogMsg Bool
b SomeException
ex RetryStatus
st
waitPostgreSqlVerboseFormat
:: forall e m . (MonadIO m, MonadMask m, Exception e)
=> (Bool -> e -> RetryStatus -> m ())
-> RetryPolicyM m
-> ConnectInfo
-> m Connection
waitPostgreSqlVerboseFormat :: (Bool -> e -> RetryStatus -> m ())
-> RetryPolicyM m -> ConnectInfo -> m Connection
waitPostgreSqlVerboseFormat Bool -> e -> RetryStatus -> m ()
out = [RetryStatus -> Handler m Bool]
-> RetryPolicyM m -> ConnectInfo -> m Connection
forall (m :: * -> *).
(MonadIO m, MonadMask m) =>
[RetryStatus -> Handler m Bool]
-> RetryPolicyM m -> ConnectInfo -> m Connection
waitPostgreSqlWith [RetryStatus -> Handler m Bool
h]
where h :: RetryStatus -> Handler m Bool
h = (e -> m Bool)
-> (Bool -> e -> RetryStatus -> m ())
-> RetryStatus
-> Handler m Bool
forall (m :: * -> *) e.
(Monad m, Exception e) =>
(e -> m Bool)
-> (Bool -> e -> RetryStatus -> m ())
-> RetryStatus
-> Handler m Bool
logRetries (m Bool -> e -> m Bool
forall a b. a -> b -> a
const (m Bool -> e -> m Bool) -> m Bool -> e -> m Bool
forall a b. (a -> b) -> a -> b
$ Bool -> m Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True) Bool -> e -> RetryStatus -> m ()
out
waitPostgreSqlWith
:: (MonadIO m, MonadMask m)
=> [RetryStatus -> Handler m Bool] -> RetryPolicyM m -> ConnectInfo
-> m Connection
waitPostgreSqlWith :: [RetryStatus -> Handler m Bool]
-> RetryPolicyM m -> ConnectInfo -> m Connection
waitPostgreSqlWith [RetryStatus -> Handler m Bool]
hs RetryPolicyM m
policy ConnectInfo
info =
[RetryStatus -> Handler m Bool]
-> RetryPolicyM m -> m Connection -> m Connection
forall (m :: * -> *) a.
(MonadIO m, MonadMask m) =>
[RetryStatus -> Handler m Bool] -> RetryPolicyM m -> m a -> m a
recoveringWith [RetryStatus -> Handler m Bool]
hs RetryPolicyM m
policy (m Connection -> m Connection) -> m Connection -> m Connection
forall a b. (a -> b) -> a -> b
$
IO Connection -> m Connection
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Connection -> m Connection) -> IO Connection -> m Connection
forall a b. (a -> b) -> a -> b
$
IO Connection
-> (Connection -> IO ())
-> (Connection -> IO Connection)
-> IO Connection
forall (m :: * -> *) a c b.
MonadMask m =>
m a -> (a -> m c) -> (a -> m b) -> m b
bracket (ConnectInfo -> IO Connection
connect ConnectInfo
info) Connection -> IO ()
close ((Connection -> IO Connection) -> IO Connection)
-> (Connection -> IO Connection) -> IO Connection
forall a b. (a -> b) -> a -> b
$ \Connection
con -> do
[[Int]]
rs <- Connection -> Query -> IO [[Int]]
forall r. FromRow r => Connection -> Query -> IO [r]
query_ @[Int] Connection
con Query
"SELECT 1;"
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([[Int]]
rs [[Int]] -> [[Int]] -> Bool
forall a. Eq a => a -> a -> Bool
== [[Int
1]]) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ SqlError -> IO ()
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM (SqlError -> IO ()) -> SqlError -> IO ()
forall a b. (a -> b) -> a -> b
$
ByteString -> SqlError
fatalError ByteString
"Unexpected result for SELECT 1."
Connection -> IO Connection
forall (f :: * -> *) a. Applicative f => a -> f a
pure Connection
con