Skip to content

Commit

Permalink
Cancel jobs that failed rendering (e.g. timed out).
Browse files Browse the repository at this point in the history
  • Loading branch information
leMaik committed Nov 22, 2020
1 parent 31733d9 commit 4c71892
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
24 changes: 23 additions & 1 deletion src/main/java/de/lemaik/chunkymap/rendering/rs/ApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -395,6 +394,29 @@ public void onResponse(Call call, Response response) {
return result;
}

public CompletableFuture<Void> cancelJob(String jobId) {
CompletableFuture<Void> 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<TaskTracker.Task> taskCreator) {
TaskTracker.Task task = taskCreator.get();
return new RequestBody() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public CompletableFuture<BufferedImage> render(FileBufferRenderContext context,
Scene scene = context.getChunky().getSceneFactory().newScene();
initializeScene.accept(scene);

RenderJob job;
RenderJob job = null;
try {
if (initializeLocally) {
job = api
Expand All @@ -63,6 +63,12 @@ public CompletableFuture<BufferedImage> 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);
}
}
Expand Down

0 comments on commit 4c71892

Please sign in to comment.