module PgRecorder
( listenSession
, dbNotificationHandler
) where
import PgRecorder.Prelude
import PgRecorder.Config
import PgRecorder.Database
import qualified Hasql.Pool as HP
import qualified Hasql.Connection as H
listenSession :: AppConfig -> (ByteString -> ByteString -> IO ()) -> IO ()
listenSession conf withNotification = do
con <- either (panic . show) id <$> H.acquire (toS $ configDatabase conf)
listen con $ toPgIdentifier . toS $ channel conf
waitForNotifications withNotification con
dbNotificationHandler :: HP.Settings -> Text -> IO (ByteString -> ByteString -> IO ())
dbNotificationHandler poolConfig dispatcher =
dispatchNotificationToDb (toS dispatcher) <$> HP.acquire poolConfig
dispatchNotificationToDb :: ByteString -> HP.Pool -> ByteString -> ByteString -> IO ()
dispatchNotificationToDb dispatcher pool chan notification =
void $ async executeNotification
where
executeNotification = do
r <- callProcedure pool (toPgIdentifier dispatcher) chan notification
case r of
Left e -> print e
_ -> return ()