From 70255ca8de7f6b304f349dfc913e73cb64e735bc Mon Sep 17 00:00:00 2001 From: maddalax Date: Wed, 18 Sep 2024 22:44:54 -0500 Subject: [PATCH] keep track of tailwind process so we can clear it --- cli/signals.go | 1 + cli/tasks/css/css.go | 2 +- cli/tasks/process/process.go | 51 +++++++++++++++++++++++----------- cli/tasks/reloader/reloader.go | 2 +- 4 files changed, 38 insertions(+), 18 deletions(-) diff --git a/cli/signals.go b/cli/signals.go index 2ec875f..b4ccdee 100644 --- a/cli/signals.go +++ b/cli/signals.go @@ -22,6 +22,7 @@ func RegisterSignals() chan bool { fmt.Println() fmt.Println("Received signal:", sig) // Perform cleanup + fmt.Println("Cleaning up...") process.KillAll() // Signal that cleanup is done done <- true diff --git a/cli/tasks/css/css.go b/cli/tasks/css/css.go index c3fd896..1e8a20a 100644 --- a/cli/tasks/css/css.go +++ b/cli/tasks/css/css.go @@ -12,5 +12,5 @@ func GenerateCss(flags ...process.RunFlag) error { func GenerateCssWatch(flags ...process.RunFlag) error { return process.RunMany([]string{ "./assets/css/tailwindcss -i ./assets/css/input.css -o ./assets/dist/main.css -c ./assets/css/tailwind.config.js --watch=always", - }, append(flags, process.DontTrack, process.Silent)...) + }, append(flags, process.KillOnlyOnExit, process.Silent)...) } diff --git a/cli/tasks/process/process.go b/cli/tasks/process/process.go index bfe75fd..27002af 100644 --- a/cli/tasks/process/process.go +++ b/cli/tasks/process/process.go @@ -13,12 +13,17 @@ import ( "time" ) -var workingDir string -var commands = make([]*exec.Cmd, 0) +type CmdWithFlags struct { + flags []RunFlag + cmd *exec.Cmd +} -func AppendRunning(cmd *exec.Cmd) { +var workingDir string +var commands = make([]CmdWithFlags, 0) + +func AppendRunning(cmd *exec.Cmd, flags ...RunFlag) { slog.Debug("running", slog.String("command", strings.Join(cmd.Args, " "))) - commands = append(commands, cmd) + commands = append(commands, CmdWithFlags{flags: flags, cmd: cmd}) } func GetWorkingDir() string { @@ -37,18 +42,27 @@ func GetPathRelativeToCwd(path string) string { return filepath.Join(GetWorkingDir(), path) } -func KillAll() { +func shouldSkipKilling(flags []RunFlag, skipFlag []RunFlag) bool { + for _, flag := range flags { + if slices.Contains(skipFlag, flag) { + return true + } + } + return false +} + +func KillAll(skipFlag ...RunFlag) { tries := 0 for { tries++ allFinished := true for _, cmd := range commands { - if cmd.Process == nil { + if cmd.cmd.Process == nil { allFinished = false if tries > 50 { - args := strings.Join(cmd.Args, " ") + args := strings.Join(cmd.cmd.Args, " ") slog.Debug("process %v is not running after 50 tries, breaking.\n", args) allFinished = true break @@ -64,7 +78,10 @@ func KillAll() { } for _, command := range commands { - pid := command.Process.Pid + if shouldSkipKilling(command.flags, skipFlag) { + continue + } + pid := command.cmd.Process.Pid err := syscall.Kill(-pid, syscall.SIGKILL) if err != nil { continue @@ -74,15 +91,18 @@ func KillAll() { for { finished := true for _, c := range commands { - if c.Process == nil { + if c.cmd.Process == nil { continue } - exists, err := PidExists(int32(c.Process.Pid)) + if shouldSkipKilling(c.flags, skipFlag) { + continue + } + exists, err := PidExists(int32(c.cmd.Process.Pid)) if err != nil { finished = false } if exists { - syscall.Kill(-c.Process.Pid, syscall.SIGKILL) + syscall.Kill(-c.cmd.Process.Pid, syscall.SIGKILL) finished = false } } @@ -95,7 +115,7 @@ func KillAll() { } } - commands = make([]*exec.Cmd, 0) + commands = make([]CmdWithFlags, 0) slog.Debug("all processes killed\n") } @@ -137,7 +157,7 @@ type RunFlag int const ( ExitOnError RunFlag = iota Silent - DontTrack + KillOnlyOnExit ) func RunMany(commands []string, flags ...RunFlag) error { @@ -170,9 +190,8 @@ func Run(command string, flags ...RunFlag) error { cmd.Dir = workingDir } - if !slices.Contains(flags, DontTrack) { - AppendRunning(cmd) - } + AppendRunning(cmd, flags...) + err := cmd.Run() if err == nil { diff --git a/cli/tasks/reloader/reloader.go b/cli/tasks/reloader/reloader.go index 62309b9..49fd037 100644 --- a/cli/tasks/reloader/reloader.go +++ b/cli/tasks/reloader/reloader.go @@ -132,7 +132,7 @@ func OnFileChange(events []*fsnotify.Event) { if tasks.Run { util.Trace("kill all processes", func() any { - process.KillAll() + process.KillAll(process.KillOnlyOnExit) return nil }) }