diff --git a/src/main/java/de/lemaik/chunkymap/rendering/rs/ApiClient.java b/src/main/java/de/lemaik/chunkymap/rendering/rs/ApiClient.java index de40156..45e375b 100644 --- a/src/main/java/de/lemaik/chunkymap/rendering/rs/ApiClient.java +++ b/src/main/java/de/lemaik/chunkymap/rendering/rs/ApiClient.java @@ -29,7 +29,6 @@ import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; -import java.io.InputStreamReader; import java.io.Reader; import java.net.URL; import java.util.List; @@ -395,6 +394,29 @@ public void onResponse(Call call, Response response) { return result; } + public CompletableFuture cancelJob(String jobId) { + CompletableFuture result = new CompletableFuture<>(); + client.newCall(new Request.Builder() + .url(baseUrl + "/jobs/" + jobId) + .patch(new MultipartBody.Builder().addFormDataPart("action", "cancel").build()).build()) + .enqueue(new Callback() { + @Override + public void onFailure(Call call, IOException e) { + result.completeExceptionally(e); + } + + @Override + public void onResponse(Call call, Response response) { + if (response.code() == 204) { + result.complete(null); + } else { + result.completeExceptionally(new IOException("The job could not be downloaded")); + } + } + }); + return result; + } + private static RequestBody fileBody(final File file, Supplier taskCreator) { TaskTracker.Task task = taskCreator.get(); return new RequestBody() { diff --git a/src/main/java/de/lemaik/chunkymap/rendering/rs/RemoteRenderer.java b/src/main/java/de/lemaik/chunkymap/rendering/rs/RemoteRenderer.java index 70f2b4b..b9c4f6c 100644 --- a/src/main/java/de/lemaik/chunkymap/rendering/rs/RemoteRenderer.java +++ b/src/main/java/de/lemaik/chunkymap/rendering/rs/RemoteRenderer.java @@ -46,7 +46,7 @@ public CompletableFuture render(FileBufferRenderContext context, Scene scene = context.getChunky().getSceneFactory().newScene(); initializeScene.accept(scene); - RenderJob job; + RenderJob job = null; try { if (initializeLocally) { job = api @@ -63,6 +63,12 @@ public CompletableFuture render(FileBufferRenderContext context, api.waitForCompletion(job, 30, TimeUnit.MINUTES).get(); return CompletableFuture.completedFuture(api.getPicture(job.getId())); } catch (InterruptedException | ExecutionException e) { + if (job != null) { + try { + api.cancelJob(job.getId()).get(); + } catch (InterruptedException | ExecutionException ignore) { + } + } throw new IOException("Rendering failed", e); } }