Skip to content

Commit

Permalink
vspd: Move start/stop logging to main func.
Browse files Browse the repository at this point in the history
This ensures that the startup/shutdown messages are always the
first/last thing to be logged.
  • Loading branch information
jholdstock committed Sep 13, 2023
1 parent e38032b commit 8038a0e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
23 changes: 22 additions & 1 deletion cmd/vspd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ package main
import (
"fmt"
"os"
"runtime"

"github.com/decred/vspd/internal/version"
)

func main() {
Expand All @@ -23,7 +26,25 @@ func run() int {
return 1
}

vspd, err := newVspd(cfg)
log := cfg.logger("VSP")

defer log.Criticalf("Shutdown complete")
log.Criticalf("Version %s (Go version %s %s/%s)", version.String(),
runtime.Version(), runtime.GOOS, runtime.GOARCH)

if cfg.netParams == &mainNetParams && version.IsPreRelease() {
log.Warnf("")
log.Warnf("\tWARNING: This is a pre-release version of vspd which should not be used on mainnet.")
log.Warnf("")
}

if cfg.VspClosed {
log.Warnf("")
log.Warnf("\tWARNING: Config --vspclosed is set. This will prevent vspd from accepting new tickets.")
log.Warnf("")
}

vspd, err := newVspd(cfg, log)
if err != nil {
fmt.Fprintf(os.Stderr, "newVspd error: %v\n", err)
return 1
Expand Down
21 changes: 1 addition & 20 deletions cmd/vspd/vspd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"context"
"errors"
"fmt"
"runtime"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -56,14 +55,13 @@ type vspd struct {

// newVspd creates the essential resources required by vspd - a database, logger
// and RPC clients - then returns an instance of vspd which is ready to be run.
func newVspd(cfg *config) (*vspd, error) {
func newVspd(cfg *config, log slog.Logger) (*vspd, error) {
// Open database.
db, err := database.Open(cfg.dbPath, cfg.logger(" DB"), maxVoteChangeRecords)
if err != nil {
return nil, fmt.Errorf("failed to open database: %w", err)
}

log := cfg.logger("VSP")
rpcLog := cfg.logger("RPC")

// Create a channel to receive blockConnected notifications from dcrd.
Expand Down Expand Up @@ -92,24 +90,7 @@ func newVspd(cfg *config) (*vspd, error) {
// run starts all of vspds background services including the web server, and
// stops all started services when a shutdown is requested.
func (v *vspd) run() int {
v.log.Criticalf("Version %s (Go version %s %s/%s)", version.String(), runtime.Version(),
runtime.GOOS, runtime.GOARCH)

if v.cfg.netParams == &mainNetParams &&
version.IsPreRelease() {
v.log.Warnf("")
v.log.Warnf("\tWARNING: This is a pre-release version of vspd which should not be used on mainnet.")
v.log.Warnf("")
}

if v.cfg.VspClosed {
v.log.Warnf("")
v.log.Warnf("\tWARNING: Config --vspclosed is set. This will prevent vspd from accepting new tickets.")
v.log.Warnf("")
}

// Defer shutdown tasks.
defer v.log.Criticalf("Shutdown complete")
const writeBackup = true
defer v.db.Close(writeBackup)
defer v.dcrd.Close()
Expand Down

0 comments on commit 8038a0e

Please sign in to comment.