Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: periodic log message during catch-up sync #181

Merged
merged 1 commit into from
Aug 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions internal/indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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")
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down