-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunProgram.go
More file actions
115 lines (95 loc) · 2.91 KB
/
RunProgram.go
File metadata and controls
115 lines (95 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package gorun
import (
"fmt"
"io"
"os/exec"
"strings"
"sync"
)
func (h *GoRun) RunProgram() error {
h.mutex.Lock()
defer h.mutex.Unlock()
// Always stop any previous running program first
// Use cleanup if KillAllOnStop is enabled
if h.KillAllOnStop {
if err := h.stopProgramAndCleanupUnsafe(true); err != nil {
h.safeBuffer.Write([]byte(fmt.Sprintf("Warning: Error stopping previous programs: %v\n", err)))
}
} else {
if err := h.stopProgramUnsafe(); err != nil {
h.safeBuffer.Write([]byte(fmt.Sprintf("Warning: Error stopping previous program: %v\n", err)))
}
}
runArgs := []string{}
if h.RunArguments != nil {
runArgs = h.RunArguments()
}
h.Cmd = exec.Command(h.ExecProgramPath, runArgs...)
h.hasWaited = false // Reset wait flag for new process
// DEBUG: Log the exact run command being executed
// fmt.Fprintf(h.safeBuffer, "[GORUN DEBUG] Starting: %s %v\n", h.ExecProgramPath, runArgs)
// fmt.Fprintf(h.safeBuffer, "[GORUN DEBUG] Working dir: %s\n", h.WorkingDir)
// Set working directory if specified
if h.WorkingDir != "" {
h.Cmd.Dir = h.WorkingDir
}
stderr, err := h.Cmd.StderrPipe()
if err != nil {
return err
}
stdout, err := h.Cmd.StdoutPipe()
if err != nil {
return err
}
err = h.Cmd.Start()
if err != nil {
// DEBUG: Log start failure details
// fmt.Fprintf(h.safeBuffer, "[GORUN DEBUG] Failed to start process: %v\n", err)
// Clean up the failed command to prevent issues in subsequent operations
h.Cmd = nil
h.isRunning = false
h.hasWaited = false
return err
}
// DEBUG: Log successful start
// fmt.Fprintf(h.safeBuffer, "[GORUN DEBUG] Process started successfully with PID: %d\n", h.Cmd.Process.Pid)
h.isRunning = true
var once sync.Once
done := make(chan struct{})
// Create local references for goroutines to avoid race conditions
currentCmd := h.Cmd
go io.Copy(h.safeBuffer, stderr)
go io.Copy(h.safeBuffer, stdout)
go func() {
select {
case <-h.ExitChan:
// h.Print("Received exit signal, stopping application...")
h.StopProgram()
once.Do(func() { close(done) })
case <-done:
// finish goroutine
}
}()
go func() {
err := currentCmd.Wait()
h.mutex.Lock()
h.isRunning = false
h.hasWaited = true // Mark that Wait() has been called
h.mutex.Unlock()
if err != nil {
// Check if the error is due to a signal termination (normal shutdown)
errMsg := err.Error()
if !strings.Contains(errMsg, "signal: terminated") &&
!strings.Contains(errMsg, "signal: killed") &&
!strings.Contains(errMsg, "signal: interrupt") &&
!strings.Contains(errMsg, "waitid: no child processes") {
// This is an actual error, not a normal signal termination
h.safeBuffer.Write([]byte(fmt.Sprintf("App: %v closed with error: %v\n", h.ExecProgramPath, err)))
}
// No log for normal signal terminations or known benign waitid error
}
// No log for clean exits either
once.Do(func() { close(done) })
}()
return nil
}