Skip to content

Commit a55a4bc

Browse files
committed
fix: Don't disconnect-after-idle when just given a job
1 parent 26d6567 commit a55a4bc

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

agent/agent_worker.go

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ func (a *AgentWorker) runPingLoop(ctx context.Context, idleMonitor *IdleMonitor)
293293
defer setStat("🛑 Ping loop stopped!")
294294
setStat("🏃 Starting...")
295295

296+
disconnectAfterIdleTimeout := time.Second * time.Duration(a.agentConfiguration.DisconnectAfterIdleTimeout)
297+
296298
// Create the ticker
297299
pingInterval := time.Second * time.Duration(a.agent.PingInterval)
298300
pingTicker := time.NewTicker(pingInterval)
@@ -384,40 +386,50 @@ func (a *AgentWorker) runPingLoop(ctx context.Context, idleMonitor *IdleMonitor)
384386
}
385387

386388
// Exit after acquire-job.
389+
// For acquire-job agents, registration sets ignore-in-dispatches=true,
390+
// so job should be nil. If not nil, complain.
387391
if a.agentConfiguration.AcquireJob != "" {
392+
if job != nil {
393+
a.logger.Error("Agent ping dispatched a job (id %q) but agent is in acquire-job mode!", job.ID)
394+
}
388395
return nil
389396
}
390397

391-
// Exit after disconnect-after-job.
398+
// Exit after disconnect-after-job. Finishing the job sets
399+
// ignore-in-dispatches=true, so job should be nil. If not, complain.
392400
if ranJob && a.agentConfiguration.DisconnectAfterJob {
401+
if job != nil {
402+
a.logger.Error("Agent ping dispatched a job (id %q) but agent is in disconnect-after-job mode (and already ran a job)!", job.ID)
403+
}
393404
a.logger.Info("Job ran, and disconnect-after-job is enabled. Disconnecting...")
394405
return nil
395406
}
396407

397-
// Handle disconnect after idle timeout (and deprecated disconnect-after-job-timeout)
398-
if a.agentConfiguration.DisconnectAfterIdleTimeout > 0 {
399-
idleDeadline := lastActionTime.Add(time.Second *
400-
time.Duration(a.agentConfiguration.DisconnectAfterIdleTimeout))
408+
// Note that Ping only returns a job if err == nil.
409+
if job == nil {
410+
if disconnectAfterIdleTimeout == 0 {
411+
// No job and no idle timeout.
412+
continue
413+
}
401414

415+
// Handle disconnect after idle timeout (and deprecated disconnect-after-job-timeout).
416+
// Only do this check if we weren't just dispatched a job.
417+
// (If we were dispatched a job, we're not idle.)
418+
idleDeadline := lastActionTime.Add(disconnectAfterIdleTimeout)
402419
if time.Now().After(idleDeadline) {
403420
// Let other agents know this agent is now idle and termination
404421
// is possible
405422
idleMonitor.MarkIdle(a.agent.UUID)
406423

407424
// But only terminate if everyone else is also idle
408425
if idleMonitor.Idle() {
409-
a.logger.Info("All agents have been idle for %d seconds. Disconnecting...",
410-
a.agentConfiguration.DisconnectAfterIdleTimeout)
426+
a.logger.Info("All agents have been idle for %v. Disconnecting...", disconnectAfterIdleTimeout)
411427
return nil
412-
} else {
413-
a.logger.Debug("Agent has been idle for %.f seconds, but other agents haven't",
414-
time.Since(lastActionTime).Seconds())
415428
}
429+
a.logger.Debug("Agent has been idle for %.f seconds, but other agents haven't",
430+
time.Since(lastActionTime).Seconds())
416431
}
417-
}
418-
419-
// Note that Ping only returns a job if err == nil.
420-
if job == nil {
432+
// Not idle enough yet. Wait and ping again.
421433
continue
422434
}
423435

0 commit comments

Comments
 (0)