@@ -293,6 +293,8 @@ func (a *AgentWorker) runPingLoop(ctx context.Context, idleMonitor *IdleMonitor)
293
293
defer setStat ("🛑 Ping loop stopped!" )
294
294
setStat ("🏃 Starting..." )
295
295
296
+ disconnectAfterIdleTimeout := time .Second * time .Duration (a .agentConfiguration .DisconnectAfterIdleTimeout )
297
+
296
298
// Create the ticker
297
299
pingInterval := time .Second * time .Duration (a .agent .PingInterval )
298
300
pingTicker := time .NewTicker (pingInterval )
@@ -384,40 +386,50 @@ func (a *AgentWorker) runPingLoop(ctx context.Context, idleMonitor *IdleMonitor)
384
386
}
385
387
386
388
// 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.
387
391
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
+ }
388
395
return nil
389
396
}
390
397
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.
392
400
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
+ }
393
404
a .logger .Info ("Job ran, and disconnect-after-job is enabled. Disconnecting..." )
394
405
return nil
395
406
}
396
407
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
+ }
401
414
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 )
402
419
if time .Now ().After (idleDeadline ) {
403
420
// Let other agents know this agent is now idle and termination
404
421
// is possible
405
422
idleMonitor .MarkIdle (a .agent .UUID )
406
423
407
424
// But only terminate if everyone else is also idle
408
425
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 )
411
427
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 ())
415
428
}
429
+ a .logger .Debug ("Agent has been idle for %.f seconds, but other agents haven't" ,
430
+ time .Since (lastActionTime ).Seconds ())
416
431
}
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.
421
433
continue
422
434
}
423
435
0 commit comments