Skip to content

Commit b3ed1bc

Browse files
committed
Caputure stderr of mithril-client and wait for bytes_downloaded
They slightly changed the messages produced by mithril-client.
1 parent 316cba0 commit b3ed1bc

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

hydra-cluster/src/Hydra/Cluster/Mithril.hs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import Data.ByteString qualified as BS
1010
import Hydra.Cluster.Fixture (KnownNetwork (..))
1111
import Network.HTTP.Simple (getResponseBody, httpBS, parseRequest)
1212
import System.IO.Error (isEOFError)
13-
import System.Process.Typed (createPipe, getStdout, proc, setStdout, withProcessWait_)
13+
import System.Process.Typed (createPipe, getStderr, proc, setStderr, withProcessWait_)
1414

1515
data MithrilLog
1616
= StartSnapshotDownload {network :: KnownNetwork, directory :: FilePath}
17-
| -- | Output captured directly from mithril-client
18-
StdOut {output :: Value}
17+
| -- | Output captured directly from mithril-client stderr.
18+
StdErr {output :: Value}
1919
deriving stock (Eq, Show, Generic)
2020
deriving anyclass (ToJSON, FromJSON)
2121

@@ -28,7 +28,7 @@ downloadLatestSnapshotTo tracer network directory = do
2828
traceWith tracer StartSnapshotDownload{network, directory}
2929
genesisKey <- parseRequest genesisKeyURL >>= httpBS <&> getResponseBody
3030
let cmd =
31-
setStdout createPipe $
31+
setStderr createPipe $
3232
proc "mithril-client" $
3333
concat
3434
[ ["--aggregator-endpoint", aggregatorEndpoint]
@@ -37,15 +37,15 @@ downloadLatestSnapshotTo tracer network directory = do
3737
, ["--download-dir", directory]
3838
, ["--json"]
3939
]
40-
withProcessWait_ cmd traceStdout
40+
withProcessWait_ cmd traceStderr
4141
_ -> error $ "Network " <> show network <> " not supported by mithril."
4242
where
43-
traceStdout p =
43+
traceStderr p =
4444
ignoreEOFErrors . forever $ do
45-
bytes <- BS.hGetLine (getStdout p)
45+
bytes <- BS.hGetLine (getStderr p)
4646
case Aeson.eitherDecodeStrict bytes of
4747
Left err -> error $ "failed to decode: \n" <> show bytes <> "\nerror: " <> show err
48-
Right output -> traceWith tracer StdOut{output}
48+
Right output -> traceWith tracer StdErr{output}
4949

5050
ignoreEOFErrors =
5151
handleJust (guard . isEOFError) (const $ pure ())

hydra-cluster/test/Test/Hydra/Cluster/MithrilSpec.hs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import Hydra.Prelude
44
import Test.Hydra.Prelude
55

66
import Control.Concurrent.Class.MonadSTM (newTVarIO, readTVarIO)
7-
import Control.Lens ((^?!))
8-
import Data.Aeson.Lens (key, _Number)
7+
import Control.Lens ((^?))
8+
import Data.Aeson.Lens (key)
99
import Hydra.Cluster.Mithril (MithrilLog (..), downloadLatestSnapshotTo)
1010
import Hydra.Logging (Envelope (..), Tracer, traceInTVar)
1111
import System.Directory (doesDirectoryExist)
@@ -22,19 +22,19 @@ spec = parallel $ do
2222
doesDirectoryExist dbPath `shouldReturn` False
2323
race_
2424
(downloadLatestSnapshotTo tracer network tmpDir)
25-
(waitForStep 3 getTraces)
25+
(waitForDownload getTraces)
2626

27-
-- | Wait for the 'StdOut' message that matches the given step number.
28-
waitForStep :: HasCallStack => Natural -> IO [Envelope MithrilLog] -> IO ()
29-
waitForStep step getTraces = do
27+
-- | Wait for the 'StdErr' message to indicate it starts downloading.
28+
waitForDownload :: HasCallStack => IO [Envelope MithrilLog] -> IO ()
29+
waitForDownload getTraces = do
3030
traces <- getTraces
31-
unless (any isRightStep traces) $ do
31+
unless (any isRightTrace traces) $ do
3232
threadDelay 1
33-
waitForStep step getTraces
33+
waitForDownload getTraces
3434
where
35-
isRightStep = \case
36-
Envelope{message = StdOut{output}} ->
37-
output ^?! key "step_num" . _Number == fromIntegral step
35+
isRightTrace = \case
36+
Envelope{message = StdErr{output}} ->
37+
isJust $ output ^? key "bytes_downloaded"
3838
_ -> False
3939

4040
-- | Create a tracer that captures all messages and a function to retrieve all

0 commit comments

Comments
 (0)