mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-22 21:50:47 -05:00
perf: restart without a fixed pause and wait no longer than --timeout (#36)
This commit is contained in:
1 parent
fc67e72a62
commit
01d6133548
9 files changed
+407
-34
No files matched your search
@@ -2,6 +2,7 @@ package io.floci.cli.commands;
|
||||
|
||||
import io.floci.cli.GlobalOptions;
|
||||
import io.floci.cli.ProductProfile;
|
||||
import io.floci.cli.docker.DockerClient;
|
||||
import io.floci.cli.http.FlociHttpClient;
|
||||
import io.floci.cli.util.Durations;
|
||||
import io.floci.cli.output.Ansi;
|
||||
@@ -14,6 +15,10 @@ import picocli.CommandLine.*;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
@Command(
|
||||
name = "wait",
|
||||
@@ -32,26 +37,48 @@ public class WaitCommand implements Callable<Integer> {
|
||||
}
|
||||
|
||||
protected WaitCommand(ProductProfile profile) {
|
||||
this(profile, new DockerClient());
|
||||
}
|
||||
|
||||
/** Test seam: {@code docker} answers the container lookup that finds the endpoint. */
|
||||
public WaitCommand(ProductProfile profile, DockerClient docker) {
|
||||
this.profile = profile;
|
||||
this.global = new GlobalOptions(profile);
|
||||
this.docker = docker;
|
||||
}
|
||||
|
||||
private final DockerClient docker;
|
||||
|
||||
@Option(names = {"--timeout"}, description = "Maximum time to wait (e.g. 30s, 2m)", defaultValue = "30s", paramLabel = "<duration>")
|
||||
String timeout;
|
||||
|
||||
@Option(names = {"--service"}, description = "Wait until a specific service is enabled", paramLabel = "<name>")
|
||||
String service;
|
||||
|
||||
/**
|
||||
* Set by {@code start}, which already knows the port it bound: skips the {@code docker inspect}
|
||||
* that resolving the endpoint from the container would cost.
|
||||
*/
|
||||
String knownEndpoint;
|
||||
|
||||
static final long FIRST_DELAY_MS = 25;
|
||||
static final long MAX_DELAY_MS = 500;
|
||||
static final Duration MAX_REQUEST_TIMEOUT = Duration.ofSeconds(10);
|
||||
|
||||
@Override
|
||||
public Integer call() {
|
||||
Printer printer = global.printer();
|
||||
long timeoutMillis = Durations.parseDuration(timeout);
|
||||
String effectiveEndpoint = global.resolvedEndpoint(new io.floci.cli.docker.DockerClient());
|
||||
FlociHttpClient client = new FlociHttpClient(effectiveEndpoint, profile.controlPrefix());
|
||||
// The deadline starts now, so the container lookup below counts against --timeout too.
|
||||
Instant deadline = Instant.now().plusMillis(timeoutMillis);
|
||||
String effectiveEndpoint = knownEndpoint != null ? knownEndpoint : endpointWithin(deadline);
|
||||
FlociHttpClient client = new FlociHttpClient(effectiveEndpoint, profile.controlPrefix());
|
||||
|
||||
// Poll soon after a start, then back off; never let a request or a pause run past the
|
||||
// deadline, so --timeout is a real upper bound.
|
||||
long delay = FIRST_DELAY_MS;
|
||||
while (Instant.now().isBefore(deadline)) {
|
||||
if (isReady(client, service)) {
|
||||
if (isReady(client, service, requestTimeout(Duration.between(Instant.now(), deadline)))) {
|
||||
if (printer.format() != OutputFormat.text) {
|
||||
printer.structured(Map.of("ready", true, "endpoint", effectiveEndpoint));
|
||||
} else {
|
||||
@@ -60,10 +87,13 @@ public class WaitCommand implements Callable<Integer> {
|
||||
return 0;
|
||||
}
|
||||
printSpinner(printer, deadline);
|
||||
try { Thread.sleep(500); } catch (InterruptedException e) {
|
||||
long remaining = Duration.between(Instant.now(), deadline).toMillis();
|
||||
if (remaining <= 0) break;
|
||||
try { Thread.sleep(Math.min(delay, remaining)); } catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
break;
|
||||
}
|
||||
delay = nextDelay(delay);
|
||||
}
|
||||
|
||||
printer.error("Timed out waiting for " + profile.displayName() + " after " + timeout
|
||||
@@ -71,9 +101,48 @@ public class WaitCommand implements Callable<Integer> {
|
||||
return 1;
|
||||
}
|
||||
|
||||
private boolean isReady(FlociHttpClient client, String requiredService) {
|
||||
// A stalled daemon or an unreachable remote DOCKER_HOST can hold `docker inspect` far longer
|
||||
// than --timeout, so the lookup gets the time left and the configured endpoint is the fallback.
|
||||
private String endpointWithin(Instant deadline) {
|
||||
long remaining = Duration.between(Instant.now(), deadline).toMillis();
|
||||
if (remaining <= 0) return global.endpoint;
|
||||
// A detached virtual thread: a hung lookup is abandoned, not joined, and cannot keep the
|
||||
// JVM alive once wait returns.
|
||||
CompletableFuture<String> resolved = new CompletableFuture<>();
|
||||
Thread lookup = Thread.ofVirtual().name("floci-wait-endpoint").start(() -> {
|
||||
try {
|
||||
resolved.complete(global.resolvedEndpoint(docker));
|
||||
} catch (RuntimeException e) {
|
||||
resolved.complete(global.endpoint);
|
||||
}
|
||||
});
|
||||
try {
|
||||
var health = client.health();
|
||||
return resolved.get(remaining, TimeUnit.MILLISECONDS);
|
||||
} catch (TimeoutException | ExecutionException e) {
|
||||
lookup.interrupt(); // DockerClient kills the docker process it was waiting on
|
||||
return global.endpoint;
|
||||
} catch (InterruptedException e) {
|
||||
lookup.interrupt();
|
||||
Thread.currentThread().interrupt();
|
||||
return global.endpoint;
|
||||
}
|
||||
}
|
||||
|
||||
/** 25 ms, 50, 200, 400, then 500 ms between polls: an emulator is often up within
|
||||
* a few dozen milliseconds of `docker run`, and a refused local poll costs nothing. */
|
||||
public static long nextDelay(long previous) {
|
||||
return Math.min(previous * 2, MAX_DELAY_MS);
|
||||
}
|
||||
|
||||
/** A request may use the time left, up to the usual 10 s, and at least a millisecond. */
|
||||
public static Duration requestTimeout(Duration remaining) {
|
||||
if (remaining.compareTo(MAX_REQUEST_TIMEOUT) > 0) return MAX_REQUEST_TIMEOUT;
|
||||
return remaining.isNegative() || remaining.isZero() ? Duration.ofMillis(1) : remaining;
|
||||
}
|
||||
|
||||
private boolean isReady(FlociHttpClient client, String requiredService, Duration timeout) {
|
||||
try {
|
||||
var health = client.health(timeout);
|
||||
if (requiredService == null) return true;
|
||||
for (String s : health.services()) {
|
||||
if (s.equalsIgnoreCase(requiredService)) return true;
|
||||
|
||||
Reference in new issue
Block a user