Skip to content

Commit

Permalink
Make multiple attempts to contact Code Dx server
Browse files Browse the repository at this point in the history
When running a build, previously a single failed attempted to contact the server would throw an exception and cause the build to fail. This will allow the plugin to try multiple times at different intervals before failing the build.
  • Loading branch information
samuelbjohnson committed Mar 31, 2015
1 parent 1910d71 commit d934f2e
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public List<CountGroup> getFindingsGroupedCounts(int id, Filter filter, String c
* @throws IOException
* @throws CodeDxClientException
*/
private <T> T doGet(String path, Type typeOfT, boolean experimental) throws ClientProtocolException, IOException, CodeDxClientException {
protected <T> T doGet(String path, Type typeOfT, boolean experimental) throws ClientProtocolException, IOException, CodeDxClientException {

HttpGet getRequest;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.secdec.codedx.api.client;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;

import java.io.IOException;
import java.io.PrintStream;
import java.lang.reflect.Type;
import java.net.Socket;
import java.net.SocketException;

/**
* Created by samuelj on 3/27/15.
*/
public class CodeDxRepeatingClient extends CodeDxClient {
private PrintStream logger;

public CodeDxRepeatingClient(String url, String key, PrintStream logger) {
super(url, key);
this.logger = logger;
}

/**
* A generic get that will marshal JSON data into some type.
* @param path Append this to the URL
* @param typeOfT
* @param experimental If this request is part of the experimental API
* @return Something of type T
* @throws ClientProtocolException
* @throws IOException
* @throws CodeDxClientException
*/
protected <T> T doGet(String path, Type typeOfT, boolean experimental) throws IOException, CodeDxClientException {
try {
int fails = 0;
while (fails < 3) {
try {
return super.doGet(path, typeOfT, experimental);
} catch (CodeDxClientException clientException) {
fails++;
logger.println("Attempt " + fails + " " + clientException.getMessage() + " response code: " + clientException.getHttpCode());
switch (fails) {
case 1:
logger.println("Trying again after 1 second");
Thread.sleep(1000);
break;
case 2:
logger.println("Trying again after 5 seconds");
Thread.sleep(5000);
break;
case 3:
logger.println("Trying again after 30 seconds");
Thread.sleep(30000);
break;
default:
throw clientException;
}
} catch (SocketException socketException) {
fails++;
logger.println("Attempt " + fails + " " + socketException.getMessage());
switch (fails) {
case 1:
logger.println("Trying again after 1 second");
Thread.sleep(1000);
break;
case 2:
logger.println("Trying again after 5 seconds");
Thread.sleep(5000);
break;
case 3:
logger.println("Trying again after 30 seconds");
Thread.sleep(30000);
break;
default:
throw socketException;
}
}

}
} catch (InterruptedException i) {
logger.println("Thread was interrupted while waiting to re-attempt GET");
throw new CodeDxClientException("Thread was interrupted. Unabled to finish GET", -1);
}
//This shouldn't happen, but we all know how that assumption turns out.
throw new CodeDxClientException("GET was unsuccessful for " + path, -1);
}
}
12 changes: 2 additions & 10 deletions src/main/java/org/jenkinsci/plugins/codedx/CodeDxPublisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.jenkinsci.plugins.codedx;

import com.secdec.codedx.api.client.*;
import hudson.FilePath;
import hudson.Launcher;
import hudson.Extension;
Expand All @@ -39,15 +40,6 @@
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.QueryParameter;

import com.secdec.codedx.api.client.CodeDxClient;
import com.secdec.codedx.api.client.CodeDxClientException;
import com.secdec.codedx.api.client.CountGroup;
import com.secdec.codedx.api.client.Filter;
import com.secdec.codedx.api.client.Job;
import com.secdec.codedx.api.client.Project;
import com.secdec.codedx.api.client.StartAnalysisResponse;
import com.secdec.codedx.api.client.TriageStatus;

import javax.servlet.ServletException;

import java.io.IOException;
Expand Down Expand Up @@ -219,7 +211,7 @@ public boolean perform(

if (toSend.size() > 0) {

final CodeDxClient client = new CodeDxClient(url, key);
final CodeDxClient client = new CodeDxRepeatingClient(url, key, listener.getLogger());

try {
listener.getLogger().println("Sending analysis request");
Expand Down

0 comments on commit d934f2e

Please sign in to comment.