Skip to content

Commit

Permalink
Add healthcheck to marlowe-tx
Browse files Browse the repository at this point in the history
  • Loading branch information
jhbertra committed Mar 16, 2023
1 parent 6b8a8e6 commit 49c9028
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
11 changes: 11 additions & 0 deletions marlowe-runtime/marlowe-tx/Main.hs
Expand Up @@ -90,6 +90,7 @@ run = runComponent_ proc Options{..} -> do
, loadWalletContext = Query.loadWalletContext $ queryChainSync . GetUTxOs
, eventBackend = narrowEventBackend (injectSelector App) eventBackend
, getCurrentScripts = ScriptRegistry.getCurrentScripts
, httpPort = fromIntegral httpPort
, ..
}

Expand All @@ -102,6 +103,7 @@ data Options = Options
, host :: HostName
, logConfigFile :: Maybe FilePath
, submitConfirmationBlocks :: BlockNo
, httpPort :: PortNumber
}

getOptions :: IO Options
Expand All @@ -116,6 +118,7 @@ getOptions = execParser $ info (helper <*> parser) infoMod
<*> hostParser
<*> logConfigFileParser
<*> submitConfirmationBlocksParser
<*> httpPortParser

chainSeekPortParser = option auto $ mconcat
[ long "chain-sync-port"
Expand Down Expand Up @@ -172,6 +175,14 @@ getOptions = execParser $ info (helper <*> parser) infoMod
, help "The logging configuration JSON file."
]

httpPortParser = option auto $ mconcat
[ long "http-port"
, metavar "PORT_NUMBER"
, help "Port number to serve the http healthcheck API on"
, value 8080
, showDefault
]

submitConfirmationBlocksParser = option (BlockNo <$> auto) $ mconcat
[ long "submit-confirmation-blocks"
, value 0
Expand Down
14 changes: 12 additions & 2 deletions marlowe-runtime/tx/Language/Marlowe/Runtime/Transaction.hs
Expand Up @@ -9,7 +9,8 @@ import Cardano.Api (Tx)
import qualified Cardano.Api as C
import Cardano.Api.Byron (BabbageEra)
import Control.Concurrent.Component
import Control.Concurrent.STM (STM)
import Control.Concurrent.Component.Probes
import Control.Concurrent.STM (STM, atomically)
import Data.Text (Text)
import Data.Void
import Language.Marlowe.Runtime.ChainSync.Api (ChainSyncQuery, RuntimeChainSeekClient)
Expand Down Expand Up @@ -43,12 +44,21 @@ data TransactionDependencies r = TransactionDependencies
, queryChainSync :: forall e a. ChainSyncQuery Void e a -> IO a
, eventBackend :: EventBackend IO r TransactionServerSelector
, getCurrentScripts :: forall v. MarloweVersion v -> MarloweScripts
, httpPort :: Int
}

transaction :: Component IO (TransactionDependencies r) ()
transaction = proc TransactionDependencies{..} -> do
getTip <- transactionChainClient -< TransactionChainClientDependencies{..}
(connected, getTip) <- transactionChainClient -< TransactionChainClientDependencies{..}
transactionServer -< TransactionServerDependencies{..}
probeServer -< ProbeServerDependencies
{ probes = Probes
{ startup = pure True
, liveness = atomically connected
, readiness = atomically connected
}
, port = httpPort
}

getTransactionSererSelectorConfig :: GetSelectorConfig TransactionServerSelector
getTransactionSererSelectorConfig = \case
Expand Down
15 changes: 12 additions & 3 deletions marlowe-runtime/tx/Language/Marlowe/Runtime/Transaction/Chain.hs
Expand Up @@ -6,6 +6,7 @@ module Language.Marlowe.Runtime.Transaction.Chain
import Control.Concurrent (threadDelay)
import Control.Concurrent.Component
import Control.Concurrent.STM (STM, atomically, newTVar, readTVar, writeTVar)
import Control.Exception (finally)
import Data.Functor (($>))
import Data.Void (absurd)
import Language.Marlowe.Runtime.ChainSync.Api (Move(..), RuntimeChainSeekClient)
Expand All @@ -18,12 +19,20 @@ newtype TransactionChainClientDependencies = TransactionChainClientDependencies
{ chainSyncConnector :: SomeClientConnector RuntimeChainSeekClient IO
}

transactionChainClient :: Component IO TransactionChainClientDependencies (STM Chain.ChainPoint)
transactionChainClient :: Component IO TransactionChainClientDependencies (STM Bool, STM Chain.ChainPoint)
transactionChainClient = component \TransactionChainClientDependencies{..} -> do
tipVar <- newTVar Chain.Genesis
pure (runSomeConnector chainSyncConnector $ client tipVar, readTVar tipVar)
connectedVar <- newTVar False
pure
( flip finally (atomically $ writeTVar connectedVar False)
$ runSomeConnector chainSyncConnector
$ client connectedVar tipVar
, (readTVar connectedVar, readTVar tipVar)
)
where
client tipVar = ChainSeekClient $ pure clientIdle
client connectedVar tipVar = ChainSeekClient do
atomically $ writeTVar connectedVar True
pure clientIdle
where
clientIdle = SendMsgQueryNext AdvanceToTip clientNext
clientNext = ClientStNext
Expand Down

0 comments on commit 49c9028

Please sign in to comment.