mirror of
https://github.com/3ybactuk/marketplace-go-service-project.git
synced 2025-10-30 22:13:44 +03:00
43 lines
723 B
Go
43 lines
723 B
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
type StatusConsumer interface {
|
|
FetchEvents(ctx context.Context) error
|
|
}
|
|
|
|
type NotifierService struct {
|
|
consumer StatusConsumer
|
|
}
|
|
|
|
func NewNotifierService(consumer StatusConsumer) *NotifierService {
|
|
return &NotifierService{
|
|
consumer: consumer,
|
|
}
|
|
}
|
|
|
|
func (s *NotifierService) RunFetchEvents(ctx context.Context) error {
|
|
backoff := 1 * time.Second
|
|
|
|
for {
|
|
if err := s.consumer.FetchEvents(ctx); err != nil {
|
|
if ctx.Err() != nil {
|
|
return ctx.Err()
|
|
}
|
|
|
|
log.Error().Err(err).Msgf("consume error (retrying in %d second(s))", backoff)
|
|
}
|
|
|
|
select {
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
case <-time.After(backoff):
|
|
}
|
|
}
|
|
}
|