Пофиксил получение статуса контейнера

This commit is contained in:
kirill.labutin 2025-02-11 02:18:16 +03:00
parent 9e31bfe72f
commit 7866737054
1 changed files with 36 additions and 11 deletions

View File

@ -130,6 +130,7 @@ public class App {
layerFile.delete();
}
SystemLogger.message("Backup is done ", LOG_CONTEXT);
console.pause();
if (!container.isRunning() && console.confirm("Do you want to start container?")) {
startContainer(container);
}
@ -140,7 +141,14 @@ public class App {
SystemLogger.message("Stopping container " + container, LOG_CONTEXT);
try {
device.getApiConnection().execute("/container/stop .id=" + container.getId());
Thread.sleep(10000L);
waitWhile(() -> {
try {
updateContainers();
return validate(container).isRunning();
} catch (MikrotikApiException e) {
throw new RuntimeException(e);
}
}, 10000L);
} catch (MikrotikApiException | InterruptedException e) {
throw new RuntimeException("Error stopping container", e);
}
@ -159,18 +167,35 @@ public class App {
}
}
public interface Functor<T> {
T apply();
}
@SuppressWarnings("BusyWait")
private void waitWhile(Functor<Boolean> condition, long timeout) throws InterruptedException {
var startTime = System.currentTimeMillis();
while (condition.apply()) {
if (System.currentTimeMillis() - startTime > timeout) {
throw new InterruptedException();
}
Thread.sleep(1000);
}
}
private void startContainer(ContainerMetadata container) {
try {
if (!container.isRunning()) {
SystemLogger.message("Starting container " + container, LOG_CONTEXT);
device.getApiConnection().execute("/container/start .id=" + container.getId());
Thread.sleep(10000L);
waitWhile(() -> {
try {
updateContainers();
var id = container.getId();
container = containers.stream().filter(containerMetadata -> containerMetadata.getId().equals(id)).findFirst().orElse(null);
if (container == null || !container.isRunning()) {
throw new RuntimeException("Something went wrong. Unable to start container.");
return !validate(container).isRunning();
} catch (MikrotikApiException e) {
throw new RuntimeException(e);
}
}, 10000L);
}
} catch (Exception e) {
throw new RuntimeException(e);