vspd: Create essential resources in main run func.

Creating the essential DB/RPC resources which are required for both
webapi and vspd in the main func allows the two components to be
decoupled.
This commit is contained in:
jholdstock 2023-09-15 14:46:21 +01:00 committed by Jamie Holdstock
parent 61b6460014
commit ba3c7bf6d9
2 changed files with 33 additions and 37 deletions

View File

@ -9,8 +9,11 @@ import (
"os" "os"
"runtime" "runtime"
"github.com/decred/dcrd/wire"
"github.com/decred/vspd/database"
"github.com/decred/vspd/internal/config" "github.com/decred/vspd/internal/config"
"github.com/decred/vspd/internal/version" "github.com/decred/vspd/internal/version"
"github.com/decred/vspd/rpc"
) )
func main() { func main() {
@ -45,11 +48,34 @@ func run() int {
log.Warnf("") log.Warnf("")
} }
vspd, err := newVspd(cfg, log) // Open database.
db, err := database.Open(cfg.dbPath, cfg.logger(" DB"), maxVoteChangeRecords)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "newVspd error: %v\n", err) log.Errorf("Failed to open database: %v", err)
return 1 return 1
} }
const writeBackup = true
defer db.Close(writeBackup)
return vspd.run() rpcLog := cfg.logger("RPC")
// Create a channel to receive blockConnected notifications from dcrd.
blockNotifChan := make(chan *wire.BlockHeader)
// Create RPC client for local dcrd instance (used for broadcasting and
// checking the status of fee transactions).
dcrd := rpc.SetupDcrd(cfg.DcrdUser, cfg.DcrdPass, cfg.DcrdHost, cfg.dcrdCert, cfg.network.Params, rpcLog, blockNotifChan)
defer dcrd.Close()
// Create RPC client for remote dcrwallet instances (used for voting).
wallets := rpc.SetupWallet(cfg.walletUsers, cfg.walletPasswords, cfg.walletHosts, cfg.walletCerts, cfg.network.Params, rpcLog)
defer wallets.Close()
vspd := newVspd(cfg, log, db, dcrd, wallets, blockNotifChan)
// Create a context that is canceled when a shutdown request is received
// through an interrupt signal.
ctx := shutdownListener(log)
return vspd.run(ctx)
} }

View File

@ -53,26 +53,8 @@ type vspd struct {
lastScannedBlock int64 lastScannedBlock int64
} }
// newVspd creates the essential resources required by vspd - a database, logger func newVspd(cfg *vspdConfig, log slog.Logger, db *database.VspDatabase,
// and RPC clients - then returns an instance of vspd which is ready to be run. dcrd rpc.DcrdConnect, wallets rpc.WalletConnect, blockNotifChan chan *wire.BlockHeader) *vspd {
func newVspd(cfg *vspdConfig, 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)
}
rpcLog := cfg.logger("RPC")
// Create a channel to receive blockConnected notifications from dcrd.
blockNotifChan := make(chan *wire.BlockHeader)
// Create RPC client for local dcrd instance (used for broadcasting and
// checking the status of fee transactions).
dcrd := rpc.SetupDcrd(cfg.DcrdUser, cfg.DcrdPass, cfg.DcrdHost, cfg.dcrdCert, cfg.network.Params, rpcLog, blockNotifChan)
// Create RPC client for remote dcrwallet instances (used for voting).
wallets := rpc.SetupWallet(cfg.walletUsers, cfg.walletPasswords, cfg.walletHosts, cfg.walletCerts, cfg.network.Params, rpcLog)
v := &vspd{ v := &vspd{
cfg: cfg, cfg: cfg,
@ -84,22 +66,10 @@ func newVspd(cfg *vspdConfig, log slog.Logger) (*vspd, error) {
blockNotifChan: blockNotifChan, blockNotifChan: blockNotifChan,
} }
return v, nil return v
} }
// run starts all of vspds background services including the web server, and func (v *vspd) run(ctx context.Context) int {
// stops all started services when a shutdown is requested.
func (v *vspd) run() int {
// Defer shutdown tasks.
const writeBackup = true
defer v.db.Close(writeBackup)
defer v.dcrd.Close()
defer v.wallets.Close()
// Create a context that is canceled when a shutdown request is received
// through an interrupt signal.
ctx := shutdownListener(v.log)
// Run database integrity checks to ensure all data in database is present // Run database integrity checks to ensure all data in database is present
// and up-to-date. // and up-to-date.
err := v.checkDatabaseIntegrity(ctx) err := v.checkDatabaseIntegrity(ctx)