chore: prefer to panic instead of os.Exit (#15285)
This commit is contained in:
@@ -282,7 +282,6 @@ func WriteConfigFile(configFilePath string, config interface{}) {
|
||||
|
||||
func mustWriteFile(filePath string, contents []byte, mode os.FileMode) {
|
||||
if err := os.WriteFile(filePath, contents, mode); err != nil {
|
||||
fmt.Printf(fmt.Sprintf("failed to write file: %v", err) + "\n")
|
||||
os.Exit(1)
|
||||
panic(fmt.Errorf("failed to write file: %w", err))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -352,30 +352,6 @@ func ExternalIP() (string, error) {
|
||||
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
|
||||
|
||||
if cleanupFunc != nil {
|
||||
cleanupFunc()
|
||||
}
|
||||
exitCode := 128
|
||||
|
||||
switch sig {
|
||||
case syscall.SIGINT:
|
||||
exitCode += int(syscall.SIGINT)
|
||||
case syscall.SIGTERM:
|
||||
exitCode += int(syscall.SIGTERM)
|
||||
}
|
||||
|
||||
os.Exit(exitCode)
|
||||
}()
|
||||
}
|
||||
|
||||
// ListenForQuitSignals listens for SIGINT and SIGTERM. When a signal is received,
|
||||
// the cleanup function is called, indicating the caller can gracefully exit or
|
||||
// return.
|
||||
|
||||
+3
-4
@@ -4,6 +4,7 @@ package simapp
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -261,8 +262,7 @@ func NewSimApp(
|
||||
|
||||
// register the streaming service with the BaseApp
|
||||
if err := bApp.SetStreamingService(appOpts, appCodec, keys); err != nil {
|
||||
logger.Error("failed to load state streaming", "err", err)
|
||||
os.Exit(1)
|
||||
panic(fmt.Errorf("failed to load state streaming: %w", err))
|
||||
}
|
||||
|
||||
app := &SimApp{
|
||||
@@ -517,8 +517,7 @@ func NewSimApp(
|
||||
|
||||
if loadLatest {
|
||||
if err := app.LoadLatestVersion(); err != nil {
|
||||
logger.Error("error on loading last version", "err", err)
|
||||
os.Exit(1)
|
||||
panic(fmt.Errorf("error loading last version: %w", err))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -4,6 +4,7 @@ package simapp
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -246,8 +247,7 @@ func NewSimApp(
|
||||
app.App = appBuilder.Build(logger, db, traceStore, baseAppOptions...)
|
||||
|
||||
if err := app.App.BaseApp.SetStreamingService(appOpts, app.appCodec, app.kvStoreKeys()); err != nil {
|
||||
logger.Error("failed to load state streaming", "err", err)
|
||||
os.Exit(1)
|
||||
panic(fmt.Errorf("failed to load state streaming: %w", err))
|
||||
}
|
||||
|
||||
/**** Module Options ****/
|
||||
|
||||
@@ -9,9 +9,11 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -612,11 +614,35 @@ func New(l Logger, baseDir string, cfg Config) (*Network, error) {
|
||||
|
||||
// Ensure we cleanup incase any test was abruptly halted (e.g. SIGINT) as any
|
||||
// defer in a test would not be called.
|
||||
server.TrapSignal(network.Cleanup)
|
||||
trapSignal(network.Cleanup)
|
||||
|
||||
return network, nil
|
||||
}
|
||||
|
||||
// trapSignal traps SIGINT and SIGTERM and calls os.Exit once a signal is received.
|
||||
func trapSignal(cleanupFunc func()) {
|
||||
sigs := make(chan os.Signal, 1)
|
||||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
||||
go func() {
|
||||
sig := <-sigs
|
||||
|
||||
if cleanupFunc != nil {
|
||||
cleanupFunc()
|
||||
}
|
||||
exitCode := 128
|
||||
|
||||
switch sig {
|
||||
case syscall.SIGINT:
|
||||
exitCode += int(syscall.SIGINT)
|
||||
case syscall.SIGTERM:
|
||||
exitCode += int(syscall.SIGTERM)
|
||||
}
|
||||
|
||||
os.Exit(exitCode)
|
||||
}()
|
||||
}
|
||||
|
||||
// LatestHeight returns the latest height of the network or an error if the
|
||||
// query fails or no validators exist.
|
||||
func (n *Network) LatestHeight() (int64, error) {
|
||||
|
||||
Reference in New Issue
Block a user