Skip to content

Commit

Permalink
refactor(examples): use correct ec
Browse files Browse the repository at this point in the history
  • Loading branch information
alebabai committed May 9, 2024
1 parent c0c6938 commit 40573d2
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 23 deletions.
20 changes: 13 additions & 7 deletions examples/confluent/cmd/consumer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,9 @@ func main() {
}

c, err := ckafka.NewConsumer(&ckafka.ConfigMap{
"bootstrap.servers": brokerAddr,
"group.id": consumer.KafkaGroupID,
"enable.auto.commit": true,
"allow.auto.create.topics": true,
"bootstrap.servers": brokerAddr,
"group.id": consumer.KafkaGroupID,
"enable.auto.commit": true,
})
if err != nil {
fatal(logger, fmt.Errorf("failed to init kafka consumer: %w", err))
Expand Down Expand Up @@ -119,17 +118,24 @@ func main() {
}
}()

sigc := make(chan os.Signal, 1)

go func() {
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM)
errc <- fmt.Errorf("%s", <-sigc)
}()

_ = logger.Log("msg", "application started")
_ = logger.Log("msg", "application stopped", "exit", <-errc)

select {
case sig := <-sigc:
_ = logger.Log("msg", "application stopped", "exit", sig)
case err := <-errc:
fatal(logger, err)
}
}

func fatal(logger log.Logger, err error) {
_ = level.Error(logger).Log("err", err)
_ = level.Error(logger).Log("msg", "application stopped by an error", "err", err)
os.Exit(1)
}
14 changes: 10 additions & 4 deletions examples/confluent/cmd/producer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,23 @@ func main() {
}
}()

sigc := make(chan os.Signal, 1)

go func() {
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM)
errc <- fmt.Errorf("%s", <-sigc)
}()

_ = logger.Log("msg", "application started")
_ = logger.Log("msg", "application stopped", "exit", <-errc)

select {
case sig := <-sigc:
_ = logger.Log("msg", "application stopped", "exit", sig)
case err := <-errc:
fatal(logger, err)
}
}

func fatal(logger log.Logger, err error) {
_ = logger.Log("err", err)
_ = level.Error(logger).Log("msg", "application stopped by an error", "err", err)
os.Exit(1)
}
4 changes: 2 additions & 2 deletions examples/confluent/compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ services:

consumer:
image: library/golang:1.22
restart: always
restart: on-failure
depends_on:
- kafka
environment:
Expand All @@ -42,7 +42,7 @@ services:

producer:
image: library/golang:1.22
restart: always
restart: on-failure
depends_on:
- kafka
environment:
Expand Down
14 changes: 10 additions & 4 deletions examples/sarama/cmd/consumer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,23 @@ func main() {
}
}()

sigc := make(chan os.Signal, 1)

go func() {
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM)
errc <- fmt.Errorf("%s", <-sigc)
}()

_ = logger.Log("msg", "application started")
_ = logger.Log("msg", "application stopped", "exit", <-errc)

select {
case sig := <-sigc:
_ = logger.Log("msg", "application stopped", "exit", sig)
case err := <-errc:
fatal(logger, err)
}
}

func fatal(logger log.Logger, err error) {
_ = level.Error(logger).Log("err", err)
_ = level.Error(logger).Log("msg", "application stopped by an error", "err", err)
os.Exit(1)
}
14 changes: 10 additions & 4 deletions examples/sarama/cmd/producer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,23 @@ func main() {
}
}()

sigc := make(chan os.Signal, 1)

go func() {
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM)
errc <- fmt.Errorf("%s", <-sigc)
}()

_ = logger.Log("msg", "application started")
_ = logger.Log("msg", "application stopped", "exit", <-errc)

select {
case sig := <-sigc:
_ = logger.Log("msg", "application stopped", "exit", sig)
case err := <-errc:
fatal(logger, err)
}
}

func fatal(logger log.Logger, err error) {
_ = logger.Log("err", err)
_ = level.Error(logger).Log("msg", "application stopped by an error", "err", err)
os.Exit(1)
}
4 changes: 2 additions & 2 deletions examples/sarama/compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ services:

consumer:
image: library/golang:1.22-alpine
restart: always
restart: on-failure
depends_on:
- kafka
environment:
Expand All @@ -42,7 +42,7 @@ services:

producer:
image: library/golang:1.22-alpine
restart: always
restart: on-failure
depends_on:
- kafka
environment:
Expand Down

0 comments on commit 40573d2

Please sign in to comment.