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

refactor: move consumer config to cronsumer struct #12

Merged
merged 1 commit into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func main() {
}

cronsumer := kcronsumer.NewCronsumer(kafkaConfig, consumeFn)
cronsumer.Run(kafkaConfig.Consumer)
cronsumer.Run()
}
```

Expand All @@ -62,7 +62,7 @@ func main() {
}

cronsumer := kcronsumer.NewCronsumer(kafkaConfig, consumeFn)
cronsumer.Run(kafkaConfig.Consumer)
cronsumer.Run()
}
```

Expand All @@ -76,14 +76,14 @@ func main() {
return nil
}
firstHandler := kcronsumer.NewCronsumer(firstCfg, firstConsumerFn)
firstHandler.Start(firstCfg.Consumer)
firstHandler.Start()

var secondConsumerFn kcronsumer.ConsumeFn = func(message model.Message) error {
fmt.Printf("Second consumer > Message received: %s\n", string(message.Value))
return nil
}
secondHandler := kcronsumer.NewCronsumer(secondCfg, secondConsumerFn)
secondHandler.Start(firstCfg.Consumer)
secondHandler.Start()
// ...
}
```
Expand Down
4 changes: 2 additions & 2 deletions cronsumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
type ConsumeFn func(message model.Message) error

type Cronsumer interface {
Start(cfg model.ConsumerConfig)
Run(cfg model.ConsumerConfig)
Start()
Run()
Stop()
}

Expand Down
4 changes: 2 additions & 2 deletions example/multiple-consumers/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ func main() {
return nil
}
firstHandler := kcronsumer.NewCronsumer(firstCfg, firstConsumerFn)
firstHandler.Start(firstCfg.Consumer)
firstHandler.Start()

var secondConsumerFn kcronsumer.ConsumeFn = func(message model.Message) error {
fmt.Printf("Second consumer > Message received: %s\n", string(message.Value))
return nil
}
secondHandler := kcronsumer.NewCronsumer(secondCfg, secondConsumerFn)
secondHandler.Start(firstCfg.Consumer)
secondHandler.Start()

select {} // block main goroutine (we did to show it by on purpose)
}
Expand Down
2 changes: 1 addition & 1 deletion example/single-consumer-with-deadletter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func main() {
}

cronsumer := kcronsumer.NewCronsumer(kafkaConfig, consumeFn)
cronsumer.Run(kafkaConfig.Consumer)
cronsumer.Run()
}

func getConfig() *model.KafkaConfig {
Expand Down
2 changes: 1 addition & 1 deletion example/single-consumer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func main() {
}

cronsumer := kcronsumer.NewCronsumer(kafkaConfig, consumeFn)
cronsumer.Run(kafkaConfig.Consumer)
cronsumer.Run()
}

func getConfig() *model.KafkaConfig {
Expand Down
13 changes: 9 additions & 4 deletions internal/cronsumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (
)

type Cronsumer interface {
Start(cfg model.ConsumerConfig)
Run(cfg model.ConsumerConfig)
Start()
Run()
Stop()
}

type cronsumer struct {
cfg *model.KafkaConfig
cron *gocron.Cron
consumer KafkaCronsumer
logger model.Logger
Expand All @@ -30,6 +31,7 @@ func NewCronsumer(cfg *model.KafkaConfig, c func(message model.Message) error) C
cron: gocron.New(),
consumer: consumer,
logger: l,
cfg: cfg,
}
}

Expand All @@ -40,11 +42,13 @@ func NewCronsumerWithLogger(cfg *model.KafkaConfig, c func(m model.Message) erro
cron: gocron.New(),
consumer: consumer,
logger: l,
cfg: cfg,
}
}

// Start starts the kafka consumer KafkaCronsumer with a new goroutine so its asynchronous operation (non-blocking)
func (s *cronsumer) Start(cfg model.ConsumerConfig) {
func (s *cronsumer) Start() {
cfg := s.cfg.Consumer
checkRequiredParams(cfg)
_, _ = s.cron.AddFunc(cfg.Cron, func() {
s.logger.Info("Topic started at time: " + time.Now().String())
Expand All @@ -55,7 +59,8 @@ func (s *cronsumer) Start(cfg model.ConsumerConfig) {
}

// Run runs the kafka consumer KafkaCronsumer with the caller goroutine so its synchronous operation (blocking)
func (s *cronsumer) Run(cfg model.ConsumerConfig) {
func (s *cronsumer) Run() {
cfg := s.cfg.Consumer
checkRequiredParams(cfg)
_, _ = s.cron.AddFunc(cfg.Cron, func() {
s.logger.Info("Topic started at time: " + time.Now().String())
Expand Down