daml/bazel_tools/client_server/runner/Main.hs
Jussi Mäki ca7bbacd6c Build rules for producing and verifying ledger dumps (#3290)
* Add client_server_build and integrity_test rules

And use them to implement ledger dump of the reference
server and to check it.

* Only build and test ledger dump on Linux. Only run tests relevant to dump.

* Make client_server_build quiet in happy path

* Reformat

* Remove unnecessary runfiles for client_server_build
2019-11-07 09:41:54 +00:00

45 lines
1.3 KiB
Haskell

-- Copyright (c) 2019 The DAML Authors. All rights reserved.
-- SPDX-License-Identifier: Apache-2.0
module Main(main) where
import Control.Concurrent (threadDelay)
import qualified Data.Text.IO as T
import System.Environment
import System.Process
import System.IO
import System.IO.Extra (withTempFile)
import System.Exit
import Safe
import Data.List.Split (splitOn)
retryDelayMillis :: Int
retryDelayMillis = 100
-- Wait up to 60s for the port file to be written to. A long timeout is used to
-- avoid flaky results under high system load.
maxRetries :: Int
maxRetries = 60 * (1000 `div` retryDelayMillis)
readPortFile :: Int -> String -> IO Int
readPortFile 0 _file = do
T.hPutStrLn stderr "Port file was not written to in time."
exitFailure
readPortFile n file =
readMay <$> readFile file >>= \case
Nothing -> do
threadDelay (1000 * retryDelayMillis)
readPortFile (n-1) file
Just p -> pure p
main :: IO ()
main = do
[clientExe, clientArgs, serverExe, serverArgs] <- getArgs
withTempFile $ \tempFile -> do
let splitArgs = filter (/= "") . splitOn " "
let serverProc = proc serverExe $ ["--port-file", tempFile] <> splitArgs serverArgs
withCreateProcess serverProc $ \_stdin _stdout _stderr _ph -> do
port <- readPortFile maxRetries tempFile
callProcess clientExe (["--target-port", show port] <> splitArgs clientArgs)