From d6420fb7692777cc7c3c5da1c6b36dbce63bcb3f Mon Sep 17 00:00:00 2001 From: Aurora Gaffney Date: Wed, 31 Jul 2024 15:54:32 -0500 Subject: [PATCH] feat: periodic log message during catch-up sync Fixes #179 --- internal/indexer/indexer.go | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/internal/indexer/indexer.go b/internal/indexer/indexer.go index 325092b..073e71d 100644 --- a/internal/indexer/indexer.go +++ b/internal/indexer/indexer.go @@ -8,7 +8,9 @@ package indexer import ( "encoding/hex" + "fmt" "strings" + "time" "github.com/blinklabs-io/cdnsd/internal/config" "github.com/blinklabs-io/cdnsd/internal/logging" @@ -26,15 +28,21 @@ import ( "github.com/miekg/dns" ) +const ( + syncStatusLogInterval = 30 * time.Second +) + type Domain struct { Name string Nameservers map[string]string } type Indexer struct { - pipeline *pipeline.Pipeline - domains map[string]Domain - tipReached bool + pipeline *pipeline.Pipeline + domains map[string]Domain + tipReached bool + syncLogTimer *time.Timer + syncStatus input_chainsync.ChainSyncStatus } // Singleton indexer instance @@ -51,10 +59,14 @@ func (i *Indexer) Start() error { inputOpts := []input_chainsync.ChainSyncOptionFunc{ input_chainsync.WithStatusUpdateFunc( func(status input_chainsync.ChainSyncStatus) { + i.syncStatus = status if err := state.GetState().UpdateCursor(status.SlotNumber, status.BlockHash); err != nil { logger.Errorf("failed to update cursor: %s", err) } if !i.tipReached && status.TipReached { + if i.syncLogTimer != nil { + i.syncLogTimer.Stop() + } i.tipReached = true logger.Infof("caught up to chain tip") } @@ -149,6 +161,8 @@ func (i *Indexer) Start() error { logger.Fatalf("pipeline failed: %s\n", err) } }() + // Schedule periodic catch-up sync log messages + i.scheduleSyncStatusLog() return nil } @@ -259,6 +273,22 @@ func (i *Indexer) handleEvent(evt event.Event) error { return nil } +func (i *Indexer) scheduleSyncStatusLog() { + i.syncLogTimer = time.AfterFunc(syncStatusLogInterval, i.syncStatusLog) +} + +func (i *Indexer) syncStatusLog() { + logging.GetLogger().Info( + fmt.Sprintf( + "catch-up sync in progress: at %d.%s (current tip slot is %d)", + i.syncStatus.SlotNumber, + i.syncStatus.BlockHash, + i.syncStatus.TipSlotNumber, + ), + ) + i.scheduleSyncStatusLog() +} + func (i *Indexer) LookupDomain(name string) *Domain { if domain, ok := i.domains[name]; ok { return &domain