Skip to content

Commit 628dd1a

Browse files
author
Kaspar Tint
committed
Simplify the triggerJobAndWaitUntilFinished method a bit
Make the polling rate configurable
1 parent 984d828 commit 628dd1a

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

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

Lines changed: 15 additions & 15 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,31 +122,23 @@ 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-
Build build = server.getBuild(queueItem);
139139
boolean isBuilding = build.details().isBuilding();
140140
while (isBuilding) {
141-
Thread.sleep(200);
141+
Thread.sleep(retryInterval);
142142
isBuilding = build.details().isBuilding();
143143
}
144144

0 commit comments

Comments
 (0)