mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-22 21:50:47 -05:00
chore: first commit
This commit is contained in:
69 files changed
+3697
No files matched your search
@@ -0,0 +1,79 @@
|
||||
package io.floci.cli.commands;
|
||||
|
||||
import io.floci.cli.GlobalOptions;
|
||||
import io.floci.cli.http.FlociHttpClient;
|
||||
import io.floci.cli.output.Ansi;
|
||||
import io.floci.cli.output.Printer;
|
||||
import picocli.CommandLine.*;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
@Command(
|
||||
name = "wait",
|
||||
description = "Wait until Floci is ready to accept requests",
|
||||
mixinStandardHelpOptions = true
|
||||
)
|
||||
public class WaitCommand implements Callable<Integer> {
|
||||
|
||||
@Mixin
|
||||
GlobalOptions global;
|
||||
|
||||
@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;
|
||||
|
||||
@Override
|
||||
public Integer call() {
|
||||
Printer printer = global.printer();
|
||||
long timeoutMillis = parseDuration(timeout);
|
||||
FlociHttpClient client = new FlociHttpClient(global.endpoint);
|
||||
Instant deadline = Instant.now().plusMillis(timeoutMillis);
|
||||
|
||||
while (Instant.now().isBefore(deadline)) {
|
||||
if (isReady(client, service)) {
|
||||
printer.println(Ansi.green("Floci is ready") + " (" + global.endpoint + ")");
|
||||
return 0;
|
||||
}
|
||||
printSpinner(printer, deadline);
|
||||
try { Thread.sleep(500); } catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
printer.error("Timed out waiting for Floci after " + timeout + ".\nIs the container running? Try 'floci status'.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
private boolean isReady(FlociHttpClient client, String requiredService) {
|
||||
try {
|
||||
var health = client.health();
|
||||
if (requiredService == null) return true;
|
||||
for (String s : health.services()) {
|
||||
if (s.equalsIgnoreCase(requiredService)) return true;
|
||||
}
|
||||
return false;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void printSpinner(Printer printer, Instant deadline) {
|
||||
long remaining = Duration.between(Instant.now(), deadline).toSeconds();
|
||||
printer.print("\r" + Ansi.gray("Waiting... (" + remaining + "s remaining)") + " ");
|
||||
}
|
||||
|
||||
public static long parseDuration(String s) {
|
||||
if (s == null || s.isBlank()) return 30_000;
|
||||
s = s.trim().toLowerCase();
|
||||
if (s.endsWith("ms")) return Long.parseLong(s.substring(0, s.length() - 2));
|
||||
if (s.endsWith("s")) return Long.parseLong(s.substring(0, s.length() - 1)) * 1000;
|
||||
if (s.endsWith("m")) return Long.parseLong(s.substring(0, s.length() - 1)) * 60_000;
|
||||
if (s.endsWith("h")) return Long.parseLong(s.substring(0, s.length() - 1)) * 3_600_000;
|
||||
return Long.parseLong(s) * 1000;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user