@@ -176,13 +176,14 @@ data Loggers
176176 , _lsPgLogger :: ! Q. PGLogger
177177 }
178178
179- newtype ServerAppM a = ServerAppM { unServerAppM :: ReaderT Q. PGPool IO a }
179+ -- | An application with Postgres database as a metadata storage
180+ newtype PGMetadataStorageApp a
181+ = PGMetadataStorageApp { runPGMetadataStorageApp :: Q. PGPool -> IO a }
180182 deriving ( Functor , Applicative , Monad
181183 , MonadIO , MonadBase IO , MonadBaseControl IO
182184 , MonadCatch , MonadThrow , MonadMask
183- , MonadReader Q.PGPool
184- , MonadUnique
185- )
185+ , MonadUnique , MonadReader Q.PGPool
186+ ) via (ReaderT Q. PGPool IO )
186187
187188-- | this function initializes the catalog and returns an @InitCtx@, based on the command given
188189-- - for serve command it creates a proper PG connection pool
@@ -629,12 +630,12 @@ execQuery env queryBs = do
629630 buildSchemaCacheStrict
630631 encJToLBS <$> runQueryM env query
631632
632- instance Tracing. HasReporter ServerAppM
633+ instance Tracing. HasReporter PGMetadataStorageApp
633634
634- instance MonadQueryInstrumentation ServerAppM where
635+ instance MonadQueryInstrumentation PGMetadataStorageApp where
635636 askInstrumentQuery _ = pure (id , noProfile)
636637
637- instance HttpLog ServerAppM where
638+ instance HttpLog PGMetadataStorageApp where
638639 logHttpError logger userInfoM reqId waiReq req qErr headers =
639640 unLogger logger $ mkHttpLog $
640641 mkHttpErrorLogContext userInfoM reqId waiReq req qErr Nothing Nothing headers
@@ -643,57 +644,58 @@ instance HttpLog ServerAppM where
643644 unLogger logger $ mkHttpLog $
644645 mkHttpAccessLogContext userInfoM reqId waiReq compressedResponse qTime cType headers
645646
646- instance MonadExecuteQuery ServerAppM where
647+ instance MonadExecuteQuery PGMetadataStorageApp where
647648 cacheLookup _ _ = pure ([] , Nothing )
648649 cacheStore _ _ = pure ()
649650
650- instance UserAuthentication (Tracing. TraceT ServerAppM ) where
651+ instance UserAuthentication (Tracing. TraceT PGMetadataStorageApp ) where
651652 resolveUserInfo logger manager headers authMode =
652653 runExceptT $ getUserInfoWithExpTime logger manager headers authMode
653654
654- instance MetadataApiAuthorization ServerAppM where
655+ instance MetadataApiAuthorization PGMetadataStorageApp where
655656 authorizeMetadataApi query userInfo = do
656657 let currRole = _uiRole userInfo
657658 when (requiresAdmin query && currRole /= adminRoleName) $
658659 withPathK " args" $ throw400 AccessDenied errMsg
659660 where
660661 errMsg = " restricted access : admin only"
661662
662- instance ConsoleRenderer ServerAppM where
663+ instance ConsoleRenderer PGMetadataStorageApp where
663664 renderConsole path authMode enableTelemetry consoleAssetsDir =
664665 return $ mkConsoleHTML path authMode enableTelemetry consoleAssetsDir
665666
666- instance MonadGQLExecutionCheck ServerAppM where
667+ instance MonadGQLExecutionCheck PGMetadataStorageApp where
667668 checkGQLExecution userInfo _ enableAL sc query = runExceptT $ do
668669 req <- toParsed query
669670 checkQueryInAllowlist enableAL userInfo req sc
670671 return req
671672
672- instance MonadConfigApiHandler ServerAppM where
673+ instance MonadConfigApiHandler PGMetadataStorageApp where
673674 runConfigApiHandler = configApiGetHandler
674675
675- instance MonadQueryLog ServerAppM where
676+ instance MonadQueryLog PGMetadataStorageApp where
676677 logQueryLog logger query genSqlM reqId =
677678 unLogger logger $ QueryLog query genSqlM reqId
678679
679- instance WS. MonadWSLog ServerAppM where
680+ instance WS. MonadWSLog PGMetadataStorageApp where
680681 logWSLog = unLogger
681682
682- runTxInMetadataStorage :: Q. TxE QErr a -> MetadataStorageT ServerAppM a
683- runTxInMetadataStorage tx = do
684- pool <- lift ask
685- liftEitherM $ liftIO $ runExceptT $
686- Q. runTx pool (Q. RepeatableRead , Just Q. ReadWrite ) tx
687-
688- instance MonadMetadataStorage (MetadataStorageT ServerAppM ) where
689-
690- getDeprivedCronTriggerStats = runTxInMetadataStorage getDeprivedCronTriggerStatsTx
691- getScheduledEventsForDelivery = runTxInMetadataStorage getScheduledEventsForDeliveryTx
692- insertScheduledEvent = runTxInMetadataStorage . insertScheduledEventTx
693- insertScheduledEventInvocation a b = runTxInMetadataStorage $ insertInvocationTx a b
694- setScheduledEventOp a b c = runTxInMetadataStorage $ setScheduledEventOpTx a b c
695- unlockScheduledEvents a b = runTxInMetadataStorage $ unlockScheduledEventsTx a b
696- unlockAllLockedScheduledEvents = runTxInMetadataStorage unlockAllLockedScheduledEventsTx
683+ instance MonadTx (MetadataStorageT PGMetadataStorageApp ) where
684+ liftTx tx = do
685+ pool <- lift ask
686+ -- Each operation in metadata storage is executed in an isolated transaction.
687+ -- No two operations are not necessarily executed together.
688+ liftEitherM $ liftIO $ runExceptT $ Q. runTx pool (Q. RepeatableRead , Nothing ) tx
689+
690+ instance MonadMetadataStorage (MetadataStorageT PGMetadataStorageApp ) where
691+
692+ getDeprivedCronTriggerStats = liftTx getDeprivedCronTriggerStatsTx
693+ getScheduledEventsForDelivery = liftTx getScheduledEventsForDeliveryTx
694+ insertScheduledEvent = liftTx . insertScheduledEventTx
695+ insertScheduledEventInvocation a b = liftTx $ insertInvocationTx a b
696+ setScheduledEventOp a b c = liftTx $ setScheduledEventOpTx a b c
697+ unlockScheduledEvents a b = liftTx $ unlockScheduledEventsTx a b
698+ unlockAllLockedScheduledEvents = liftTx unlockAllLockedScheduledEventsTx
697699
698700--- helper functions ---
699701
0 commit comments