aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsJob.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/io/trygvis/esper/testing/jenkins/JenkinsJob.java')
-rw-r--r--src/main/java/io/trygvis/esper/testing/jenkins/JenkinsJob.java83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsJob.java b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsJob.java
new file mode 100644
index 0000000..6596dfa
--- /dev/null
+++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsJob.java
@@ -0,0 +1,83 @@
+package io.trygvis.esper.testing.jenkins;
+
+import org.codehaus.httpcache4j.util.*;
+
+import java.io.*;
+import java.net.*;
+import java.util.*;
+import java.util.concurrent.*;
+
+public class JenkinsJob implements Closeable {
+
+ private final JenkinsClient client;
+ private final URI uri;
+
+ private JenkinsJobXml latestStatus;
+ // private boolean shouldRun = true;
+ // private final Thread thread;
+ private final ScheduledFuture<?> future;
+
+ public JenkinsJob(ScheduledExecutorService executorService, JenkinsClient client, URI uri) {
+ this.client = client;
+ this.uri = URIBuilder.fromURI(uri).addRawPath("api/xml").toURI();
+
+ long initialDelay = (long) Math.random() + 1;
+ long period = (long) (Math.random() * 10d) + 1;
+ future = executorService.scheduleAtFixedRate(new Runnable() {
+ public void run() {
+ JenkinsJob.this.doWork();
+ }
+ }, initialDelay, period, TimeUnit.SECONDS);
+
+// thread = new Thread(new Runnable() {
+// public void run() {
+// JenkinsJob.this.run();
+// }
+// });
+// thread.setDaemon(true);
+// thread.start();
+ }
+
+ public JenkinsJobXml getLatestStatus() {
+ return latestStatus;
+ }
+
+ /*
+ public void close() throws IOException {
+ shouldRun = false;
+ thread.interrupt();
+ while (thread.isAlive()) {
+ try {
+ thread.join();
+ } catch (InterruptedException e) {
+ continue;
+ }
+ }
+ }
+
+ private void run() {
+ Random r = new Random();
+ while (shouldRun) {
+ doWork();
+
+ try {
+ Thread.sleep(1000 + r.nextInt(10) * 1000);
+ } catch (InterruptedException e) {
+ // ignore
+ }
+ }
+ }
+ */
+
+ public void close() throws IOException {
+ future.cancel(true);
+ }
+
+ private void doWork() {
+ try {
+ latestStatus = client.fetchJob(uri);
+ } catch (Exception e) {
+ e.printStackTrace(System.out);
+ }
+ }
+}