@@ -31,7 +31,7 @@ func (job *baseJob) SignalAll(sig syscall.Signal) {
31
31
}
32
32
}
33
33
34
- if job .cmd == nil || job . cmd . Process == nil {
34
+ if ! job .HasStarted () {
35
35
errFunc (
36
36
fmt .Errorf ("job is not running" ),
37
37
)
@@ -43,6 +43,9 @@ func (job *baseJob) SignalAll(sig syscall.Signal) {
43
43
}
44
44
45
45
func (job * baseJob ) signalAll (sig syscall.Signal ) error {
46
+ if ! job .HasStarted () {
47
+ return nil
48
+ }
46
49
if job .cmd .Process .Pid <= 0 { // Ensure PID is positive before negating
47
50
return syscall .Errno (syscall .ESRCH ) // No such process or invalid PID
48
51
}
@@ -56,7 +59,7 @@ func (job *baseJob) Signal(sig os.Signal) {
56
59
}
57
60
}
58
61
59
- if job .cmd == nil || job . cmd . Process == nil {
62
+ if ! job .HasStarted () {
60
63
errFunc (
61
64
fmt .Errorf ("job is not running" ),
62
65
)
@@ -89,6 +92,10 @@ func (job *baseJob) IsControllable() bool {
89
92
return job .Config .Controllable
90
93
}
91
94
95
+ func (job * baseJob ) HasStarted () bool {
96
+ return job .cmd != nil && job .cmd .Process != nil
97
+ }
98
+
92
99
func (job * baseJob ) GetPhase () * JobPhase {
93
100
return & job .phase
94
101
}
@@ -183,13 +190,11 @@ func (job *baseJob) startOnce(ctx context.Context, process chan<- *os.Process) e
183
190
select {
184
191
// job errChan or failed
185
192
case err := <- errChan :
186
- if job .cmd != nil && job .cmd .Process != nil { // Check if process actually started
187
- if termErr := job .signalAll (syscall .SIGTERM ); termErr != nil {
188
- if e , ok := termErr .(syscall.Errno ); ok && e == syscall .ESRCH {
189
- // ESRCH (Error No Such Process) is fine, means process group already gone
190
- } else {
191
- l .WithError (termErr ).Error ("failed to send SIGTERM to job's process group" )
192
- }
193
+ if termErr := job .signalAll (syscall .SIGTERM ); termErr != nil {
194
+ if e , ok := termErr .(syscall.Errno ); ok && e == syscall .ESRCH {
195
+ // ESRCH (Error No Such Process) is fine, means process group already gone
196
+ } else {
197
+ l .WithError (termErr ).Error ("failed to send SIGTERM to job's process group" )
193
198
}
194
199
}
195
200
@@ -209,7 +214,7 @@ func (job *baseJob) startOnce(ctx context.Context, process chan<- *os.Process) e
209
214
}
210
215
return err
211
216
case <- ctx .Done ():
212
- if job .cmd != nil && job . cmd . Process != nil { // Check if process actually started
217
+ if job .HasStarted () {
213
218
// ctx canceled, try to terminate job
214
219
_ = job .signalAll (syscall .SIGTERM )
215
220
l .WithField ("job.name" , job .Config .Name ).Info ("sent SIGTERM to job's process group on ctx.Done" )
@@ -226,9 +231,8 @@ func (job *baseJob) startOnce(ctx context.Context, process chan<- *os.Process) e
226
231
return err
227
232
}
228
233
}
229
- // If job.cmd or job.cmd.Process is nil, it means the process didn't start or already cleaned up.
230
234
l .WithField ("job.name" , job .Config .Name ).Info ("context done, but process was not running or already cleaned up." )
231
- return ctx .Err () // Return context error
235
+ return ctx .Err ()
232
236
}
233
237
}
234
238
0 commit comments