Skip to content

fix: Remove combined flags file on restart #248

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 1 commit into from
Mar 21, 2025
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
28 changes: 27 additions & 1 deletion launchpad/pkg/filewatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,23 @@ import (
"sync"
)

var changeFlagLock sync.Mutex
var (
changeFlagLock sync.Mutex
workLock sync.Mutex
isPaused = false
)

func PauseFileWatcher() {
workLock.Lock()
defer workLock.Unlock()
isPaused = true
}

func ContinueFileWatcher() {
workLock.Lock()
defer workLock.Unlock()
isPaused = false
}

func StartFileWatcher() error {
watcher, err := fsnotify.NewWatcher()
Expand All @@ -24,9 +40,17 @@ func StartFileWatcher() error {
return
}
if event.Op&(fsnotify.Create|fsnotify.Write|fsnotify.Remove) != 0 {
workLock.Lock()
if isPaused {
workLock.Unlock()
fmt.Printf("%v config changed, but file watcher is paused \n", event.Name)
return
}
fmt.Printf("%v config changed, regenerating JSON...\n", event.Name)
if err := CombineJSONFiles(InputDir); err != nil {
fmt.Printf("Error combining JSON files: %v\n", err)
workLock.Unlock()
return
}
if strings.HasSuffix(event.Name, "changing-flag.json") {
changeFlagLock.Lock()
Expand All @@ -36,6 +60,8 @@ func StartFileWatcher() error {
changeFlagUpdateListeners = nil
changeFlagLock.Unlock()
}

workLock.Unlock()
}
case err, ok := <-watcher.Errors:
if !ok {
Expand Down
21 changes: 21 additions & 0 deletions launchpad/pkg/flagd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package flagd
import (
"bufio"
"context"
"errors"
"fmt"
"io"
"os"
"os/exec"
"strings"
"sync"
Expand All @@ -18,6 +20,21 @@ var (
restartCancelFunc context.CancelFunc // Stores the cancel function for delayed restarts
)

func ensureStartConditions() {
if _, err := os.Stat(OutputFile); errors.Is(err, os.ErrNotExist) {
err := CombineJSONFiles(InputDir)
if err != nil {
fmt.Printf("Error combining JSON files on flagd start: %v\n", err)
}
}
ContinueFileWatcher()
}

func deleteCombinedFlagsFile() {
// if we cannot delete it, we can assume it did not exist in the first place, so we can ignore this error
_ = os.Remove(OutputFile)
}

func RestartFlagd(seconds int) {
flagdLock.Lock()
if restartCancelFunc != nil {
Expand All @@ -28,6 +45,8 @@ func RestartFlagd(seconds int) {
ctx, cancel := context.WithCancel(context.Background())
restartCancelFunc = cancel

PauseFileWatcher()
deleteCombinedFlagsFile()
err := stopFlagDWithoutLock()
if err != nil {
fmt.Printf("Failed to restart flagd: %v\n", err)
Expand Down Expand Up @@ -69,6 +88,8 @@ func StartFlagd(config string) error {
return err
}

ensureStartConditions()

configPath := fmt.Sprintf("./configs/%s.json", config)

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