5
5
import com .offbytwo .jenkins .model .JobWithDetails ;
6
6
import com .offbytwo .jenkins .model .QueueItem ;
7
7
import com .offbytwo .jenkins .model .QueueReference ;
8
+ import com .redhat .digkins .model .BuildStatus ;
8
9
import org .slf4j .Logger ;
9
10
import org .slf4j .LoggerFactory ;
10
11
@@ -28,39 +29,41 @@ public class TriggerBuildService {
28
29
public static final long POLL_PERIOD = 2 * 1000L ;
29
30
30
31
31
- private JenkinsServer jenkins ;
32
+ private JenkinsServer jenkinsServer ;
32
33
33
34
/**
34
- * @param jenkins jenkins api instance
35
+ * @param jenkinsServer jenkins api instance
35
36
*/
36
- public TriggerBuildService (JenkinsServer jenkins ) {
37
- this .jenkins = jenkins ;
37
+ public TriggerBuildService (JenkinsServer jenkinsServer ) {
38
+ this .jenkinsServer = jenkinsServer ;
38
39
}
39
40
40
41
/**
41
42
* See the documentation in {@link com.redhat.digkins.DiggerClient#build(String, long)}
42
43
*
43
44
* @param jobName name of the job
44
45
* @param timeout timeout
45
- * @return the build number
46
+ * @return the build status
46
47
* @throws IOException if connection problems occur during connecting to Jenkins
47
48
* @throws InterruptedException if a problem occurs during sleeping between checks
48
49
* @see com.redhat.digkins.DiggerClient#build(String, long)
49
50
*/
50
- public long build (String jobName , long timeout ) throws IOException , InterruptedException {
51
- final long timeoutTime = System .currentTimeMillis () + timeout ;
51
+ public BuildStatus build (String jobName , long timeout ) throws IOException , InterruptedException {
52
+ final long whenToTimeout = System .currentTimeMillis () + timeout ;
52
53
53
54
LOG .debug ("Going to build job with name: {}" , jobName );
54
55
LOG .debug ("Going to timeout in {} msecs if build didn't start executing" , timeout );
55
56
56
- JobWithDetails job = jenkins .getJob (jobName );
57
+ JobWithDetails job = jenkinsServer .getJob (jobName );
57
58
if (job == null ) {
59
+ LOG .debug ("Unable to find job for name '{}'" , jobName );
58
60
throw new IllegalArgumentException ("Unable to find job for name '" + jobName + "'" );
59
61
}
60
62
61
63
final QueueReference queueReference = job .build ();
62
64
if (queueReference == null ) {
63
65
// this is probably an implementation problem we have here
66
+ LOG .debug ("Queue reference cannot be null!" );
64
67
throw new IllegalStateException ("Queue reference cannot be null!" );
65
68
}
66
69
LOG .debug ("Build triggered; queue item reference: {}" , queueReference .getQueueItemUrlPart ());
@@ -74,22 +77,23 @@ public long build(String jobName, long timeout) throws IOException, InterruptedE
74
77
75
78
QueueItem queueItem ;
76
79
while (true ) {
77
- queueItem = jenkins .getQueueItem (queueReference );
80
+ queueItem = jenkinsServer .getQueueItem (queueReference );
78
81
LOG .debug ("Queue item : {}" , queueItem );
79
82
80
83
if (queueItem == null ) {
81
84
// this is probably an implementation problem we have here
85
+ LOG .debug ("Queue item cannot be null!" );
82
86
throw new IllegalStateException ("Queue item cannot be null!" );
83
87
}
84
88
85
89
LOG .debug ("Build item cancelled:{}, blocked:{}, buildable:{}, stuck:{}" , queueItem .isCancelled (), queueItem .isBlocked (), queueItem .isBuildable (), queueItem .isStuck ());
86
90
87
91
if (queueItem .isCancelled ()) {
88
- LOG .debug ("Queue item is cancelled. Returning -1 " );
89
- return - 1 ;
92
+ LOG .debug ("Queue item is cancelled. Returning CANCELLED_IN_QUEUE " );
93
+ return new BuildStatus ( BuildStatus . State . CANCELLED_IN_QUEUE , - 1 ) ;
90
94
} else if (queueItem .isStuck ()) {
91
- LOG .debug ("Queue item is stuck. Returning -1 " );
92
- return - 1 ;
95
+ LOG .debug ("Queue item is stuck. Returning STUCK_IN_QUEUE " );
96
+ return new BuildStatus ( BuildStatus . State . STUCK_IN_QUEUE , - 1 ) ;
93
97
}
94
98
95
99
// do not return -1 if blocked.
@@ -99,15 +103,15 @@ public long build(String jobName, long timeout) throws IOException, InterruptedE
99
103
100
104
if (executable != null ) {
101
105
LOG .debug ("Build has an executable. Returning build number: {}" , executable .getNumber ());
102
- return executable .getNumber ();
106
+ return new BuildStatus ( BuildStatus . State . BUILDING , executable .getNumber (). intValue () );
103
107
} else {
104
108
LOG .debug ("Build did not start executing yet." );
105
- if (timeoutTime < System .currentTimeMillis ()) {
109
+ if (whenToTimeout > System .currentTimeMillis ()) {
106
110
LOG .debug ("Timeout period has not exceeded yet. Sleeping for {} msecs" , POLL_PERIOD );
107
111
Thread .sleep (POLL_PERIOD );
108
112
} else {
109
- LOG .debug ("Timeout period has exceeded. Returning -1 ." );
110
- return - 1 ;
113
+ LOG .debug ("Timeout period has exceeded. Returning TIMED_OUT ." );
114
+ return new BuildStatus ( BuildStatus . State . TIMED_OUT , - 1 ) ;
111
115
}
112
116
}
113
117
0 commit comments