-
Notifications
You must be signed in to change notification settings - Fork 0
/
wait_topic.go
68 lines (56 loc) · 1.71 KB
/
wait_topic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"log/slog"
"os"
"time"
"github.com/spf13/cobra"
)
var expected int
// topicCmd represents the topic command
var waitTopicCmd = &cobra.Command{
Use: "topic",
Aliases: []string{"to", "topics"},
Short: "Wait for kafka topics produced by the fink-broker",
Run: func(cmd *cobra.Command, args []string) {
start := time.Now()
slog.Info("Wait for kafka topics produced by the fink-broker")
// Channel to signal when the condition is met
allTopicsFound := make(chan bool, 1)
// Start a goroutine to check the condition
go func() {
for {
topics, err := getFinkTopics()
cobra.CheckErr(err)
t := time.Now()
elapsed := t.Sub(start)
slog.Info("Found topics", "elapsedTime", elapsed.Round(1), "topics", topics, "topicsCount", len(topics), "expected", expected)
if len(topics) == expected {
allTopicsFound <- true
return
}
time.Sleep(time.Second * 10) // Adjust the sleep duration as needed
}
}()
select {
case <-allTopicsFound:
fmt.Printf("Condition met: Found %d fink topics.", expected)
case <-time.After(timeout):
fmt.Fprintf(os.Stderr, "error: timed out waiting for the condition on topics")
os.Exit(1)
}
},
}
func init() {
waitCmd.AddCommand(waitTopicCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// topicCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
waitTopicCmd.Flags().IntVarP(&expected, "expected", "e", 10, "Number of expected topics")
}