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