Make run() return int instead of error.

Theres no reason for run() to return an error, it can simply return an exit code for the process.
This commit is contained in:
jholdstock 2022-03-31 13:48:40 +01:00 committed by Jamie Holdstock
parent 75026f5e91
commit ebadf8eefc

21
vspd.go
View File

@ -6,7 +6,6 @@ package main
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"os" "os"
"runtime" "runtime"
@ -31,24 +30,22 @@ func main() {
shutdownCtx := withShutdownCancel(context.Background()) shutdownCtx := withShutdownCancel(context.Background())
go shutdownListener() go shutdownListener()
// Run until error is returned, or shutdown is requested. // Run until an exit code is returned.
if err := run(shutdownCtx); err != nil && !errors.Is(err, context.Canceled) { os.Exit(run(shutdownCtx))
os.Exit(1)
}
} }
// run is the main startup and teardown logic performed by the main package. It // run is the main startup and teardown logic performed by the main package. It
// is responsible for parsing the config, creating a dcrwallet RPC client, // is responsible for parsing the config, creating dcrd and dcrwallet RPC clients,
// opening the database, starting the webserver, and stopping all started // opening the database, starting the webserver, and stopping all started
// services when the context is cancelled. // services when the provided context is cancelled.
func run(shutdownCtx context.Context) error { func run(shutdownCtx context.Context) int {
// Load config file and parse CLI args. // Load config file and parse CLI args.
cfg, err := loadConfig() cfg, err := loadConfig()
if err != nil { if err != nil {
// Don't use logger here because it may not be initialized. // Don't use logger here because it may not be initialized.
fmt.Fprintf(os.Stderr, "Config error: %v\n", err) fmt.Fprintf(os.Stderr, "Config error: %v\n", err)
return err return 1
} }
// Show version at startup. // Show version at startup.
@ -78,7 +75,7 @@ func run(shutdownCtx context.Context) error {
log.Errorf("Database error: %v", err) log.Errorf("Database error: %v", err)
requestShutdown() requestShutdown()
shutdownWg.Wait() shutdownWg.Wait()
return err return 1
} }
defer db.Close() defer db.Close()
@ -118,7 +115,7 @@ func run(shutdownCtx context.Context) error {
log.Errorf("Failed to initialize webapi: %v", err) log.Errorf("Failed to initialize webapi: %v", err)
requestShutdown() requestShutdown()
shutdownWg.Wait() shutdownWg.Wait()
return err return 1
} }
// Create a dcrd client with a blockconnected notification handler. // Create a dcrd client with a blockconnected notification handler.
@ -135,5 +132,5 @@ func run(shutdownCtx context.Context) error {
// returning. // returning.
shutdownWg.Wait() shutdownWg.Wait()
return shutdownCtx.Err() return 0
} }