Skip to content

feat: listening for logs before returning a http status code #230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions launchpad/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Ignore local test files
flagd
/flags/
/rawflags/
Binary file removed launchpad/flagd
Binary file not shown.
68 changes: 59 additions & 9 deletions launchpad/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bufio"
"context"
"encoding/json"
"fmt"
Expand All @@ -12,6 +13,7 @@ import (
"os/signal"
"path/filepath"
"strconv"
"strings"
"sync"
"syscall"
"time"
Expand All @@ -29,8 +31,11 @@ var (
)

func stopFlagd() error {
flagdLock.Lock()
defer flagdLock.Unlock()
// this is not ideal, as start flagd might already lock we need to try here, and only unlock if it works
// definitely room for improvement
if flagdLock.TryLock() {
defer flagdLock.Unlock()
}

if flagdCmd != nil && flagdCmd.Process != nil {
if err := flagdCmd.Process.Kill(); err != nil {
Expand Down Expand Up @@ -60,6 +65,7 @@ func startFlagd(config string) error {
currentConfig = config // Update the current configuration
}

flagdLock.Lock()
// Stop any currently running flagd instance
if err := stopFlagd(); err != nil {
return err
Expand All @@ -68,18 +74,62 @@ func startFlagd(config string) error {
configPath := "./configs/" + config + ".json"

// Start a new instance
flagdLock.Lock()
defer flagdLock.Unlock()
flagdCmd = exec.Command("./flagd", "start", "--config", configPath)
flagdLock.Unlock() // 🔥 Unlock before logs start
// Set up the output of flagd to be printed to the current terminal (stdout)
flagdCmd.Stdout = os.Stdout
flagdCmd.Stderr = os.Stderr

// Capture stdout and stderr separately
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)
}
// Start the process
if err := flagdCmd.Start(); err != nil {
return err
return fmt.Errorf("failed to start flagd: %v", err)
}
// Create channels to signal readiness
ready := make(chan bool)

// Monitor stdout and stderr for a readiness signal
go func() {
scannerOut := bufio.NewScanner(stdout)
for scannerOut.Scan() {
fmt.Println("[flagd stdout]:", scannerOut.Text())
}
}()

go func() {
scannerErr := bufio.NewScanner(stderr)
for scannerErr.Scan() {
fmt.Println("[flagd stderr]:", scannerErr.Text())
line := scannerErr.Text()
fmt.Println("[flagd stderr]:", line)
if strings.Contains(line, "listening at") {
ready <- true
return
}
}
}()

// Wait for flagd to print the expected log or timeout
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): // Timeout
err := stopFlagd()
if err != nil {
fmt.Println(err)
}
return fmt.Errorf("flagd start timeout exceeded")
}
log.Println("started flagd with config ", currentConfig)
return nil
}

func stopFlagdHandler(w http.ResponseWriter, r *http.Request) {
Expand Down