Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tear down daml services gracefully #12591

Merged
merged 2 commits into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions daml-assistant/daml-helper/src/DA/Daml/Helper/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ data Command
| LedgerFetchDar { flags :: LedgerFlags, pid :: String, saveAs :: FilePath }
| LedgerReset {flags :: LedgerFlags}
| LedgerExport { flags :: LedgerFlags, remainingArguments :: [String] }
| LedgerNavigator { flags :: LedgerFlags, remainingArguments :: [String] }
| LedgerNavigator { flags :: LedgerFlags, remainingArguments :: [String], shutdownStdinClose :: Bool }
| Codegen { lang :: Lang, remainingArguments :: [String] }
| PackagesList {flags :: LedgerFlags}
| LedgerMeteringReport { flags :: LedgerFlags, from :: IsoTime, to :: Maybe IsoTime, application :: Maybe ApplicationId, compactOutput :: Bool }
Expand All @@ -78,6 +78,7 @@ data Command
, portFileM :: Maybe FilePath
, darPaths :: [FilePath]
, remainingArguments :: [String]
, shutdownStdinClose :: Bool
}

data AppTemplate
Expand Down Expand Up @@ -339,6 +340,7 @@ commandParser = subparser $ fold
ledgerNavigatorCmd = LedgerNavigator
<$> ledgerFlags (ShowJsonApi False)
<*> many (argument str (metavar "ARG" <> help "Extra arguments to navigator."))
<*> stdinCloseOpt

app :: ReadM ApplicationId
app = fmap (ApplicationId . pack) str
Expand Down Expand Up @@ -442,6 +444,7 @@ commandParser = subparser $ fold
darPaths <- many $ option str (long "dar" <> metavar "PATH"
<> help "DAR file to upload to sandbox")
remainingArguments <- many (argument str (metavar "ARG"))
shutdownStdinClose <- stdinCloseOpt
pure CantonSandbox {..}

cantonSandboxCmdInfo =
Expand Down Expand Up @@ -483,10 +486,11 @@ runCommand = \case
LedgerFetchDar {..} -> runLedgerFetchDar flags pid saveAs
LedgerReset {..} -> runLedgerReset flags
LedgerExport {..} -> runLedgerExport flags remainingArguments
LedgerNavigator {..} -> runLedgerNavigator flags remainingArguments
LedgerNavigator {..} -> (if shutdownStdinClose then withCloseOnStdin else id) $ runLedgerNavigator flags remainingArguments
Codegen {..} -> runCodegen lang remainingArguments
LedgerMeteringReport {..} -> runLedgerMeteringReport flags from to application compactOutput
CantonSandbox {..} ->
(if shutdownStdinClose then withCloseOnStdin else id) $
withCantonPortFile cantonOptions $ \cantonOptions cantonPortFile ->
withCantonSandbox cantonOptions remainingArguments $ \ph -> do
putStrLn "Starting Canton sandbox."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,21 @@ authorizationHeaders alice = [("Authorization", "Bearer " <> T.encodeUtf8 (hardc

withDamlServiceIn :: FilePath -> String -> [String] -> (ProcessHandle -> IO a) -> IO a
withDamlServiceIn path command args act = withDevNull $ \devNull -> do
let proc' = (shell $ unwords $ ["daml", command] <> args)
let proc' = (shell $ unwords $ ["daml", command, "--shutdown-stdin-close"] <> args)
{ std_out = UseHandle devNull
, create_group = True
, std_in = CreatePipe
, cwd = Just path
}
withCreateProcess proc' $ \_ _ _ ph -> do
withCreateProcess proc' $ \stdin _ _ ph -> do
Just stdin <- pure stdin
r <- act ph
interruptProcessGroupOf ph
hClose stdin
-- We tear things down gracefully instead of killing
-- the process group so that waiting for the parent process
-- ensures that all child processes are all dead too.
-- Gonig via closing stdin works on Windows whereas tearing things
-- down gracefully
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you a word but otherwise looks great, thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typing is hard, thx :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can confirm

_ <- waitForProcess ph
pure r

data DamlStartResource = DamlStartResource
Expand Down