Merge PR #2667: Gaialite signal handling fix
Gaialite signal handling is broken, repair it
This commit is contained in:
commit
0d5ebd2495
|
@ -2,16 +2,13 @@ package lcd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"net"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/cosmos/cosmos-sdk/client"
|
"github.com/cosmos/cosmos-sdk/client"
|
||||||
"github.com/cosmos/cosmos-sdk/client/context"
|
"github.com/cosmos/cosmos-sdk/client/context"
|
||||||
"github.com/cosmos/cosmos-sdk/client/keys"
|
"github.com/cosmos/cosmos-sdk/client/keys"
|
||||||
"github.com/cosmos/cosmos-sdk/client/rpc"
|
"github.com/cosmos/cosmos-sdk/client/rpc"
|
||||||
"github.com/cosmos/cosmos-sdk/client/tx"
|
"github.com/cosmos/cosmos-sdk/client/tx"
|
||||||
"github.com/cosmos/cosmos-sdk/codec"
|
"github.com/cosmos/cosmos-sdk/codec"
|
||||||
|
"github.com/cosmos/cosmos-sdk/server"
|
||||||
auth "github.com/cosmos/cosmos-sdk/x/auth/client/rest"
|
auth "github.com/cosmos/cosmos-sdk/x/auth/client/rest"
|
||||||
bank "github.com/cosmos/cosmos-sdk/x/bank/client/rest"
|
bank "github.com/cosmos/cosmos-sdk/x/bank/client/rest"
|
||||||
gov "github.com/cosmos/cosmos-sdk/x/gov/client/rest"
|
gov "github.com/cosmos/cosmos-sdk/x/gov/client/rest"
|
||||||
|
@ -21,9 +18,11 @@ import (
|
||||||
"github.com/rakyll/statik/fs"
|
"github.com/rakyll/statik/fs"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
cmn "github.com/tendermint/tendermint/libs/common"
|
|
||||||
"github.com/tendermint/tendermint/libs/log"
|
"github.com/tendermint/tendermint/libs/log"
|
||||||
tmserver "github.com/tendermint/tendermint/rpc/lib/server"
|
tmserver "github.com/tendermint/tendermint/rpc/lib/server"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -53,10 +52,16 @@ func ServeCommand(cdc *codec.Codec) *cobra.Command {
|
||||||
sslHosts := viper.GetString(flagSSLHosts)
|
sslHosts := viper.GetString(flagSSLHosts)
|
||||||
certFile := viper.GetString(flagSSLCertFile)
|
certFile := viper.GetString(flagSSLCertFile)
|
||||||
keyFile := viper.GetString(flagSSLKeyFile)
|
keyFile := viper.GetString(flagSSLKeyFile)
|
||||||
cleanupFunc := func() {}
|
|
||||||
|
|
||||||
var listener net.Listener
|
var listener net.Listener
|
||||||
var fingerprint string
|
var fingerprint string
|
||||||
|
|
||||||
|
server.TrapSignal(func() {
|
||||||
|
err := listener.Close()
|
||||||
|
logger.Error("error closing listener", "err", err)
|
||||||
|
})
|
||||||
|
|
||||||
|
var cleanupFunc func()
|
||||||
if viper.GetBool(flagInsecure) {
|
if viper.GetBool(flagInsecure) {
|
||||||
listener, err = tmserver.StartHTTPServer(
|
listener, err = tmserver.StartHTTPServer(
|
||||||
listenAddr, handler, logger,
|
listenAddr, handler, logger,
|
||||||
|
@ -89,6 +94,7 @@ func ServeCommand(cdc *codec.Codec) *cobra.Command {
|
||||||
}
|
}
|
||||||
defer cleanupFunc()
|
defer cleanupFunc()
|
||||||
}
|
}
|
||||||
|
|
||||||
listener, err = tmserver.StartHTTPAndTLSServer(
|
listener, err = tmserver.StartHTTPAndTLSServer(
|
||||||
listenAddr, handler,
|
listenAddr, handler,
|
||||||
certFile, keyFile,
|
certFile, keyFile,
|
||||||
|
@ -102,13 +108,6 @@ func ServeCommand(cdc *codec.Codec) *cobra.Command {
|
||||||
}
|
}
|
||||||
logger.Info("REST server started")
|
logger.Info("REST server started")
|
||||||
|
|
||||||
// wait forever and cleanup
|
|
||||||
cmn.TrapSignal(func() {
|
|
||||||
defer cleanupFunc()
|
|
||||||
err := listener.Close()
|
|
||||||
logger.Error("error closing listener", "err", err)
|
|
||||||
})
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,9 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
@ -203,6 +205,23 @@ func ExternalIP() (string, error) {
|
||||||
return "", errors.New("are you connected to the network?")
|
return "", errors.New("are you connected to the network?")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TrapSignal traps SIGINT and SIGTERM and terminates the server correctly.
|
||||||
|
func TrapSignal(cleanupFunc func()) {
|
||||||
|
sigs := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
go func() {
|
||||||
|
sig := <-sigs
|
||||||
|
switch sig {
|
||||||
|
case syscall.SIGTERM:
|
||||||
|
defer cleanupFunc()
|
||||||
|
os.Exit(128 + int(syscall.SIGTERM))
|
||||||
|
case syscall.SIGINT:
|
||||||
|
defer cleanupFunc()
|
||||||
|
os.Exit(128 + int(syscall.SIGINT))
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
func skipInterface(iface net.Interface) bool {
|
func skipInterface(iface net.Interface) bool {
|
||||||
if iface.Flags&net.FlagUp == 0 {
|
if iface.Flags&net.FlagUp == 0 {
|
||||||
return true // interface down
|
return true // interface down
|
||||||
|
|
Loading…
Reference in New Issue