Let background processing finish before shutting down

This commit is contained in:
jholdstock 2020-06-10 12:29:21 +01:00 committed by David Hill
parent 82dc5f5ba1
commit 78692fea88
2 changed files with 9 additions and 2 deletions

View File

@ -3,6 +3,7 @@ package background
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"sync"
"time" "time"
"decred.org/dcrwallet/rpc/client/dcrd" "decred.org/dcrwallet/rpc/client/dcrd"
@ -18,6 +19,7 @@ var (
walletRPC rpc.WalletConnect walletRPC rpc.WalletConnect
netParams *chaincfg.Params netParams *chaincfg.Params
notifierClosed chan struct{} notifierClosed chan struct{}
shutdownWg *sync.WaitGroup
) )
type NotificationHandler struct{} type NotificationHandler struct{}
@ -54,6 +56,9 @@ func (n *NotificationHandler) Notify(method string, params json.RawMessage) erro
// blockconnected notification is received from dcrd. // blockconnected notification is received from dcrd.
func blockConnected() { func blockConnected() {
shutdownWg.Add(1)
defer shutdownWg.Done()
dcrdClient, err := dcrdRPC.Client(ctx, netParams) dcrdClient, err := dcrdRPC.Client(ctx, netParams)
if err != nil { if err != nil {
log.Error(err) log.Error(err)
@ -216,11 +221,12 @@ func connectNotifier(dcrdWithNotifs rpc.DcrdConnect) error {
case <-ctx.Done(): case <-ctx.Done():
return nil return nil
case <-notifierClosed: case <-notifierClosed:
log.Warnf("dcrd notifier closed")
return nil return nil
} }
} }
func Start(c context.Context, vdb *database.VspDatabase, drpc rpc.DcrdConnect, func Start(c context.Context, wg *sync.WaitGroup, vdb *database.VspDatabase, drpc rpc.DcrdConnect,
dcrdWithNotif rpc.DcrdConnect, wrpc rpc.WalletConnect, p *chaincfg.Params) { dcrdWithNotif rpc.DcrdConnect, wrpc rpc.WalletConnect, p *chaincfg.Params) {
ctx = c ctx = c
@ -228,6 +234,7 @@ func Start(c context.Context, vdb *database.VspDatabase, drpc rpc.DcrdConnect,
dcrdRPC = drpc dcrdRPC = drpc
walletRPC = wrpc walletRPC = wrpc
netParams = p netParams = p
shutdownWg = wg
// Run the block connected handler now to catch up with any blocks mined // Run the block connected handler now to catch up with any blocks mined
// while vspd was shut down. // while vspd was shut down.

View File

@ -91,7 +91,7 @@ func run(ctx context.Context) error {
// Start background process which will continually attempt to reconnect to // Start background process which will continually attempt to reconnect to
// dcrd if the connection drops. // dcrd if the connection drops.
background.Start(ctx, db, dcrd, dcrdWithNotifs, wallets, cfg.netParams.Params) background.Start(ctx, &shutdownWg, db, dcrd, dcrdWithNotifs, wallets, cfg.netParams.Params)
// Wait for shutdown tasks to complete before running deferred tasks and // Wait for shutdown tasks to complete before running deferred tasks and
// returning. // returning.