Skip to content

Commit 2e312b9

Browse files
NuhRoemer
authored andcommitted
Verification if the Computer has been used
Sometimes the Computer will be assigned to a task, but the Task will start running elsewhere. So, the Computer waits forever and will never be turned off when the Task was executed somewhere else.
1 parent c3610e0 commit 2e312b9

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

src/main/java/org/jenkinsci/plugins/docker/swarm/DockerSwarmAgentRetentionStrategy.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,28 @@ public class DockerSwarmAgentRetentionStrategy extends RetentionStrategy<DockerS
2424
implements ExecutorListener {
2525
private static final Logger LOGGER = Logger.getLogger(DockerSwarmAgentRetentionStrategy.class.getName());
2626

27-
private int timeout = 1;
2827
private transient volatile boolean terminating;
28+
29+
private boolean isTaskAccepted;
2930
private boolean isTaskCompleted;
31+
private long timeout; // in ms
3032

3133
@DataBoundConstructor
32-
public DockerSwarmAgentRetentionStrategy(int idleMinutes) {
33-
this.timeout = idleMinutes;
34+
public DockerSwarmAgentRetentionStrategy(int timeout /* in minutes */) {
35+
this.timeout = MINUTES.toMillis(timeout);
3436
}
3537

36-
public int getIdleMinutes() {
38+
public long getTimeout() {
3739
return timeout;
3840
}
3941

4042
@Override
4143
public long check(@Nonnull DockerSwarmComputer c) {
42-
if (c.isIdle() && c.isOnline() && isTaskCompleted) {
43-
final long idleMilliseconds = System.currentTimeMillis() - c.getIdleStartMilliseconds();
44-
if (idleMilliseconds > MINUTES.toMillis(timeout)) {
44+
if (c.isIdle() && c.isOnline()) {
45+
final long connectTime = System.currentTimeMillis() - c.getConnectTime();
46+
final long idleTime = System.currentTimeMillis() - c.getIdleStartMilliseconds();
47+
final boolean isTimeout = connectTime > timeout && idleTime > timeout;
48+
if (isTimeout && (!isTaskAccepted || isTaskCompleted)) {
4549
LOGGER.log(Level.INFO, "Disconnecting due to idle {0}", c.getName());
4650
done(c);
4751
}
@@ -57,25 +61,27 @@ public void start(DockerSwarmComputer c) {
5761

5862
@Override
5963
public void taskAccepted(Executor executor, Queue.Task task) {
64+
this.isTaskAccepted = true;
65+
getLogger(executor).println("Task accepted: " + task.getFullDisplayName());
6066
}
6167

6268
@Override
6369
public void taskCompleted(Executor executor, Queue.Task task, long durationMS) {
6470
this.isTaskCompleted = true;
65-
getLogger(executor).println("Task completed: " + task.getFullDisplayName());
71+
getLogger(executor).println("Task completed: " + task.getFullDisplayName() + " (" + durationMS + " ms)");
6672
done(executor);
6773
}
6874

69-
private PrintStream getLogger(Executor executor) {
70-
final DockerSwarmComputer c = (DockerSwarmComputer) executor.getOwner();
71-
return c.getListener().getLogger();
72-
}
73-
7475
@Override
7576
public void taskCompletedWithProblems(Executor executor, Queue.Task task, long durationMS, Throwable problems) {
7677
taskCompleted(executor, task, durationMS);
7778
}
7879

80+
private PrintStream getLogger(Executor executor) {
81+
final DockerSwarmComputer c = (DockerSwarmComputer) executor.getOwner();
82+
return c.getListener().getLogger();
83+
}
84+
7985
private void done(Executor executor) {
8086
final DockerSwarmComputer c = (DockerSwarmComputer) executor.getOwner();
8187
Queue.Executable exec = executor.getCurrentExecutable();
@@ -101,6 +107,7 @@ private synchronized void done(final DockerSwarmComputer c) {
101107
try {
102108
node.terminate();
103109
} catch (IOException e) {
110+
LOGGER.log(Level.WARNING, "Failed to terminate " + c.getName(), e);
104111
}
105112
}
106113
});

0 commit comments

Comments
 (0)