perf: restart without a fixed pause and wait no longer than --timeout (#36)

This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-08 22:41:35 -05:00
1 parent fc67e72a62
commit 01d6133548
9 files changed
+407 -34

No files matched your search

@@ -2,11 +2,13 @@ package io.floci.cli.docker;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
/**
* Thin subprocess wrapper around the docker CLI.
@@ -14,6 +16,17 @@ import java.util.Optional;
*/
public class DockerClient {
private final String binary;
public DockerClient() {
this("docker");
}
/** Test seam: run {@code binary} in place of {@code docker}. */
public DockerClient(String binary) {
this.binary = binary;
}
public record ContainerInfo(
String id,
String name,
@@ -129,16 +142,23 @@ public class DockerClient {
}
private String run(String... cmd) throws DockerException {
Process proc = null;
try {
Process proc = runProcess(cmd);
String stdout = new String(proc.getInputStream().readAllBytes());
String stderr = new String(proc.getErrorStream().readAllBytes());
proc = runProcess(cmd);
// Both streams drain at once, so a full stderr pipe cannot stall the process, and the
// wait below is interruptible: an abandoned lookup kills its process instead of
// leaving it running after the CLI exits.
CompletableFuture<String> stdout = drain(proc.getInputStream());
CompletableFuture<String> stderr = drain(proc.getErrorStream());
int code = proc.waitFor();
String out = stdout.join();
String err = stderr.join();
if (code != 0) {
throw new DockerException(stderr.isBlank() ? stdout : stderr);
throw new DockerException(err.isBlank() ? out : err);
}
return stdout;
return out;
} catch (InterruptedException e) {
if (proc != null) proc.destroyForcibly();
Thread.currentThread().interrupt();
throw new DockerException("Interrupted");
} catch (IOException e) {
@@ -178,7 +198,23 @@ public class DockerClient {
}
}
private static CompletableFuture<String> drain(InputStream stream) {
CompletableFuture<String> text = new CompletableFuture<>();
Thread.ofVirtual().start(() -> {
try (stream) {
text.complete(new String(stream.readAllBytes()));
} catch (IOException e) {
text.complete("");
}
});
return text;
}
private Process runProcess(String... cmd) throws IOException {
if (cmd.length > 0 && "docker".equals(cmd[0])) {
cmd = cmd.clone();
cmd[0] = binary;
}
return new ProcessBuilder(cmd)
.redirectErrorStream(false)
.start();