Skip to content

Commit

Permalink
fix: allow cmd to exit gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhas07 committed May 15, 2024
1 parent d9a13c4 commit 3cf08c7
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions cmd/templ/generatecmd/run/run_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,48 @@ import (
"strings"
"sync"
"syscall"
"time"
)

var m = &sync.Mutex{}
var running = map[string]*exec.Cmd{}
var (
m = &sync.Mutex{}
running = map[string]*exec.Cmd{}
)

func KillAll() (err error) {
m.Lock()
defer m.Unlock()
for _, cmd := range running {
err := syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
err := syscall.Kill(-cmd.Process.Pid, syscall.SIGTERM)
if err != nil {
return err
}
time.Sleep(1 * time.Second)
if err = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL); err != nil {
return err
}
}
running = map[string]*exec.Cmd{}
return
}

func Stop(cmd *exec.Cmd) (err error) {
return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
return syscall.Kill(-cmd.Process.Pid, syscall.SIGTERM)
}

func Run(ctx context.Context, workingDir, input string) (cmd *exec.Cmd, err error) {
m.Lock()
defer m.Unlock()
cmd, ok := running[input]
if ok {
if err = syscall.Kill(-cmd.Process.Pid, syscall.SIGTERM); err != nil {
return cmd, err
}
time.Sleep(1 * time.Second)
if err = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL); err != nil {
return cmd, err
}

delete(running, input)
}
parts := strings.Fields(input)
Expand Down

0 comments on commit 3cf08c7

Please sign in to comment.