Commit f88ffb37 authored by Alfredo Di Napoli's avatar Alfredo Di Napoli

Add logging capabilities to a DbTx monad

parent 616f2982
...@@ -146,11 +146,12 @@ postAsyncJSON l ngramsList jobHandle = do ...@@ -146,11 +146,12 @@ postAsyncJSON l ngramsList jobHandle = do
markProgress 1 jobHandle markProgress 1 jobHandle
corpus_node <- runDBQuery $ getNode l -- (Proxy :: Proxy HyperdataList) runDBTx $ do
let corpus_id = fromMaybe (panicTrace "no parent_id") (_node_parent_id corpus_node) corpus_node <- getNode l -- (Proxy :: Proxy HyperdataList)
$(logLocM) DEBUG "[postAsyncJSON] Executing re-indexing..." let corpus_id = fromMaybe (panicTrace "no parent_id") (_node_parent_id corpus_node)
_ <- runDBTx $ reIndexWith env corpus_id l NgramsTerms (Set.fromList [MapTerm, CandidateTerm]) $(txLogLocM) DEBUG "[postAsyncJSON] Executing re-indexing..."
$(logLocM) DEBUG "[postAsyncJSON] Re-indexing done." _ <- reIndexWith env corpus_id l NgramsTerms (Set.fromList [MapTerm, CandidateTerm])
$(txLogLocM) DEBUG "[postAsyncJSON] Re-indexing done."
markComplete jobHandle markComplete jobHandle
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
{-# LANGUAGE LambdaCase #-} {-# LANGUAGE LambdaCase #-}
{-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-} {-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TemplateHaskell #-}
{--| This module exposes a custom monad and functions to model database operations within Gargantext. {--| This module exposes a custom monad and functions to model database operations within Gargantext.
The peculiarity of the custom monad is that it describe a DSL for the operations we wish to perform, The peculiarity of the custom monad is that it describe a DSL for the operations we wish to perform,
...@@ -33,6 +34,9 @@ module Gargantext.Database.Transactional ( ...@@ -33,6 +34,9 @@ module Gargantext.Database.Transactional (
, mkOpaInsert , mkOpaInsert
, mkOpaDelete , mkOpaDelete
-- * Emitting log messages
, txLogLocM
-- * Throwing and catching errors (which allows rollbacks) -- * Throwing and catching errors (which allows rollbacks)
, dbFail , dbFail
, catchDBTxError , catchDBTxError
...@@ -49,12 +53,27 @@ import Control.Monad.Trans.Control (MonadBaseControl, control) ...@@ -49,12 +53,27 @@ import Control.Monad.Trans.Control (MonadBaseControl, control)
import Data.Int (Int64) import Data.Int (Int64)
import Data.Pool (withResource, Pool) import Data.Pool (withResource, Pool)
import Data.Profunctor.Product.Default import Data.Profunctor.Product.Default
import Data.Text qualified as T
import Database.PostgreSQL.Simple qualified as PG import Database.PostgreSQL.Simple qualified as PG
import Database.PostgreSQL.Simple.Transaction qualified as PG import Database.PostgreSQL.Simple.Transaction qualified as PG
import Gargantext.Database.Class import Gargantext.Database.Class
import Gargantext.System.Logging (LogLevel, getLocTH, formatWithLoc, getLogger, logTxt, MonadLogger)
import Language.Haskell.TH
import Opaleye import Opaleye
import Prelude import Prelude
data LogMessage =
LogMessage
{ _lm_severity :: LogLevel
, _lm_msg :: T.Text
}
txLogLocM :: ExpQ
txLogLocM = [| \level msg ->
let loc = $(getLocTH)
in DBTx . liftF $ DBLogMessage (LogMessage level (formatWithLoc loc msg)) id
|]
data DBTxException err data DBTxException err
= RollbackRequested err = RollbackRequested err
deriving (Show, Eq) deriving (Show, Eq)
...@@ -127,6 +146,9 @@ data DBTransactionOp err (r :: DBOperation) next where ...@@ -127,6 +146,9 @@ data DBTransactionOp err (r :: DBOperation) next where
OpaDelete :: Delete a -> (a -> next) -> DBTransactionOp err DBWrite next OpaDelete :: Delete a -> (a -> next) -> DBTransactionOp err DBWrite next
-- | Monadic failure for DB transactions. -- | Monadic failure for DB transactions.
DBFail :: err -> DBTransactionOp err r next DBFail :: err -> DBTransactionOp err r next
-- | Emits a log message. Log messages are collected in a pure setting and emitted while interpreting
-- the monad.
DBLogMessage :: LogMessage -> (() -> next) -> DBTransactionOp err r next
-- | A 'DBTx' is a free monad (using the free church-encoding 'F') using 'DBTransactionOp' as the functor. -- | A 'DBTx' is a free monad (using the free church-encoding 'F') using 'DBTransactionOp' as the functor.
-- In practical terms, it's just a monad where we can execute just the operations described by the -- In practical terms, it's just a monad where we can execute just the operations described by the
...@@ -141,7 +163,13 @@ type DBReadOnly err r a = DBTx err DBRead a ...@@ -141,7 +163,13 @@ type DBReadOnly err r a = DBTx err DBRead a
-- Strict constraints to perform transactional read and writes. -- Strict constraints to perform transactional read and writes.
-- Isomorphic to a DBCmd, but it doesn't impose a 'HasConfig' constraint, as -- Isomorphic to a DBCmd, but it doesn't impose a 'HasConfig' constraint, as
-- values can always be passed as parameters of a query or update. -- values can always be passed as parameters of a query or update.
type DBTxCmd err a = forall m env. (IsCmd env err m, HasConnectionPool env, Safe.MonadCatch m) => m a type DBTxCmd err a =
forall m env. (
IsCmd env err m
, HasConnectionPool env
, Safe.MonadCatch m
, MonadLogger m
) => m a
instance Functor (DBTransactionOp err r) where instance Functor (DBTransactionOp err r) where
fmap f = \case fmap f = \case
...@@ -155,6 +183,7 @@ instance Functor (DBTransactionOp err r) where ...@@ -155,6 +183,7 @@ instance Functor (DBTransactionOp err r) where
OpaUpdate upd cont -> OpaUpdate upd (f . cont) OpaUpdate upd cont -> OpaUpdate upd (f . cont)
OpaDelete del cont -> OpaDelete del (f . cont) OpaDelete del cont -> OpaDelete del (f . cont)
DBFail err -> DBFail err DBFail err -> DBFail err
DBLogMessage msg cont -> DBLogMessage msg (f . cont)
-- | Generalised version of 'withResource' to work over any unlifted monad. -- | Generalised version of 'withResource' to work over any unlifted monad.
-- For some reason 'resource-pool' removed this from version 0.3.0.0 onwards. -- For some reason 'resource-pool' removed this from version 0.3.0.0 onwards.
...@@ -229,6 +258,9 @@ evalOp conn = \case ...@@ -229,6 +258,9 @@ evalOp conn = \case
OpaUpdate upd cc -> cc <$> liftBase (runUpdate conn upd) OpaUpdate upd cc -> cc <$> liftBase (runUpdate conn upd)
OpaDelete del cc -> cc <$> liftBase (runDelete conn del) OpaDelete del cc -> cc <$> liftBase (runDelete conn del)
DBFail err -> liftBase (Safe.throwIO $ RollbackRequested err) DBFail err -> liftBase (Safe.throwIO $ RollbackRequested err)
DBLogMessage (LogMessage sev msg) cc -> cc <$> do
lgr <- getLogger
logTxt lgr sev msg
evalOpaCountQuery :: PG.Connection -> Select a -> IO Int evalOpaCountQuery :: PG.Connection -> Select a -> IO Int
evalOpaCountQuery conn sel = do evalOpaCountQuery conn sel = do
......
...@@ -10,6 +10,10 @@ module Gargantext.System.Logging ( ...@@ -10,6 +10,10 @@ module Gargantext.System.Logging (
, logLoc , logLoc
, withLogger , withLogger
, withLoggerIO , withLoggerIO
-- * Internals
, getLocTH
, formatWithLoc
) where ) where
import Gargantext.System.Logging.Types import Gargantext.System.Logging.Types
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment