Skip to content

Commit

Permalink
capture ctrl-c and close GroupConsumer
Browse files Browse the repository at this point in the history
  • Loading branch information
childe committed Jul 15, 2024
1 parent 0044ad0 commit af4db29
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions command/cmd/group-consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"errors"
"fmt"
"math"
"os"
"os/signal"
"syscall"

"github.com/childe/healer"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -59,25 +62,30 @@ var groupConsumerCmd = &cobra.Command{
if err != nil {
return err
}
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGINT)

defer consumer.Close()

messages, err := consumer.Consume(nil)
if err != nil {
return err
}

if maxMessages <= 0 {
for {
message := <-messages
fmt.Printf("%d: %s\n", message.Message.Offset, message.Message.Value)
}
} else {
for i := 0; i < maxMessages; i++ {
i := 0
for {
select {
case <-sigChan:
return nil
default:
message := <-messages
fmt.Printf("%d: %s\n", message.Message.Offset, message.Message.Value)
i++
if maxMessages > 0 && i >= maxMessages {
return nil
}
}
}
return nil
},
}

Expand Down

0 comments on commit af4db29

Please sign in to comment.