2019-08-13 19:23:03 +03:00
|
|
|
-- Copyright (c) 2019 The DAML Authors. All rights reserved.
|
2019-04-11 18:31:54 +03:00
|
|
|
-- SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2019-05-02 11:12:31 +03:00
|
|
|
module Main(main) where
|
2019-04-11 18:31:54 +03:00
|
|
|
|
2019-07-03 10:59:29 +03:00
|
|
|
import Control.Concurrent (threadDelay)
|
2019-04-11 18:31:54 +03:00
|
|
|
import qualified Data.Text.IO as T
|
|
|
|
import System.Environment
|
2019-07-03 10:59:29 +03:00
|
|
|
import System.Process
|
|
|
|
import System.IO
|
|
|
|
import System.IO.Extra (withTempFile)
|
2019-04-11 18:31:54 +03:00
|
|
|
import System.Exit
|
|
|
|
import Safe
|
|
|
|
import Data.List.Split (splitOn)
|
|
|
|
|
2019-04-17 11:35:47 +03:00
|
|
|
retryDelayMillis :: Int
|
|
|
|
retryDelayMillis = 100
|
2019-04-11 18:31:54 +03:00
|
|
|
|
2019-04-17 11:35:47 +03:00
|
|
|
-- 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)
|
2019-04-11 18:31:54 +03:00
|
|
|
|
|
|
|
readPortFile :: Int -> String -> IO Int
|
2019-07-03 10:59:29 +03:00
|
|
|
readPortFile 0 _file = do
|
2019-04-11 18:31:54 +03:00
|
|
|
T.hPutStrLn stderr "Port file was not written to in time."
|
|
|
|
exitFailure
|
|
|
|
|
|
|
|
readPortFile n file =
|
|
|
|
readMay <$> readFile file >>= \case
|
|
|
|
Nothing -> do
|
2019-04-17 11:35:47 +03:00
|
|
|
threadDelay (1000 * retryDelayMillis)
|
2019-04-11 18:31:54 +03:00
|
|
|
readPortFile (n-1) file
|
|
|
|
Just p -> pure p
|
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = do
|
|
|
|
[clientExe, clientArgs, serverExe, serverArgs] <- getArgs
|
2019-07-03 10:59:29 +03:00
|
|
|
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)
|