-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature / enable&disable duration (#81)
* feat: naming change * feat: duration validation disabled * feat: integration test added * feat: nonstopwork var defined for duration controls * feat: modify readme * feat: renaming and goroutine leak fix * feat: integration test topic name changed * feat: linter fix
- Loading branch information
1 parent
6066a2b
commit 741d6f4
Showing
9 changed files
with
195 additions
and
138 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package internal | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
|
||
"github.com/Trendyol/kafka-cronsumer/pkg/kafka" | ||
|
||
"github.com/Trendyol/kafka-cronsumer/pkg/logger" | ||
|
||
gocron "github.com/robfig/cron/v3" | ||
) | ||
|
||
type cronsumerClient struct { | ||
cfg *kafka.Config | ||
cron *gocron.Cron | ||
consumer *cronsumer | ||
metricCollectors []prometheus.Collector | ||
} | ||
|
||
func NewCronsumer(cfg *kafka.Config, fn kafka.ConsumeFn) kafka.Cronsumer { | ||
c := newCronsumer(cfg, fn) | ||
|
||
return &cronsumerClient{ | ||
cron: gocron.New(), | ||
consumer: c, | ||
cfg: cfg, | ||
metricCollectors: []prometheus.Collector{NewCollector(cfg.MetricPrefix, c.metric)}, | ||
} | ||
} | ||
|
||
func (s *cronsumerClient) WithLogger(logger logger.Interface) { | ||
s.cfg.Logger = logger | ||
} | ||
|
||
func (s *cronsumerClient) Start() { | ||
s.setup() | ||
s.cron.Start() | ||
} | ||
|
||
func (s *cronsumerClient) Run() { | ||
s.setup() | ||
s.cron.Run() | ||
} | ||
|
||
func (s *cronsumerClient) Stop() { | ||
s.cron.Stop() | ||
s.consumer.Stop() | ||
} | ||
|
||
func (s *cronsumerClient) Produce(message kafka.Message) error { | ||
return s.consumer.kafkaProducer.Produce(message) | ||
} | ||
|
||
func (s *cronsumerClient) ProduceBatch(messages []kafka.Message) error { | ||
return s.consumer.kafkaProducer.ProduceBatch(messages) | ||
} | ||
|
||
func (s *cronsumerClient) GetMetricCollectors() []prometheus.Collector { | ||
return s.metricCollectors | ||
} | ||
|
||
func (s *cronsumerClient) setup() { | ||
cfg := s.cfg.Consumer | ||
|
||
s.consumer.SetupConcurrentWorkers(cfg.Concurrency) | ||
schedule, err := gocron.ParseStandard(cfg.Cron) | ||
if err != nil { | ||
panic("Cron parse error: " + err.Error()) | ||
} | ||
|
||
_, _ = s.cron.AddFunc(cfg.Cron, func() { | ||
cancelFuncWrapper := s.startListen(cfg) | ||
if cfg.Duration == kafka.NonStopWork { | ||
now := time.Now() | ||
nextRun := schedule.Next(now) | ||
duration := nextRun.Sub(now) | ||
time.AfterFunc(duration, cancelFuncWrapper) | ||
} else { | ||
time.AfterFunc(cfg.Duration, cancelFuncWrapper) | ||
} | ||
}) | ||
} | ||
|
||
func (s *cronsumerClient) startListen(cfg kafka.ConsumerConfig) func() { | ||
s.cfg.Logger.Debug("Consuming " + cfg.Topic + " started at time: " + time.Now().String()) | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
cancelFuncWrapper := func() { | ||
s.cfg.Logger.Debug("Consuming " + cfg.Topic + " paused at " + time.Now().String()) | ||
cancel() | ||
} | ||
|
||
go s.consumer.Listen(ctx, cfg.BackOffStrategy.String(), &cancelFuncWrapper) | ||
return cancelFuncWrapper | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters