Skip to content
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.idea
node_modules
bin/
# dynamically generated flags
flags/allFlags.json
3 changes: 2 additions & 1 deletion launchpad/pkg/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ func ToggleChangingFlag() (string, error) {

// Write the updated JSON back to the file
fileLock.Lock()
if err := os.WriteFile(configFile, updatedData, 0644); err != nil {
if err := atomicWriteFile(configFile, updatedData); err != nil {
fileLock.Unlock()
return "", err
}
fileLock.Unlock()
Expand Down
71 changes: 23 additions & 48 deletions launchpad/pkg/flagd.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package flagd

import (
"bufio"
"context"
"errors"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"strings"
"sync"
"time"
)
Expand Down Expand Up @@ -95,39 +93,36 @@ func StartFlagd(config string) error {
configPath := fmt.Sprintf("./configs/%s.json", config)

flagdCmd = exec.Command("./flagd", "start", "--config", configPath)

stdout, err := flagdCmd.StdoutPipe()
if err != nil {
return fmt.Errorf("failed to capture stdout: %v", err)
}
stderr, err := flagdCmd.StderrPipe()
if err != nil {
return fmt.Errorf("failed to capture stderr: %v", err)
}
flagdCmd.Stdout = os.Stdout
flagdCmd.Stderr = os.Stderr

if err := flagdCmd.Start(); err != nil {
flagdLock.Unlock()
return fmt.Errorf("failed to start flagd: %v", err)
}

flagdLock.Unlock()
ready := make(chan bool)

go monitorOutput(stdout, ready, "stdout")
go monitorOutput(stderr, ready, "stderr")
// Poll health endpoint until ready
Copy link
Member Author

@toddbaert toddbaert Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The log output we were looking at doesn't actually mean the listening services are running - that statement is printed before the servers are guaranteed to be up.

The healthcheck actually waits until all services are up, so let's use that.

client := &http.Client{Timeout: 500 * time.Millisecond}
ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()
timeout := time.After(10 * time.Second)

select {
case success := <-ready:
if success {
fmt.Println("flagd started successfully.")
return nil
}
return fmt.Errorf("flagd did not start correctly")
case <-time.After(10 * time.Second):
err := StopFlagd()
if err != nil {
fmt.Println("could not stop flagd", err)
for {
select {
case <-timeout:
_ = StopFlagd()
return fmt.Errorf("flagd health check timed out")
case <-ticker.C:
resp, err := client.Get("http://localhost:8014/readyz")
if err == nil {
resp.Body.Close()
if resp.StatusCode == http.StatusOK {
fmt.Println("flagd started successfully.")
return nil
}
}
}
return fmt.Errorf("flagd start timeout exceeded")
}
}

Expand Down Expand Up @@ -174,23 +169,3 @@ func stopFlagDWithoutLock() error {
}
return nil
}

func monitorOutput(pipe io.ReadCloser, ready chan bool, stream string) {
scanner := bufio.NewScanner(pipe)
//adjust the capacity to your need (max characters in line)
const maxCapacity = 512 * 1024
buf := make([]byte, maxCapacity)
scanner.Buffer(buf, maxCapacity)
started := false

for scanner.Scan() {
line := scanner.Text()
fmt.Println("[flagd ", stream, "]:", line)
if ready != nil && !started && strings.Contains(line, "listening at") {
ready <- true
close(ready)
fmt.Println("flagd started properly found logline with 'listening at'")
started = true
}
}
}
29 changes: 28 additions & 1 deletion launchpad/pkg/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,33 @@ import (
"sync"
)

// writes a file atomically using mv
func atomicWriteFile(filename string, data []byte) error {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This atomically writes the file, so there's never any change of half-written data. We do something similar in OFO.

tmpFile, err := os.CreateTemp(filepath.Dir(filename), ".tmp-*")
if err != nil {
return err
}
tmpName := tmpFile.Name()

if _, err := tmpFile.Write(data); err != nil {
tmpFile.Close()
os.Remove(tmpName)
return err
}

if err := tmpFile.Close(); err != nil {
os.Remove(tmpName)
return err
}

if err := os.Rename(tmpName, filename); err != nil {
os.Remove(tmpName)
return err
}

return nil
}

var (
mu sync.Mutex
InputDir = "./rawflags"
Expand Down Expand Up @@ -54,7 +81,7 @@ func CombineJSONFiles(inputDir string) error {
return fmt.Errorf("failed to serialize combined JSON: %v", err)
}

return ioutil.WriteFile(OutputFile, combinedContent, 0644)
return atomicWriteFile(OutputFile, combinedContent)
}

func deepMerge(dst, src map[string]interface{}) map[string]interface{} {
Expand Down