Skip to content

Commit a1ef2d0

Browse files
authored
Merge pull request #229 from Veske/master
Fix race condition in JenkinsTriggerHelper
2 parents 1ea3525 + 628dd1a commit a1ef2d0

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed

jenkins-client/src/main/java/com/offbytwo/jenkins/JenkinsTriggerHelper.java

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,18 @@
1919
*/
2020
public class JenkinsTriggerHelper {
2121

22-
private JenkinsServer server;
22+
private final JenkinsServer server;
23+
private final Long retryInterval;
24+
private static final Long DEFAULT_RETRY_INTERVAL = 200L;
2325

2426
public JenkinsTriggerHelper(JenkinsServer server) {
2527
this.server = server;
28+
this.retryInterval = DEFAULT_RETRY_INTERVAL;
29+
}
30+
31+
public JenkinsTriggerHelper(JenkinsServer server, Long retryInterval) {
32+
this.server = server;
33+
this.retryInterval = retryInterval;
2634
}
2735

2836
/**
@@ -114,37 +122,26 @@ public BuildWithDetails triggerJobAndWaitUntilFinished(String jobName, boolean c
114122
*/
115123
private BuildWithDetails triggerJobAndWaitUntilFinished(String jobName, QueueReference queueRef)
116124
throws IOException, InterruptedException {
117-
JobWithDetails job;
118-
job = this.server.getJob(jobName);
125+
JobWithDetails job = this.server.getJob(jobName);
119126
QueueItem queueItem = this.server.getQueueItem(queueRef);
127+
120128
while (!queueItem.isCancelled() && job.isInQueue()) {
121-
// TODO: May be we should make this configurable?
122-
Thread.sleep(200);
129+
Thread.sleep(retryInterval);
123130
job = this.server.getJob(jobName);
124131
queueItem = this.server.getQueueItem(queueRef);
125132
}
126133

134+
Build build = server.getBuild(queueItem);
127135
if (queueItem.isCancelled()) {
128-
// TODO: Check if this is ok?
129-
// We will get the details of the last build. NOT of the cancelled
130-
// build, cause there is no information about that available cause
131-
// it does not exist.
132-
BuildWithDetails result = new BuildWithDetails(job.getLastBuild().details());
133-
// TODO: Should we add more information here?
134-
result.setResult(BuildResult.CANCELLED);
135-
return result;
136+
return build.details();
136137
}
137138

138-
job = this.server.getJob(jobName);
139-
Build lastBuild = job.getLastBuild();
140-
141-
boolean isBuilding = lastBuild.details().isBuilding();
139+
boolean isBuilding = build.details().isBuilding();
142140
while (isBuilding) {
143-
// TODO: May be we should make this configurable?
144-
Thread.sleep(200);
145-
isBuilding = lastBuild.details().isBuilding();
141+
Thread.sleep(retryInterval);
142+
isBuilding = build.details().isBuilding();
146143
}
147144

148-
return lastBuild.details();
145+
return build.details();
149146
}
150147
}

0 commit comments

Comments
 (0)