lnd+cmd: move interrupt into cmd

If the main package is used as a library, we don't want it to
register interrupt signals itself. Rather we want to pass in the
shutdown channel manually. We do this in the cmd now.
This commit is contained in:
Oliver Gugger 2020-04-30 09:42:28 +02:00
parent 7158103d4d
commit 620eaa3199
No known key found for this signature in database
GPG Key ID: 8E4256593F177720
2 changed files with 15 additions and 10 deletions

View File

@ -4,8 +4,9 @@ import (
"fmt"
"os"
flags "github.com/jessevdk/go-flags"
"github.com/jessevdk/go-flags"
"github.com/lightningnetwork/lnd"
"github.com/lightningnetwork/lnd/signal"
)
func main() {
@ -17,9 +18,15 @@ func main() {
os.Exit(1)
}
// Hook interceptor for os signals.
signal.Intercept()
// Call the "real" main in a nested manner so the defers will properly
// be executed in the case of a graceful shutdown.
if err := lnd.Main(loadedConfig, lnd.ListenerCfg{}); err != nil {
err = lnd.Main(
loadedConfig, lnd.ListenerCfg{}, signal.ShutdownChannel(),
)
if err != nil {
if e, ok := err.(*flags.Error); ok && e.Type == flags.ErrHelp {
} else {
_, _ = fmt.Fprintln(os.Stderr, err)

14
lnd.go
View File

@ -147,13 +147,11 @@ type ListenerCfg struct {
// listeners.
type rpcListeners func() ([]*ListenerWithSignal, func(), error)
// Main is the true entry point for lnd. This function is required since defers
// created in the top-level scope of a main method aren't executed if os.Exit()
// is called.
func Main(config *Config, lisCfg ListenerCfg) error {
// Hook interceptor for os signals.
signal.Intercept()
// Main is the true entry point for lnd. It accepts a fully populated and
// validated main configuration struct and an optional listener config struct.
// This function starts all main system components then blocks until a signal
// is received on the shutdownChan at which point everything is shut down again.
func Main(config *Config, lisCfg ListenerCfg, shutdownChan <-chan struct{}) error {
cfg = config
defer func() {
ltndLog.Info("Shutdown complete")
@ -728,7 +726,7 @@ func Main(config *Config, lisCfg ListenerCfg) error {
// Wait for shutdown signal from either a graceful server stop or from
// the interrupt handler.
<-signal.ShutdownChannel()
<-shutdownChan
return nil
}