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>
  • Loading branch information
iohk-bors[bot] and rvl committed Feb 17, 2020
2 parents fb51675 + e0aed8a commit 63efed4
Show file tree
Hide file tree
Showing 28 changed files with 468 additions and 138 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 @@ -39,6 +39,8 @@ let
haskellBuildUtils = iohkLib.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
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, logDebug, logInfo, logNotice )
import Cardano.CLI
( LoggingOptions (..)
, Port (..)
Expand Down Expand Up @@ -58,7 +58,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 @@ -78,7 +84,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 @@ -87,6 +93,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 @@ -228,29 +238,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
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)

{-------------------------------------------------------------------------------
Command - 'serve'
Expand Down Expand Up @@ -287,18 +300,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
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)

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

instance ToText MainLog where
Expand All @@ -393,6 +411,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
6 changes: 1 addition & 5 deletions lib/jormungandr/src/Cardano/Wallet/Jormungandr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ import Cardano.CLI
import Cardano.DB.Sqlite
( DBLog )
import Cardano.Launcher
( ProcessHasExited (..), installSignalHandlers )
( ProcessHasExited (..) )
import Cardano.Pool.Metrics
( StakePoolLayer, StakePoolLog, monitorStakePools, newStakePoolLayer )
import Cardano.Wallet
Expand Down Expand Up @@ -206,7 +206,6 @@ serveWallet
-- ^ Callback to run before the main loop
-> IO ExitCode
serveWallet Tracers{..} sTolerance databaseDir hostPref listen backend beforeMainLoop = do
installSignalHandlers (traceWith applicationTracer MsgSigTerm)
traceWith applicationTracer $ MsgStarting backend
traceWith applicationTracer $ MsgNetworkName $ networkDiscriminantVal @n
Server.withListeningSocket hostPref listen $ \case
Expand Down Expand Up @@ -342,7 +341,6 @@ toWLBlock = J.convertBlock
data ApplicationLog
= MsgStarting JormungandrBackend
| MsgNetworkName NetworkDiscriminant
| MsgSigTerm
| MsgWalletStartupError ErrStartup
| MsgServerStartupError ListenError
| MsgDatabaseStartup DatabasesStartupLog
Expand All @@ -353,7 +351,6 @@ instance ToText ApplicationLog where
MsgStarting backend ->
"Wallet backend server starting. " <> toText backend <> "..."
MsgNetworkName n -> "Node is Jörmungandr on " <> toText n
MsgSigTerm -> "Terminated by signal."
MsgDatabaseStartup dbMsg -> toText dbMsg
MsgWalletStartupError startupErr -> case startupErr of
ErrStartupGetBlockchainParameters e -> case e of
Expand Down Expand Up @@ -407,7 +404,6 @@ instance DefinePrivacyAnnotation ApplicationLog
instance DefineSeverity ApplicationLog where
defineSeverity ev = case ev of
MsgStarting _ -> Info
MsgSigTerm -> Notice
MsgNetworkName _ -> Info
MsgDatabaseStartup dbEv -> defineSeverity dbEv
MsgWalletStartupError _ -> Alert
Expand Down
2 changes: 1 addition & 1 deletion lib/jormungandr/src/Cardano/Wallet/Jormungandr/Network.hs
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ withJormungandr tr (JormungandrConfig stateDir block0 mPort output extraArgs) cb
[ "--rest-listen", "127.0.0.1:" <> show apiPort
, "--storage", stateDir </> "chain"
] ++ extraArgs
let cmd = Command "jormungandr" args (return ()) output
let cmd = Command "jormungandr" args (return ()) Inherit output
res <- withBackendProcess (contramap MsgLauncher tr) cmd $ do
waitForPort defaultRetryPolicy apiPort >>= \case
True -> Right <$> cb (JormungandrConnParams block0H baseUrl)
Expand Down
Loading

0 comments on commit 63efed4

Please sign in to comment.