|
1 | 1 | package flagd |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "bufio" |
5 | 4 | "context" |
6 | 5 | "errors" |
7 | 6 | "fmt" |
8 | | - "io" |
| 7 | + "net/http" |
9 | 8 | "os" |
10 | 9 | "os/exec" |
11 | | - "strings" |
12 | 10 | "sync" |
13 | 11 | "time" |
14 | 12 | ) |
@@ -95,39 +93,36 @@ func StartFlagd(config string) error { |
95 | 93 | configPath := fmt.Sprintf("./configs/%s.json", config) |
96 | 94 |
|
97 | 95 | flagdCmd = exec.Command("./flagd", "start", "--config", configPath) |
98 | | - |
99 | | - stdout, err := flagdCmd.StdoutPipe() |
100 | | - if err != nil { |
101 | | - return fmt.Errorf("failed to capture stdout: %v", err) |
102 | | - } |
103 | | - stderr, err := flagdCmd.StderrPipe() |
104 | | - if err != nil { |
105 | | - return fmt.Errorf("failed to capture stderr: %v", err) |
106 | | - } |
| 96 | + flagdCmd.Stdout = os.Stdout |
| 97 | + flagdCmd.Stderr = os.Stderr |
107 | 98 |
|
108 | 99 | if err := flagdCmd.Start(); err != nil { |
| 100 | + flagdLock.Unlock() |
109 | 101 | return fmt.Errorf("failed to start flagd: %v", err) |
110 | 102 | } |
111 | | - |
112 | 103 | flagdLock.Unlock() |
113 | | - ready := make(chan bool) |
114 | 104 |
|
115 | | - go monitorOutput(stdout, ready, "stdout") |
116 | | - go monitorOutput(stderr, ready, "stderr") |
| 105 | + // Poll health endpoint until ready |
| 106 | + client := &http.Client{Timeout: 500 * time.Millisecond} |
| 107 | + ticker := time.NewTicker(100 * time.Millisecond) |
| 108 | + defer ticker.Stop() |
| 109 | + timeout := time.After(10 * time.Second) |
117 | 110 |
|
118 | | - select { |
119 | | - case success := <-ready: |
120 | | - if success { |
121 | | - fmt.Println("flagd started successfully.") |
122 | | - return nil |
123 | | - } |
124 | | - return fmt.Errorf("flagd did not start correctly") |
125 | | - case <-time.After(10 * time.Second): |
126 | | - err := StopFlagd() |
127 | | - if err != nil { |
128 | | - fmt.Println("could not stop flagd", err) |
| 111 | + for { |
| 112 | + select { |
| 113 | + case <-timeout: |
| 114 | + _ = StopFlagd() |
| 115 | + return fmt.Errorf("flagd health check timed out") |
| 116 | + case <-ticker.C: |
| 117 | + resp, err := client.Get("http://localhost:8014/readyz") |
| 118 | + if err == nil { |
| 119 | + resp.Body.Close() |
| 120 | + if resp.StatusCode == http.StatusOK { |
| 121 | + fmt.Println("flagd started successfully.") |
| 122 | + return nil |
| 123 | + } |
| 124 | + } |
129 | 125 | } |
130 | | - return fmt.Errorf("flagd start timeout exceeded") |
131 | 126 | } |
132 | 127 | } |
133 | 128 |
|
@@ -174,23 +169,3 @@ func stopFlagDWithoutLock() error { |
174 | 169 | } |
175 | 170 | return nil |
176 | 171 | } |
177 | | - |
178 | | -func monitorOutput(pipe io.ReadCloser, ready chan bool, stream string) { |
179 | | - scanner := bufio.NewScanner(pipe) |
180 | | - //adjust the capacity to your need (max characters in line) |
181 | | - const maxCapacity = 512 * 1024 |
182 | | - buf := make([]byte, maxCapacity) |
183 | | - scanner.Buffer(buf, maxCapacity) |
184 | | - started := false |
185 | | - |
186 | | - for scanner.Scan() { |
187 | | - line := scanner.Text() |
188 | | - fmt.Println("[flagd ", stream, "]:", line) |
189 | | - if ready != nil && !started && strings.Contains(line, "listening at") { |
190 | | - ready <- true |
191 | | - close(ready) |
192 | | - fmt.Println("flagd started properly found logline with 'listening at'") |
193 | | - started = true |
194 | | - } |
195 | | - } |
196 | | -} |
0 commit comments