Skip to content

Commit

Permalink
Merge #1315
Browse files Browse the repository at this point in the history
1315: Add shutdown handler for new launcher r=KtorZ a=rvl

Relates to #1314.

# Overview

This adds a simpler way of ensuring clean shutdown of the wallet on windows (and linux), which doesn't require DaedalusIPC. The mechanism will be used by cardano-launcher.

- Adds the shutdown handler thread.
- Needed to rearrange startup functions around a little bit.
- Unit tests and an integration test.

# Comments

[Hydra jobset](https://hydra.iohk.io/jobset/Cardano/cardano-wallet-pr-1315)


Co-authored-by: Rodney Lorrimar <rodney.lorrimar@iohk.io>
Co-authored-by: KtorZ <matthias.benkort@gmail.com>
  • Loading branch information
3 people committed Feb 26, 2020
2 parents 0fdf956 + 3003a3a commit 3314dca
Show file tree
Hide file tree
Showing 30 changed files with 543 additions and 165 deletions.
3 changes: 1 addition & 2 deletions .weeder.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,4 @@
- name: library
- message:
- name: Module not compiled
- module: Cardano.Launcher.Windows

- module: Cardano.Startup.Windows
2 changes: 2 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ let
haskellBuildUtils = haskellBuildUtils.package;
};

inherit (haskellPackages.cardano-wallet-byron.components.exes) cardano-wallet-byron;

