Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
gusin13 committed Dec 10, 2024
1 parent aac2260 commit 27130c4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
19 changes: 5 additions & 14 deletions cmd/staking-expiry-checker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,25 +65,16 @@ func main() {
log.Fatal().Err(err).Msg("error while creating service")
}

// Create expiry poller
expiryPoller, err := poller.NewPoller(
poller.ExpiryPoller,
// Even though we pass service, it's viewed only through the specific interface
expiryPoller := poller.NewExpiryPoller(
cfg.Pollers.ExpiryChecker,
service.ProcessExpiredDelegations,
service, // service implements ExpiryChecker
)
if err != nil {
log.Fatal().Err(err).Msg("error while creating expiry poller")
}

// Create BTC subscriber poller
btcSubscriberPoller, err := poller.NewPoller(
poller.BTCSubscriberPoller,
btcSubscriberPoller := poller.NewBTCSubscriberPoller(
cfg.Pollers.BtcSubscriber,
service.ProcessBTCSubscriber,
service, // service implements BTCSubscriber
)
if err != nil {
log.Fatal().Err(err).Msg("error while creating BTC subscriber poller")
}

// Start pollers in separate goroutines
go expiryPoller.Start(ctx)
Expand Down
32 changes: 26 additions & 6 deletions internal/poller/poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ import (
"github.com/babylonlabs-io/staking-expiry-checker/internal/types"
)

// Define minimal interfaces for each poller type
type ExpiryChecker interface {
ProcessExpiredDelegations(ctx context.Context) *types.Error
}

type BTCSubscriber interface {
ProcessBTCSubscriber(ctx context.Context) *types.Error
}

type PollerType string

const (
Expand All @@ -21,20 +30,31 @@ type PollerOperation func(ctx context.Context) *types.Error

type Poller struct {
pollerType PollerType
operation PollerOperation
poll func(ctx context.Context) *types.Error
interval time.Duration
timeout time.Duration
quit chan struct{}
}

func NewPoller(pollerType PollerType, cfg config.PollerConfig, operation PollerOperation) (*Poller, error) {
// Constructors now accept interfaces instead of the full service
func NewExpiryPoller(cfg config.PollerConfig, checker ExpiryChecker) *Poller {
return &Poller{
pollerType: ExpiryPoller,
interval: cfg.Interval,
timeout: cfg.Timeout,
poll: checker.ProcessExpiredDelegations,
quit: make(chan struct{}),
}
}

func NewBTCSubscriberPoller(cfg config.PollerConfig, subscriber BTCSubscriber) *Poller {
return &Poller{
pollerType: pollerType,
operation: operation,
pollerType: BTCSubscriberPoller,
interval: cfg.Interval,
timeout: cfg.Timeout,
poll: subscriber.ProcessBTCSubscriber,
quit: make(chan struct{}),
}, nil
}
}

func (p *Poller) Start(ctx context.Context) {
Expand All @@ -47,7 +67,7 @@ func (p *Poller) Start(ctx context.Context) {
pollingCtx, cancel := context.WithTimeout(ctx, p.timeout)
defer cancel()

if err := p.operation(pollingCtx); err != nil {
if err := p.poll(pollingCtx); err != nil {
log.Error().
Err(err).
Str("poller", string(p.pollerType)).
Expand Down

0 comments on commit 27130c4

Please sign in to comment.