Skip to content

Commit

Permalink
Merge pull request #6 from gfleury/handling-ioexceptions
Browse files Browse the repository at this point in the history
Handling ioexceptions
  • Loading branch information
gfleury authored Sep 1, 2018
2 parents 907e72a + 5da2dfc commit 308d832
Showing 1 changed file with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.tsuru.client.ApiException;
import io.tsuru.client.api.TsuruApi;
import io.tsuru.client.model.Application;
import io.tsuru.client.model.Deployments;
import org.jenkinsci.plugins.tsuru.utils.TarGzip;
import org.jenkinsci.plugins.workflow.cps.EnvActionImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractStepDescriptorImpl;
Expand Down Expand Up @@ -234,9 +235,47 @@ protected Execution run() throws Exception {
getListener().getLogger().println("[app-deploy] Starting Tsuru application deployment ========>");
int timeout = step.apiInstance.getApiClient().getReadTimeout();
step.apiInstance.getApiClient().setReadTimeout(600000); // Same BuildTimeout than TSURU
String output = step.apiInstance.appDeploy(step.Args.get("appName"), deploymentFile, step.Args.get("imageTag"), step.Args.get("message"), step.Args.get("commit"));
step.apiInstance.getApiClient().setReadTimeout(timeout);
getListener().getLogger().println(output);
String output = "";
try {
output = step.apiInstance.appDeploy(step.Args.get("appName"), deploymentFile, step.Args.get("imageTag"), step.Args.get("message"), step.Args.get("commit"));
} catch (io.tsuru.client.ApiException e) {
if (e.getCause() instanceof java.io.IOException) {
int counter = 0;
String id = "";
getListener().getLogger().println("[app-deploy] Logs will be truncated, please check the logs directly on Tsuru!");
do {
List<Deployments> deploys = step.apiInstance.appDeployList(step.Args.get("appName"), 1);
if (deploys.size() > 0) {
Deployments deployment = deploys.get(0);
if (id.length() == 0) {
id = deployment.getId();
} else if (!id.contains(deployment.getId())) {
getListener().getLogger().println("[app-deploy] Another deployment started in the meanwhile! Aborting this one!");
break;
}
if (!deployment.getDuration().startsWith("-") && (deployment.getImage().length() != 0 || deployment.getError().length() != 0)) {
output = step.apiInstance.appLog(step.Args.get("appName"), 20);
if (deployment.getImage().length() != 0) {
output += "\nOK\n";
}
break;
}
} else {
// No deployments at all
getListener().getLogger().println("[app-deploy] No deployment was found!");
break;
}
Thread.sleep(5000 + (counter * 500));
counter++;
} while (counter < 20);
} else {
// TODO: Better handling on unauthorized and conflict (another deployment in course)
throw e;
}
} finally {
getListener().getLogger().println(output);
step.apiInstance.getApiClient().setReadTimeout(timeout);
}
if (!output.endsWith("OK\n")) {
throw new ApiException("[app-deploy] Tsuru deployment FAILED ˆˆˆˆˆˆˆˆˆ");
}
Expand Down

0 comments on commit 308d832

Please sign in to comment.