# `tests` are the test suites which have been built.
tests = collectComponents "tests" isCardanoWallet haskellPackages;
# `checks` are the result of executing the tests.
Expand Down
2 changes: 1 addition & 1 deletion lib/byron/cardano-wallet-byron.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ library
, cardano-crypto-wrapper
, cardano-ledger
, cardano-wallet-core
, cardano-wallet-launcher
, cborg
, contra-tracer
, cryptonite
Expand Down Expand Up @@ -88,6 +87,7 @@ executable cardano-wallet-byron
, cardano-wallet-cli
, cardano-wallet-core
, cardano-wallet-launcher
, contra-tracer
, iohk-monitoring
, network
, optparse-applicative
Expand Down
44 changes: 30 additions & 14 deletions lib/byron/exe/cardano-wallet-byron.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import Prelude
import Cardano.BM.Data.Severity
( Severity (..) )
import Cardano.BM.Trace
( Trace, appendName, logDebug, logInfo )
( Trace, appendName, logDebug, logInfo, logNotice )
import Cardano.CLI
( LoggingOptions (..)
, cli
Expand All @@ -50,8 +50,12 @@ import Cardano.CLI
, syncToleranceOption
, withLogging
)
import Cardano.Launcher
( withUtf8Encoding )
import Cardano.Startup
( ShutdownHandlerLog
, installSignalHandlers
, withShutdownHandler
, withUtf8Encoding
)
import Cardano.Wallet.Api.Server
( HostPreference, Listen (..) )
import Cardano.Wallet.Byron
Expand All @@ -66,7 +70,7 @@ import Cardano.Wallet.Byron
import Cardano.Wallet.Byron.Network
( localSocketAddrInfo )
import Cardano.Wallet.Logging
( transformTextTrace )
( trMessage, transformTextTrace )
import Cardano.Wallet.Primitive.AddressDerivation
( NetworkDiscriminant (..) )
import Cardano.Wallet.Primitive.Types
Expand All @@ -75,6 +79,10 @@ import Cardano.Wallet.Version
( GitRevision, Version, gitRevision, showFullVersion, version )
import Control.Applicative
( Const (..), optional )
import Control.Monad
( void )
import Control.Tracer
( contramap )
import Data.Text
( Text )
import Data.Text.Class
Expand Down Expand Up @@ -160,16 +168,18 @@ cmdServe = command "serve" $ info (helper <*> helper' <*> cmd) $ mempty
exec args@(ServeArgs hostPreference listen nodeSocket databaseDir sTolerance logOpt) = do
let addrInfo = localSocketAddrInfo nodeSocket
withTracers logOpt $ \tr tracers -> do
logDebug tr $ MsgServeArgs args
whenJust databaseDir $ setupDirectory (logInfo tr . MsgSetupDatabases)
exitWith =<< serveWallet @'Mainnet
tracers
sTolerance
databaseDir
hostPreference
listen
addrInfo
(beforeMainLoop tr)
installSignalHandlers (logNotice tr MsgSigTerm)
void $ withShutdownHandler (trMessage (contramap (fmap MsgShutdownHandler) tr)) $ do
logDebug tr $ MsgServeArgs args
whenJust databaseDir $ setupDirectory (logInfo tr . MsgSetupDatabases)
exitWith =<< serveWallet @'Mainnet
tracers
sTolerance
databaseDir
hostPreference
listen
addrInfo
(beforeMainLoop tr)

whenJust m fn = case m of
Nothing -> pure ()
Expand Down Expand Up @@ -212,6 +222,8 @@ data MainLog
| MsgSetupDatabases Text
| MsgServeArgs ServeArgs
| MsgListenAddress SockAddr
| MsgSigTerm
| MsgShutdownHandler ShutdownHandlerLog
deriving (Show, Eq)

instance ToText MainLog where
Expand All @@ -228,6 +240,10 @@ instance ToText MainLog where
T.pack $ show args
MsgListenAddress addr ->
"Wallet backend server listening on " <> T.pack (show addr)
MsgSigTerm ->
"Terminated by signal."
MsgShutdownHandler msg' ->
toText msg'

withTracers
:: LoggingOptions TracerSeverities
Expand Down
7 changes: 0 additions & 7 deletions lib/byron/src/Cardano/Wallet/Byron.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ import Cardano.BM.Trace
( Trace, appendName )
import Cardano.DB.Sqlite
( DBLog )
import Cardano.Launcher
( installSignalHandlers )
import Cardano.Wallet
( WalletLog )
import Cardano.Wallet.Api
Expand Down Expand Up @@ -166,7 +164,6 @@ serveWallet
-- ^ Callback to run before the main loop
-> IO ExitCode
serveWallet Tracers{..} sTolerance databaseDir hostPref listen addrInfo beforeMainLoop = do
installSignalHandlers (traceWith applicationTracer MsgSigTerm)
traceWith applicationTracer $ MsgStarting addrInfo
traceWith applicationTracer $ MsgNetworkName $ networkDiscriminantVal @n
Server.withListeningSocket hostPref listen $ \case
Expand Down Expand Up @@ -245,7 +242,6 @@ exitCodeApiServer = \case
data ApplicationLog
= MsgStarting AddrInfo
| MsgNetworkName NetworkDiscriminant
| MsgSigTerm
| MsgServerStartupError ListenError
| MsgDatabaseStartup DatabasesStartupLog
deriving (Generic, Show, Eq)
Expand All @@ -256,8 +252,6 @@ instance ToText ApplicationLog where
"Wallet backend server starting. " <> T.pack (show info) <> "..."
MsgNetworkName n ->
"Node is Haskell Node on " <> toText n
MsgSigTerm ->
"Terminated by signal."
MsgDatabaseStartup dbMsg ->
toText dbMsg
MsgServerStartupError startupErr -> case startupErr of
Expand All @@ -281,7 +275,6 @@ instance DefinePrivacyAnnotation ApplicationLog
instance DefineSeverity ApplicationLog where
defineSeverity = \case
MsgStarting _ -> Info
MsgSigTerm -> Notice
MsgNetworkName _ -> Info
MsgDatabaseStartup ev -> defineSeverity ev
MsgServerStartupError _ -> Alert
Expand Down
4 changes: 2 additions & 2 deletions lib/cli/src/Cardano/CLI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ import Cardano.BM.Data.Tracer
import Cardano.BM.Setup
( setupTrace_, shutdown )
import Cardano.BM.Trace
( Trace, logDebug )
( Trace, appendName, logDebug )
import Cardano.Wallet.Api.Client
( WalletClient (..), walletClient )
import Cardano.Wallet.Api.Server
Expand Down Expand Up @@ -1457,7 +1457,7 @@ withLogging configFile minSeverity action = bracket before after (action . snd)
where
before = initTracer configFile minSeverity
after (sb, (_, tr)) = do
logDebug tr "Logging shutdown."
logDebug (appendName "main" tr) "Logging shutdown."
shutdown sb

data LoggingOptions tracers = LoggingOptions
Expand Down
2 changes: 1 addition & 1 deletion lib/core/test/bench/db/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import Cardano.BM.Data.Tracer
( nullTracer )
import Cardano.DB.Sqlite
( SqliteContext, destroyDBLayer )
import Cardano.Launcher
import Cardano.Startup
( withUtf8Encoding )
import Cardano.Wallet.DB
( DBLayer (..), PrimaryKey (..), cleanDB )
Expand Down
2 changes: 1 addition & 1 deletion lib/core/test/unit/Main.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Main where

import Cardano.Launcher
import Cardano.Startup
( withUtf8Encoding )
import Prelude
import qualified Spec
Expand Down
1 change: 1 addition & 0 deletions lib/jormungandr/cardano-wallet-jormungandr.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ executable cardano-wallet-jormungandr
, cardano-wallet-core
, cardano-wallet-jormungandr
, cardano-wallet-launcher
, contra-tracer
, filepath
, iohk-monitoring
, network
Expand Down
96 changes: 58 additions & 38 deletions lib/jormungandr/exe/cardano-wallet-jormungandr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import Prelude
import Cardano.BM.Data.Severity
( Severity (..) )
import Cardano.BM.Trace
( Trace, appendName, logDebug, logInfo )
( Trace, appendName, logInfo, logNotice )
import Cardano.CLI
( LoggingOptions (..)
, Port (..)
Expand Down Expand Up @@ -59,7 +59,13 @@ import Cardano.CLI
, withLogging
)
import Cardano.Launcher
( StdStream (..), withUtf8Encoding )
( StdStream (..) )
import Cardano.Startup
( ShutdownHandlerLog
, installSignalHandlers
, withShutdownHandler
, withUtf8Encoding
)
import Cardano.Wallet.Api.Server
( HostPreference, Listen (..) )
import Cardano.Wallet.Jormungandr
Expand All @@ -79,7 +85,7 @@ import Cardano.Wallet.Jormungandr.Network
, JormungandrConnParams (..)
)
import Cardano.Wallet.Logging
( transformTextTrace )
( trMessage, transformTextTrace )
import Cardano.Wallet.Primitive.AddressDerivation
( NetworkDiscriminant (..) )
import Cardano.Wallet.Primitive.Types
Expand All @@ -88,6 +94,10 @@ import Cardano.Wallet.Version
( GitRevision, Version, gitRevision, showFullVersion, version )
import Control.Applicative
( Const (..), optional, (<|>) )
import Control.Monad
( void )
import Control.Tracer
( contramap )
import Data.List
( isPrefixOf )
import Data.Maybe
Expand Down Expand Up @@ -230,29 +240,32 @@ cmdLaunch dataDir = command "launch" $ info (helper <*> helper' <*> cmd) $ mempt
<*> extraArguments)
exec args@(LaunchArgs hostPreference listen nodePort mStateDir sTolerance logOpt jArgs) = do
withTracers logOpt $ \tr tracers -> do
logDebug tr $ MsgLaunchArgs args
case genesisBlock jArgs of
Right block0File -> requireFilePath block0File
Left _ -> pure ()
let stateDir = fromMaybe (dataDir </> "testnet") mStateDir
let databaseDir = stateDir </> "wallets"
let cp = JormungandrConfig
{ _stateDir = stateDir
, _genesisBlock = genesisBlock jArgs
, _restApiPort = fromIntegral . getPort <$> nodePort
, _outputStream = Inherit
, _extraArgs = extraJormungandrArgs jArgs
}
setupDirectory (logInfo tr . MsgSetupStateDir) stateDir
setupDirectory (logInfo tr . MsgSetupDatabases) databaseDir
exitWith =<< serveWallet @'Testnet
tracers
sTolerance
(Just databaseDir)
hostPreference
listen
(Launch cp)
(beforeMainLoop tr)
installSignalHandlers (logNotice tr MsgSigTerm)
let trShutdown = trMessage (contramap (fmap MsgShutdownHandler) tr)
void $ withShutdownHandler trShutdown $ do
logInfo tr $ MsgLaunchArgs args
case genesisBlock jArgs of
Right block0File -> requireFilePath block0File
Left _ -> pure ()
let stateDir = fromMaybe (dataDir </> "testnet") mStateDir
let databaseDir = stateDir </> "wallets"
let cp = JormungandrConfig
{ _stateDir = stateDir
, _genesisBlock = genesisBlock jArgs
, _restApiPort = fromIntegral . getPort <$> nodePort
, _outputStream = Inherit
, _extraArgs = extraJormungandrArgs jArgs
}
setupDirectory (logInfo tr . MsgSetupStateDir) stateDir
setupDirectory (logInfo tr . MsgSetupDatabases) databaseDir
exitWith =<< serveWallet @'Testnet
tracers
sTolerance
(Just databaseDir)
hostPreference
listen
(Launch cp)
(beforeMainLoop tr)

