package io.trygvis.esper.testing.jenkins; import fj.*; import fj.data.*; import static fj.data.Option.*; import static io.trygvis.esper.testing.jenkins.JenkinsClient.apiXml; import io.trygvis.esper.testing.jenkins.xml.JenkinsXml; import io.trygvis.esper.testing.util.object.*; import org.joda.time.*; import java.io.*; import java.net.*; import java.util.*; import java.util.HashSet; import java.util.List; import java.util.concurrent.*; public class JenkinsServerOld implements Closeable { private final JenkinsClient client; public final URI url; private final ObjectManager jobManager; private boolean shouldRun = true; private final Thread thread; private Option> jenkins = none(); public JenkinsServerOld(final ScheduledExecutorService executorService, final JenkinsClient client, URI url) { this.client = client; this.url = apiXml(url); jobManager = new ObjectManager<>("JenkinsJob", Collections.emptySet(), new ObjectFactory() { public JenkinsJob create(URI url) { return new JenkinsJob(executorService, client, url); } }); thread = new Thread(new Runnable() { public void run() { JenkinsServerOld.this.run(); } }); thread.setDaemon(true); thread.start(); } public void close() throws IOException { shouldRun = false; thread.interrupt(); while (thread.isAlive()) { try { thread.join(); } catch (InterruptedException e) { continue; } } } private void run() { while (shouldRun) { try { doWork(); } catch (Exception e) { e.printStackTrace(System.out); } try { Thread.sleep(10 * 1000); } catch (InterruptedException e) { // ignore } } } public Option> getJenkins() { return jenkins; } public Collection getJobs() { return jobManager.getObjects(); } private void doWork() { try { Option> o = client.fetchJobs(url); if(o.isNone()) { return; } JenkinsXml xml = o.some()._1(); List jobUris = new ArrayList<>(xml.jobs.size()); for (JenkinsXml.JobXml job : xml.jobs) { jobUris.add(URI.create(job.url)); } this.jenkins = some(P.p(xml, new LocalDateTime())); jobManager.update(new HashSet<>(jobUris)); } catch (Throwable e) { e.printStackTrace(System.out); } } }