@@ -10,46 +10,7 @@ import (
10
10
log "github.com/sirupsen/logrus"
11
11
)
12
12
13
- // LazyJobReapGracePeriod defines the time to wait after SIGTERM before sending SIGKILL.
14
- // It's a variable to allow modification for testing.
15
- var LazyJobReapGracePeriod = 10 * time .Second
16
-
17
- // GetLazyJobReapGracePeriodForTest returns the current grace period. Used by tests.
18
- func GetLazyJobReapGracePeriodForTest () time.Duration {
19
- return LazyJobReapGracePeriod
20
- }
21
-
22
- // SetCoolDownTimeout allows tests to set a custom coolDownTimeout.
23
- func (job * LazyJob ) SetCoolDownTimeout (d time.Duration ) {
24
- job .coolDownTimeout = d
25
- }
26
-
27
- // SetActiveConnections allows tests to set activeConnections.
28
- func (job * LazyJob ) SetActiveConnections (n uint32 ) {
29
- job .activeConnections = n
30
- }
31
-
32
- // SetLastConnectionClosed allows tests to set lastConnectionClosed.
33
- func (job * LazyJob ) SetLastConnectionClosed (t time.Time ) {
34
- job .lastConnectionClosed = t
35
- }
36
-
37
- // SetProcess allows tests to set the job's process.
38
- // This is primarily for simulating a running process.
39
- // It sets both job.process (used by LazyJob specific logic)
40
- // and job.Cmd.Process (used by BaseJob signal methods via job.Cmd).
41
- func (job * LazyJob ) SetProcess (p * os.Process ) {
42
- job .process = p
43
- if job .Cmd != nil { // Ensure Cmd is initialized
44
- job .Cmd .Process = p
45
- } else if p != nil { // If Cmd is nil, but we have a process, create a dummy Cmd
46
- // This case is tricky; ideally Cmd should be fully set up by a Start-like method.
47
- // For testing reaper logic which relies on job.Cmd.Process.Pid, we need it.
48
- // This might need refinement depending on how tests set up job.Cmd.
49
- // log.Warn("SetProcess called on LazyJob with nil Cmd; creating a minimal Cmd. May need attention.")
50
- // job.Cmd = &exec.Cmd{} // This alone is not enough, Path and SysProcAttr are needed by reaper's callers
51
- }
52
- }
13
+ const lazyJobReapGracePeriod = 10 * time .Second
53
14
54
15
func (job * LazyJob ) AssertStarted (ctx context.Context ) error {
55
16
l := log .WithField ("job.name" , job .Config .Name )
@@ -118,15 +79,13 @@ func (job *LazyJob) Run(ctx context.Context, errors chan<- error) error {
118
79
}()
119
80
}
120
81
121
- job .StartProcessReaper (ctx ) // Renamed to be exported
82
+ job .startProcessReaper (ctx )
122
83
123
84
log .Infof ("holding off starting job %s until first request" , job .Config .Name )
124
85
return nil
125
86
}
126
87
127
- // StartProcessReaper starts the goroutine that monitors the lazy job's process
128
- // and terminates it if it's idle for too long.
129
- func (job * LazyJob ) StartProcessReaper (ctx context.Context ) {
88
+ func (job * LazyJob ) startProcessReaper (ctx context.Context ) {
130
89
reaperInterval := job .coolDownTimeout / 2
131
90
if reaperInterval < 1 * time .Second {
132
91
reaperInterval = 1 * time .Second
@@ -174,15 +133,15 @@ func (job *LazyJob) StartProcessReaper(ctx context.Context) {
174
133
}
175
134
pidToReap := job .Cmd .Process .Pid
176
135
l .Infof ("sending SIGTERM to idle process PID %d" , pidToReap )
177
- if err := job .Signal (syscall .SIGTERM ); err != nil {
136
+ if err := job .signal (syscall .SIGTERM ); err != nil {
178
137
l .WithError (err ).Warnf ("failed to send SIGTERM to PID %d" , pidToReap )
179
138
job .lazyStartLock .Unlock ()
180
139
continue
181
140
}
182
141
183
142
job .lazyStartLock .Unlock ()
184
143
185
- graceTimer := time .NewTimer (LazyJobReapGracePeriod ) // Use the variable
144
+ graceTimer := time .NewTimer (lazyJobReapGracePeriod )
186
145
defer graceTimer .Stop ()
187
146
188
147
select {
@@ -191,7 +150,7 @@ func (job *LazyJob) StartProcessReaper(ctx context.Context) {
191
150
// Check if the process we sent SIGTERM to is still running
192
151
if job .process != nil && job .Cmd != nil && job .Cmd .Process != nil && job .Cmd .Process .Pid == pidToReap {
193
152
l .Warnf ("process PID %d did not exit after SIGTERM and grace period; sending SIGKILL" , pidToReap )
194
- if err := job .SignalAll (syscall .SIGKILL ); err != nil {
153
+ if err := job .signalAll (syscall .SIGKILL ); err != nil {
195
154
l .WithError (err ).Errorf ("failed to send SIGKILL to PID %d" , pidToReap )
196
155
}
197
156
} else if job .process != nil {
0 commit comments