Skip to content
This repository has been archived by the owner on Aug 18, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1962 from input-output-hk/int-index/csl-1758-bracket
Browse files Browse the repository at this point in the history
[CSL-1758] Localize DB use in launcher
  • Loading branch information
volhovm authored Nov 24, 2017
2 parents 9e46092 + c3f46f6 commit 0c1fab9
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions tools/src/launcher/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,10 @@ instance HasConfiguration => MonadDB LauncherMode where
dbWriteBatch = dbWriteBatchDefault
dbDelete = dbDeleteDefault

bracketNodeDBs :: FilePath -> (NodeDBs -> IO a) -> IO a
bracketNodeDBs dbPath = bracket (openNodeDBs False dbPath) closeNodeDBs
newtype NodeDbPath = NodeDbPath FilePath

bracketNodeDBs :: NodeDbPath -> (NodeDBs -> IO a) -> IO a
bracketNodeDBs (NodeDbPath dbPath) = bracket (openNodeDBs False dbPath) closeNodeDBs

main :: IO ()
main =
Expand All @@ -262,16 +264,14 @@ main =
Just _ ->
set Log.ltFiles [Log.HandlerWrap "launcher" Nothing] .
set Log.ltSeverity (Just Log.Debug)
bracketNodeDBs loNodeDbPath $ \lmcNodeDBs ->
Log.usingLoggerName "launcher" $
Log.usingLoggerName "launcher" $
withConfigurations loConfiguration $
let lmc = LauncherModeContext{..} in
case loWalletPath of
Nothing -> do
logNotice "LAUNCHER STARTED"
logInfo "Running in the server scenario"
serverScenario
lmc
(NodeDbPath loNodeDbPath)
loNodeLogConfig
(loNodePath, realNodeArgs, loNodeLogPath)
( loUpdaterPath
Expand All @@ -283,7 +283,7 @@ main =
logNotice "LAUNCHER STARTED"
logInfo "Running in the client scenario"
clientScenario
lmc
(NodeDbPath loNodeDbPath)
loNodeLogConfig
(loNodePath, realNodeArgs, loNodeLogPath)
(wpath, loWalletArgs)
Expand Down Expand Up @@ -325,21 +325,21 @@ main =
-- * Launch the node.
-- * If it exits with code 20, then update and restart, else quit.
serverScenario
:: LauncherModeContext
:: NodeDbPath
-> Maybe FilePath -- ^ Logger config
-> (FilePath, [Text], Maybe FilePath) -- ^ Node, its args, node log
-> (FilePath, [Text], Maybe FilePath, Maybe FilePath)
-- ^ Updater, args, updater runner, the update .tar
-> Maybe String -- ^ Report server
-> M ()
serverScenario lmc logConf node updater report = do
runUpdater lmc updater
serverScenario ndbp logConf node updater report = do
runUpdater ndbp updater
-- TODO: the updater, too, should create a log if it fails
(_, nodeAsync, nodeLog) <- spawnNode node
exitCode <- wait nodeAsync
if exitCode == ExitFailure 20 then do
logNotice $ sformat ("The node has exited with "%shown) exitCode
serverScenario lmc logConf node updater report
serverScenario ndbp logConf node updater report
else do
logWarning $ sformat ("The node has exited with "%shown) exitCode
whenJust report $ \repServ -> do
Expand All @@ -352,7 +352,7 @@ serverScenario lmc logConf node updater report = do
-- * Launch the node and the wallet.
-- * If the wallet exits with code 20, then update and restart, else quit.
clientScenario
:: LauncherModeContext
:: NodeDbPath
-> Maybe FilePath -- ^ Logger config
-> (FilePath, [Text], Maybe FilePath) -- ^ Node, its args, node log
-> (FilePath, [Text]) -- ^ Wallet, args
Expand All @@ -361,8 +361,8 @@ clientScenario
-> Int -- ^ Node timeout, in seconds
-> Maybe String -- ^ Report server
-> M ()
clientScenario lmc logConf node wallet updater nodeTimeout report = do
runUpdater lmc updater
clientScenario ndbp logConf node wallet updater nodeTimeout report = do
runUpdater ndbp updater
(nodeHandle, nodeAsync, nodeLog) <- spawnNode node
walletAsync <- async (runWallet wallet)
(someAsync, exitCode) <- waitAny [nodeAsync, walletAsync]
Expand All @@ -378,7 +378,7 @@ clientScenario lmc logConf node wallet updater nodeTimeout report = do
logInfo $ sformat ("Killing the node in "%int%" seconds") nodeTimeout
sleep (fromIntegral nodeTimeout)
killNode nodeHandle nodeAsync
clientScenario lmc logConf node wallet updater nodeTimeout report
clientScenario ndbp logConf node wallet updater nodeTimeout report
| otherwise -> do
logWarning $ sformat ("The wallet has exited with "%shown) exitCode
-- TODO: does the wallet have some kind of log?
Expand All @@ -400,8 +400,8 @@ clientScenario lmc logConf node wallet updater nodeTimeout report = do

-- | We run the updater and delete the update file if the update was
-- successful.
runUpdater :: LauncherModeContext -> (FilePath, [Text], Maybe FilePath, Maybe FilePath) -> M ()
runUpdater lmc (path, args, runnerPath, mUpdateArchivePath) = do
runUpdater :: NodeDbPath -> (FilePath, [Text], Maybe FilePath, Maybe FilePath) -> M ()
runUpdater ndbp (path, args, runnerPath, mUpdateArchivePath) = do
whenM (liftIO (doesFileExist path)) $ do
logNotice "Running the updater"
let args' = args ++ maybe [] (one . toText) mUpdateArchivePath
Expand All @@ -419,7 +419,9 @@ runUpdater lmc (path, args, runnerPath, mUpdateArchivePath) = do
-- hopefully if the updater has succeeded it *does* exist
whenJust mUpdateArchivePath $ \updateArchivePath -> liftIO $ do
updateArchive <- BS.L.readFile updateArchivePath
usingReaderT lmc $ affirmUpdateInstalled (installerHash updateArchive)
bracketNodeDBs ndbp $ \lmcNodeDBs ->
usingReaderT LauncherModeContext{..} $
affirmUpdateInstalled (installerHash updateArchive)
removeFile updateArchivePath
ExitFailure code ->
logWarning $ sformat ("The updater has failed (exit code "%int%")") code
Expand Down

0 comments on commit 0c1fab9

Please sign in to comment.