keep track of tailwind process so we can clear it

This commit is contained in:
maddalax 2024-09-18 22:44:54 -05:00
parent b610aefa36
commit 70255ca8de
4 changed files with 38 additions and 18 deletions

View file

@ -22,6 +22,7 @@ func RegisterSignals() chan bool {
fmt.Println() fmt.Println()
fmt.Println("Received signal:", sig) fmt.Println("Received signal:", sig)
// Perform cleanup // Perform cleanup
fmt.Println("Cleaning up...")
process.KillAll() process.KillAll()
// Signal that cleanup is done // Signal that cleanup is done
done <- true done <- true

View file

@ -12,5 +12,5 @@ func GenerateCss(flags ...process.RunFlag) error {
func GenerateCssWatch(flags ...process.RunFlag) error { func GenerateCssWatch(flags ...process.RunFlag) error {
return process.RunMany([]string{ 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", "./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)...)
} }

View file

@ -13,12 +13,17 @@ import (
"time" "time"
) )
var workingDir string type CmdWithFlags struct {
var commands = make([]*exec.Cmd, 0) 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, " "))) 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 { func GetWorkingDir() string {
@ -37,18 +42,27 @@ func GetPathRelativeToCwd(path string) string {
return filepath.Join(GetWorkingDir(), path) 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 tries := 0
for { for {
tries++ tries++
allFinished := true allFinished := true
for _, cmd := range commands { for _, cmd := range commands {
if cmd.Process == nil { if cmd.cmd.Process == nil {
allFinished = false allFinished = false
if tries > 50 { 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) slog.Debug("process %v is not running after 50 tries, breaking.\n", args)
allFinished = true allFinished = true
break break
@ -64,7 +78,10 @@ func KillAll() {
} }
for _, command := range commands { 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) err := syscall.Kill(-pid, syscall.SIGKILL)
if err != nil { if err != nil {
continue continue
@ -74,15 +91,18 @@ func KillAll() {
for { for {
finished := true finished := true
for _, c := range commands { for _, c := range commands {
if c.Process == nil { if c.cmd.Process == nil {
continue 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 { if err != nil {
finished = false finished = false
} }
if exists { if exists {
syscall.Kill(-c.Process.Pid, syscall.SIGKILL) syscall.Kill(-c.cmd.Process.Pid, syscall.SIGKILL)
finished = false finished = false
} }
} }
@ -95,7 +115,7 @@ func KillAll() {
} }
} }
commands = make([]*exec.Cmd, 0) commands = make([]CmdWithFlags, 0)
slog.Debug("all processes killed\n") slog.Debug("all processes killed\n")
} }
@ -137,7 +157,7 @@ type RunFlag int
const ( const (
ExitOnError RunFlag = iota ExitOnError RunFlag = iota
Silent Silent
DontTrack KillOnlyOnExit
) )
func RunMany(commands []string, flags ...RunFlag) error { func RunMany(commands []string, flags ...RunFlag) error {
@ -170,9 +190,8 @@ func Run(command string, flags ...RunFlag) error {
cmd.Dir = workingDir cmd.Dir = workingDir
} }
if !slices.Contains(flags, DontTrack) { AppendRunning(cmd, flags...)
AppendRunning(cmd)
}
err := cmd.Run() err := cmd.Run()
if err == nil { if err == nil {

View file

@ -132,7 +132,7 @@ func OnFileChange(events []*fsnotify.Event) {
if tasks.Run { if tasks.Run {
util.Trace("kill all processes", func() any { util.Trace("kill all processes", func() any {
process.KillAll() process.KillAll(process.KillOnlyOnExit)
return nil return nil
}) })
} }