{-------------------------------------------------------------------------------
Command - 'serve'
Expand Down Expand Up @@ -289,18 +302,21 @@ cmdServe = command "serve" $ info (helper <*> helper' <*> cmd) $ mempty
-> IO ()
exec args@(ServeArgs hostPreference listen nodePort databaseDir sTolerance block0H logOpt) = do
withTracers logOpt $ \tr tracers -> do
logDebug tr $ MsgServeArgs args
let baseUrl = localhostBaseUrl $ getPort nodePort
let cp = JormungandrConnParams block0H baseUrl
whenJust databaseDir $ setupDirectory (logInfo tr . MsgSetupDatabases)
exitWith =<< serveWallet @'Testnet
tracers
sTolerance
databaseDir
hostPreference
listen
(UseRunning cp)
(beforeMainLoop tr)
installSignalHandlers (logNotice tr MsgSigTerm)
let trShutdown = trMessage (contramap (fmap MsgShutdownHandler) tr)
void $ withShutdownHandler trShutdown $ do
logInfo tr $ MsgServeArgs args
let baseUrl = localhostBaseUrl $ getPort nodePort
let cp = JormungandrConnParams block0H baseUrl
whenJust databaseDir $ setupDirectory (logInfo tr . MsgSetupDatabases)
exitWith =<< serveWallet @'Testnet
tracers
sTolerance
databaseDir
hostPreference
listen
(UseRunning cp)
(beforeMainLoop tr)

whenJust m fn = case m of
Nothing -> pure ()
Expand Down Expand Up @@ -381,6 +397,8 @@ data MainLog
| MsgLaunchArgs LaunchArgs
| MsgServeArgs ServeArgs
| MsgListenAddress SockAddr
| MsgSigTerm
| MsgShutdownHandler ShutdownHandlerLog
deriving (Show, Eq)

instance ToText MainLog where
Expand All @@ -395,6 +413,8 @@ instance ToText MainLog where
MsgServeArgs args -> T.pack $ show args
MsgListenAddress addr ->
"Wallet backend server listening on " <> T.pack (show addr)
MsgSigTerm -> "Terminated by signal."
MsgShutdownHandler msg' -> toText msg'

withTracers
:: LoggingOptions TracerSeverities
Expand Down
Loading

0 comments on commit 3314dca

Please sign in to comment.