|
| 1 | +package com.redhat.digkins.services; |
| 2 | + |
| 3 | +import com.offbytwo.jenkins.JenkinsServer; |
| 4 | +import com.offbytwo.jenkins.model.Executable; |
| 5 | +import com.offbytwo.jenkins.model.JobWithDetails; |
| 6 | +import com.offbytwo.jenkins.model.QueueItem; |
| 7 | +import com.offbytwo.jenkins.model.QueueReference; |
| 8 | +import org.slf4j.Logger; |
| 9 | +import org.slf4j.LoggerFactory; |
| 10 | + |
| 11 | +import java.io.IOException; |
| 12 | + |
| 13 | +/** |
| 14 | + * Provides functionality to trigger a build. |
| 15 | + **/ |
| 16 | +public class TriggerBuildService { |
| 17 | + |
| 18 | + private static final Logger LOG = LoggerFactory.getLogger(TriggerBuildService.class); |
| 19 | + |
| 20 | + /** |
| 21 | + * How long should we wait before we start checking the queue item status. |
| 22 | + */ |
| 23 | + public static final long FIRST_CHECK_DELAY = 5 * 1000L; |
| 24 | + |
| 25 | + /** |
| 26 | + * How long should we wait before checking the queue item status for next time. |
| 27 | + */ |
| 28 | + public static final long POLL_PERIOD = 2 * 1000L; |
| 29 | + |
| 30 | + |
| 31 | + private JenkinsServer jenkins; |
| 32 | + |
| 33 | + /** |
| 34 | + * @param jenkins jenkins api instance |
| 35 | + */ |
| 36 | + public TriggerBuildService(JenkinsServer jenkins) { |
| 37 | + this.jenkins = jenkins; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * See the documentation in {@link com.redhat.digkins.DiggerClient#build(String, long)} |
| 42 | + * |
| 43 | + * @param jobName name of the job |
| 44 | + * @param timeout timeout |
| 45 | + * @return the build number |
| 46 | + * @throws IOException if connection problems occur during connecting to Jenkins |
| 47 | + * @throws InterruptedException if a problem occurs during sleeping between checks |
| 48 | + * @see com.redhat.digkins.DiggerClient#build(String, long) |
| 49 | + */ |
| 50 | + public long build(String jobName, long timeout) throws IOException, InterruptedException { |
| 51 | + final long timeoutTime = System.currentTimeMillis() + timeout; |
| 52 | + |
| 53 | + LOG.debug("Going to build job with name: {}", jobName); |
| 54 | + LOG.debug("Going to timeout in {} msecs if build didn't start executing", timeout); |
| 55 | + |
| 56 | + JobWithDetails job = jenkins.getJob(jobName); |
| 57 | + if (job == null) { |
| 58 | + throw new IllegalArgumentException("Unable to find job for name '" + jobName + "'"); |
| 59 | + } |
| 60 | + |
| 61 | + final QueueReference queueReference = job.build(); |
| 62 | + if (queueReference == null) { |
| 63 | + // this is probably an implementation problem we have here |
| 64 | + throw new IllegalStateException("Queue reference cannot be null!"); |
| 65 | + } |
| 66 | + LOG.debug("Build triggered; queue item reference: {}", queueReference.getQueueItemUrlPart()); |
| 67 | + |
| 68 | + // wait for N seconds, then fetch the queue item. |
| 69 | + // do it until we have an executable. |
| 70 | + // we would have an executable when the build leaves queue and starts building. |
| 71 | + |
| 72 | + LOG.debug("Going to sleep {} msecs", FIRST_CHECK_DELAY); |
| 73 | + Thread.sleep(FIRST_CHECK_DELAY); |
| 74 | + |
| 75 | + QueueItem queueItem; |
| 76 | + while (true) { |
| 77 | + queueItem = jenkins.getQueueItem(queueReference); |
| 78 | + LOG.debug("Queue item : {}", queueItem); |
| 79 | + |
| 80 | + if (queueItem == null) { |
| 81 | + // this is probably an implementation problem we have here |
| 82 | + throw new IllegalStateException("Queue item cannot be null!"); |
| 83 | + } |
| 84 | + |
| 85 | + LOG.debug("Build item cancelled:{}, blocked:{}, buildable:{}, stuck:{}", queueItem.isCancelled(), queueItem.isBlocked(), queueItem.isBuildable(), queueItem.isStuck()); |
| 86 | + |
| 87 | + if (queueItem.isCancelled()) { |
| 88 | + LOG.debug("Queue item is cancelled. Returning -1"); |
| 89 | + return -1; |
| 90 | + } else if (queueItem.isStuck()) { |
| 91 | + LOG.debug("Queue item is stuck. Returning -1"); |
| 92 | + return -1; |
| 93 | + } |
| 94 | + |
| 95 | + // do not return -1 if blocked. |
| 96 | + // we will wait until it is unblocked. |
| 97 | + |
| 98 | + final Executable executable = queueItem.getExecutable(); |
| 99 | + |
| 100 | + if (executable != null) { |
| 101 | + LOG.debug("Build has an executable. Returning build number: {}", executable.getNumber()); |
| 102 | + return executable.getNumber(); |
| 103 | + } else { |
| 104 | + LOG.debug("Build did not start executing yet."); |
| 105 | + if (timeoutTime < System.currentTimeMillis()) { |
| 106 | + LOG.debug("Timeout period has not exceeded yet. Sleeping for {} msecs", POLL_PERIOD); |
| 107 | + Thread.sleep(POLL_PERIOD); |
| 108 | + } else { |
| 109 | + LOG.debug("Timeout period has exceeded. Returning -1."); |
| 110 | + return -1; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments