Skip to content

Commit

Permalink
read server time from http resposne header
Browse files Browse the repository at this point in the history
  • Loading branch information
jenkins-pipeline-code committed Jul 7, 2011
1 parent 543b098 commit 3dc0cc4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public class JenkinsWorker extends SwingWorker<Hudson, Void> {
private final static int READ_TIMEOUT = 5000;
private final static int JOB_API_EXECUTORS = 5;
private final static int JOB_API_TIMEOUT = CONNECT_TIMEOUT + READ_TIMEOUT + 1000;

private String jenkinsUrl;
private Exception exception = null;
private String viewName;
Expand Down Expand Up @@ -71,12 +70,26 @@ public void run() {
}
}

private long getServerResponseTime(String jenkinsUrl) {

try {
URL url = new URL(jenkinsUrl);
URLConnection conn = url.openConnection();

return conn.getHeaderFieldDate("Date", 0);

} catch (Exception e) {
return 0;
}
}

/** {@inheritDoc} */
@Override
protected Hudson doInBackground() throws Exception {
exception = null;

try {

URL hudsonQueueApiUrl = new URL(String.format("%s/queue/api/xml", jenkinsUrl));

XStream queueXstream = getDefaultXStream();
Expand All @@ -97,22 +110,22 @@ protected Hudson doInBackground() throws Exception {
hudsonXstream.addImplicitCollection(View.class, "jobs", "job", Job.class);

Hudson hudson = (Hudson) hudsonXstream.fromXML(openStream(hudsonApiUrl));


hudson.setServerResponseTimestamp(getServerResponseTime(jenkinsUrl));
List<Job> jobs = hudson.getJobs();
for (View view : hudson.getViews()) {
if (viewName != null && viewName.equals(view.getName())) {
jobs = view.getJobs();
}
}

ExecutorService executor = Executors.newFixedThreadPool(JOB_API_EXECUTORS);

//load detailed jobs infos for all displayed jobs
for (Job job : jobs) {
executor.execute(new JobApiRunnable(job));
}

executor.shutdown();
executor.awaitTermination(JOB_API_TIMEOUT, TimeUnit.MILLISECONDS);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ public int compare(Job job1, Job job2) {
private final static String MESSAGE_NO_URL_ENTERED = "No URL given, exiting";
private final static String MESSAGE_JEKINS_WALL_DISPLAY = "Jenkins Wall Display";
private final static int MAX_QUEUE_POSITIONS = 3;

private long currentServerTimestamp = 0;

public WallDisplayFrame() {

initFrame();
Expand Down Expand Up @@ -307,6 +308,7 @@ public void actionPerformed(ActionEvent event) {
message = hudsonWorker.getException().getMessage();
} else {
jobs.addAll(JenkinsWorker.getJobsToDisplay(hudsonWorker.get(), viewName));
currentServerTimestamp = hudsonWorker.get().getServerResponseTimestamp();
Collections.sort(jobs, new JobComperator());
}

Expand Down Expand Up @@ -467,7 +469,7 @@ private void paintProgress(Graphics2D graphics, int jobX, int jobY, int jobWidth
Area jobRectangleArea =
new Area(new RoundRectangle2D.Double(jobX, jobY, jobWidth, jobHeight, JOB_ARC_WIDTH, JOB_ARC_HEIGHT));

long currentDuration = System.currentTimeMillis() - job.getLastBuild().getTimestamp();
long currentDuration = currentServerTimestamp - job.getLastBuild().getTimestamp();
long lastDuration = job.getLastSuccessfulBuild().getDuration();
long percentage = currentDuration / (lastDuration / 100);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class Hudson {
private List<Job> jobs = new ArrayList<Job>();
private List<View> views = new ArrayList<View>();
private String nodeDescription;
private long serverResponseTimestamp;

/**
* @return the nodeDescription
Expand Down Expand Up @@ -57,4 +58,18 @@ public List<Job> getJobs() {
public void setJobs(List<Job> jobs) {
this.jobs = jobs;
}

/**
* @return the serverResponseTimestamp
*/
public long getServerResponseTimestamp() {
return serverResponseTimestamp;
}

/**
* @param serverResponseTimestamp the serverResponseTimestamp to set
*/
public void setServerResponseTimestamp(long serverResponseTimestamp) {
this.serverResponseTimestamp = serverResponseTimestamp;
}
}

0 comments on commit 3dc0cc4

Please sign in to comment.