Skip to content

Commit

Permalink
Use exceptions to signal ProcessExited
Browse files Browse the repository at this point in the history
  • Loading branch information
paolino committed May 3, 2024
1 parent 8cedd79 commit d267683
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 19 deletions.
12 changes: 9 additions & 3 deletions lib/benchmarks/src/Cardano/Wallet/BenchShared.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE PackageImports #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}

-- |
Expand Down Expand Up @@ -46,6 +47,9 @@ import Cardano.BM.Trace
( Trace
, nullTracer
)
import Cardano.Launcher
( ProcessHasExited (..)
)
import Cardano.Launcher.Node
( CardanoNodeConfig (..)
, CardanoNodeConn
Expand Down Expand Up @@ -139,6 +143,7 @@ import UnliftIO.Concurrent
)
import UnliftIO.Exception
( evaluate
, try
)
import UnliftIO.Temporary
( withSystemTempDirectory
Expand Down Expand Up @@ -169,12 +174,13 @@ execBenchWithNode networkConfig action = withNoBuffering $ do
action tr (networkConfig args) conn
pure ExitSuccess
Nothing -> do
res <- withNetworkConfiguration args $ \nodeConfig ->
res <- try $ withNetworkConfiguration args $ \nodeConfig ->
withCardanoNode (trMessageText tr) nodeConfig $
action tr (networkConfig args)
case res of
Left exited -> do
sayErr $ "FAIL: cardano-node exited with status " <> toText exited
Left (exited :: ProcessHasExited) -> do
sayErr $ "FAIL: cardano-node exited with status "
<> toText exited
pure $ ExitFailure 1
Right _ -> pure ExitSuccess

Expand Down
7 changes: 4 additions & 3 deletions lib/launcher/src/Cardano/Launcher.hs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ import UnliftIO.Exception
, bracket_
, finally
, onException
, throwIO
, tryJust
)
import UnliftIO.MVar
Expand Down Expand Up @@ -206,7 +207,7 @@ withBackendProcess
-- ^ 'Command' description
-> (ProcessHandles -> m a)
-- ^ Action to execute while process is running.
-> m (Either ProcessHasExited a)
-> m a
withBackendProcess tr (Command name args before std_in std_out) action =
liftIO before >> withBackendCreateProcess tr process action
where
Expand Down Expand Up @@ -239,7 +240,7 @@ withBackendCreateProcess
-- ^ 'Command' description
-> (ProcessHandles -> m a)
-- ^ Action to execute while process is running.
-> m (Either ProcessHasExited a)
-> m a
withBackendCreateProcess tr process action = do
traceWith tr $ MsgLauncherStart name args
exitVar <- newEmptyMVar
Expand All @@ -258,7 +259,7 @@ withBackendCreateProcess tr process action = do
(action $ ProcessHandles mstdin mstdout mstderr ph)

traceWith tr $ MsgLauncherFinish (leftToMaybe res)
pure res
either throwIO pure res
where
-- Exceptions resulting from the @exec@ call for this command. The most
-- likely exception is that the command does not exist. We don't want to
Expand Down
3 changes: 1 addition & 2 deletions lib/launcher/src/Cardano/Launcher/Node.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import Prelude

import Cardano.Launcher
( LauncherLog
, ProcessHasExited
, StdStream (..)
, withBackendCreateProcess
)
Expand Down Expand Up @@ -134,7 +133,7 @@ withCardanoNode
-> CardanoNodeConfig
-> (CardanoNodeConn -> IO a)
-- ^ Callback function with a socket filename and genesis params
-> IO (Either ProcessHasExited a)
-> IO a
withCardanoNode tr cfg action = do
let socketPath = nodeSocketPath (nodeDir cfg)
let run output = do
Expand Down
3 changes: 1 addition & 2 deletions lib/launcher/src/Cardano/Launcher/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import Prelude

import Cardano.Launcher
( LauncherLog
, ProcessHasExited
, withBackendCreateProcess
)
import Cardano.Launcher.Node
Expand Down Expand Up @@ -91,7 +90,7 @@ withCardanoWallet
-> CardanoWalletConfig
-> (CardanoWalletConn -> IO a)
-- ^ Callback function with a socket filename and genesis params
-> IO (Either ProcessHasExited a)
-> IO a
withCardanoWallet tr node cfg@CardanoWalletConfig{..} action =
withBackendCreateProcess tr (cardanoWallet cfg node)
$ \_ -> action $ CardanoWalletConn walletPort
Expand Down
3 changes: 2 additions & 1 deletion lib/launcher/test/unit/Cardano/LauncherSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ import UnliftIO.Concurrent
)
import UnliftIO.Exception
( bracket
, try
)
import UnliftIO.MVar
( modifyMVar_
Expand Down Expand Up @@ -277,7 +278,7 @@ launch tr cmds = do
waitForOthers (ProcessHandles _ _ _ ph) = do
modifyMVar_ phsVar (pure . (ph:))
forever $ threadDelay maxBound
start = async . flip (withBackendProcess tr) waitForOthers
start = async . try . flip (withBackendProcess tr) waitForOthers

mapM start cmds >>= waitAnyCancel >>= \case
(_, Left e) -> do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ import Cardano.Wallet.Launch.Cluster.Config
import Cardano.Wallet.Launch.Cluster.Logging
( ClusterLog (MsgLauncher)
)
import Control.Exception
( throwIO
)
import Control.Monad.Reader
( MonadIO (..)
, MonadReader (..)
Expand All @@ -35,6 +32,4 @@ withCardanoNodeProcess
-> ClusterM a
withCardanoNodeProcess name cfg f = do
Config{..} <- ask
liftIO $ do
r <- withCardanoNode (contramap (MsgLauncher name) cfgTracer) cfg f
either throwIO pure r
liftIO $ withCardanoNode (contramap (MsgLauncher name) cfgTracer) cfg f
4 changes: 2 additions & 2 deletions lib/wallet-benchmarks/bench/memory-benchmark.hs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ withCardanoWallet
-> BenchmarkConfig
-> C.CardanoNodeConn
-> (C.CardanoWalletConn -> IO r)
-> IO (Either C.ProcessHasExited r)
-> IO r
withCardanoWallet tr workingDir walletExe BenchmarkConfig{..} node action = do
C.withCardanoWallet tr node
C.CardanoWalletConfig
Expand All @@ -252,7 +252,7 @@ withCardanoNode
-> FilePath
-> BenchmarkConfig
-> (C.CardanoNodeConn -> IO r)
-> IO (Either C.ProcessHasExited r)
-> IO r
withCardanoNode tr nodeExe BenchmarkConfig{..} =
C.withCardanoNode tr
C.CardanoNodeConfig
Expand Down

0 comments on commit d267683

Please sign in to comment.