mirror of
https://github.com/3ybactuk/marketplace-go-service-project.git
synced 2025-10-30 14:03:45 +03:00
[hw-6] add notifier service, kafka
This commit is contained in:
42
notifier/internal/domain/service/service.go
Normal file
42
notifier/internal/domain/service/service.go
Normal file
@@ -0,0 +1,42 @@
|
||||
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):
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user