Skip to content
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

Process rewrite #15

Merged
merged 2 commits into from
Apr 24, 2024
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
11 changes: 8 additions & 3 deletions process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"fmt"
"io"
"log/slog"
"os/exec"
"sync"
)
Expand Down Expand Up @@ -60,15 +61,19 @@ func (pm *ProcessManager) GetExecutes() []string {

func printSubProcess(ctx context.Context, pipe io.ReadCloser) {
scanner := bufio.NewScanner(pipe)
defer pipe.Close()
for {
select {
case <-ctx.Done():
slog.Debug("Context closed, stopping printSubProcess")
return
default:
if scanner.Scan() {
fmt.Println(scanner.Text())
if !scanner.Scan() {
if err := scanner.Err(); err != nil {
slog.Debug("Scanner error", "err", err)
}
return
}
fmt.Println(scanner.Text())
}
}
}
16 changes: 12 additions & 4 deletions process/process_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ func (pm *ProcessManager) StartProcess(ctx context.Context, cancel context.Cance
cancel()
return
}
slog.Debug("Process completed closing context", "exec", p.Exec)
ctx.Done()
} else {
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
cmd.Stderr = os.Stderr
Expand All @@ -98,17 +100,22 @@ func (pm *ProcessManager) StartProcess(ctx context.Context, cancel context.Cance
// slog.Debug("Stored Process Context", "exec", p.Exec)

go func() {
errCh := make(chan error, 1)
go func() {
errCh <- cmd.Wait()
}()
select {
case <-processCtx.Done():
_ = syscall.Kill(-p.pid, syscall.SIGKILL)
case <-ctx.Done():
slog.Debug("Context closed", "exec", p.Exec)
_ = syscall.Kill(-p.pid, syscall.SIGKILL)
default:
err := cmd.Wait()
case err := <-errCh:
if err != nil {
cancel()
}
slog.Debug("Process Errored closing context", "exec", p.Exec)
ctx.Done()
delete(pm.Ctxs, p.Exec)
delete(pm.Cancels, p.Exec)
}
Expand All @@ -124,14 +131,15 @@ func (pm *ProcessManager) StartProcess(ctx context.Context, cancel context.Cance

func (pm *ProcessManager) KillProcesses() {
for _, p := range pm.Processes {
// slog.Debug("Killing Process", "exec", p.Exec, "pid", p.pid)
if p.pid != 0 {
_, err := os.FindProcess(p.pid)
if err != nil {
// slog.Debug("Process not running", "exec", p.Exec)
continue
}
syscall.Kill(-p.pid, syscall.SIGKILL)
if cancel, ok := pm.Cancels[p.Exec]; ok {
cancel()
}
}
}
}
13 changes: 6 additions & 7 deletions process/process_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,15 @@ func (pm *ProcessManager) StartProcess(ctx context.Context, cancel context.Cance
return
}
} else {
cmd.Stderr = os.Stderr
p.logPipe, err = cmd.StdoutPipe()
go printSubProcess(ctx, p.logPipe)
err = cmd.Start()
if err != nil {
slog.Error("Getting Stdout Pipe", "exec", p.Exec, "err", err)
}
if cmd.Process != nil {
cmd.Stderr = os.Stderr
p.logPipe, err = cmd.StdoutPipe()
if err != nil {
slog.Error("Getting Stdout Pipe", "exec", p.Exec, "err", err)
}
go printSubProcess(ctx, p.logPipe)
p.pid = cmd.Process.Pid

processCtx, processCancel := context.WithCancel(ctx)
pm.Ctxs[p.Exec] = processCtx
pm.Cancels[p.Exec] = processCancel
Expand Down
Loading