-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Review launcher code, folder organization and API options
- allow launcher's command to take a 'setup' action before they're ran This allows for the wallet backend to wait a bit before it starts. Or, any kind of startup action we may think of, like generating TLS certs, etc ... - review folder organization and tests scripts - add Buildable intance to 'Command' and use that to format the initial info - use --http-bridge-port instead of --node-port
- Loading branch information
Showing
17 changed files
with
294 additions
and
302 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
- package: | ||
- name: cardano-wallet | ||
- section: | ||
- name: exe:cardano-wallet-launcher exe:cardano-wallet-server | ||
- message: | ||
- name: Module reused between components | ||
- module: Cardano.CLI | ||
- section: | ||
- name: exe:cardano-wallet-launcher test:unit | ||
- message: | ||
- name: Module reused between components | ||
- module: Cardano.Launcher |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
-- | | ||
-- Copyright: © 2018-2019 IOHK | ||
-- License: MIT | ||
-- | ||
-- This module contains a mechanism for launching external processes together, | ||
-- and provides the functionality needed to kill them all if one goes down. | ||
-- (would be achieved using @monitor@ and @kill@ in combination) | ||
|
||
module Cardano.Launcher | ||
( Command (..) | ||
, ProcessHasExited(..) | ||
, launch | ||
) where | ||
|
||
import Prelude | ||
|
||
import Control.Concurrent.Async | ||
( forConcurrently ) | ||
import Control.Exception | ||
( Exception, throwIO, try ) | ||
import Data.List | ||
( isPrefixOf ) | ||
import Fmt | ||
( Buildable (..), blockListF', indentF ) | ||
import System.Exit | ||
( ExitCode ) | ||
import System.Process | ||
( proc, waitForProcess, withCreateProcess ) | ||
|
||
|
||
data Command = Command | ||
{ cmdName :: String | ||
, cmdArgs :: [String] | ||
, cmdSetup :: IO () | ||
-- ^ An extra action to run _before_ the command | ||
} | ||
|
||
-- Format a command nicely with one argument / option per line. | ||
-- | ||
-- e.g. | ||
-- | ||
-- >>> fmt $ build $ Command "cardano-wallet-server" ["--port", "8080", "--network", "mainnet"] (return ()) | ||
-- cardano-wallet-server | ||
-- --port 8080 | ||
-- --network mainnet | ||
instance Buildable Command where | ||
build (Command name args _) = build name | ||
<> "\n" | ||
<> indentF 4 (blockListF' "" build $ snd $ foldl buildOptions ("", []) args) | ||
where | ||
buildOptions :: (String, [String]) -> String -> (String, [String]) | ||
buildOptions ("", grp) arg = | ||
(arg, grp) | ||
buildOptions (partial, grp) arg = | ||
if ("--" `isPrefixOf` partial) && not ("--" `isPrefixOf` arg) then | ||
("", grp ++ [partial <> " " <> arg]) | ||
else | ||
(arg, grp ++ [partial]) | ||
|
||
-- | ProcessHasExited is used by a monitoring thread to signal that the process | ||
-- has exited. | ||
data ProcessHasExited = ProcessHasExited String ExitCode | ||
deriving Show | ||
|
||
instance Exception ProcessHasExited | ||
|
||
launch :: [Command] -> IO ProcessHasExited | ||
launch cmds = do | ||
res <- try $ forConcurrently cmds $ \(Command name args before) -> do | ||
before | ||
withCreateProcess (proc name args) $ \_ _ _ h -> do | ||
code <- waitForProcess h | ||
throwIO $ ProcessHasExited name code | ||
case res of | ||
Left e -> return e | ||
Right _ -> error | ||
"Unreachable. Supervising threads should never finish. \ | ||
\They should stay running or throw @ProcessHasExited@." |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.