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

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

@ -25,7 +25,7 @@ public class App {
backupFolder = new File(BACKUP_FOLDER); backupFolder = new File(BACKUP_FOLDER);
if(!backupFolder.exists()){ if (!backupFolder.exists()) {
backupFolder.mkdir(); backupFolder.mkdir();
} }
@ -102,7 +102,7 @@ public class App {
backupName = suggestion; backupName = suggestion;
} }
var outputFile = new File(backupFolder,backupName + ".tar.gz"); var outputFile = new File(backupFolder, backupName + ".tar.gz");
List<FileMetadata> files; List<FileMetadata> files;
@ -112,7 +112,7 @@ public class App {
throw new RuntimeException("Error loading container file tree", e); throw new RuntimeException("Error loading container file tree", e);
} }
if(console.confirm("Container can be stopped now. Stop it?")) { if (console.confirm("Container can be stopped now. Stop it?")) {
stopContainer(container); stopContainer(container);
} }
container = validate(container); container = validate(container);
@ -130,6 +130,7 @@ public class App {
layerFile.delete(); layerFile.delete();
} }
SystemLogger.message("Backup is done ", LOG_CONTEXT); SystemLogger.message("Backup is done ", LOG_CONTEXT);
console.pause();
if (!container.isRunning() && console.confirm("Do you want to start container?")) { if (!container.isRunning() && console.confirm("Do you want to start container?")) {
startContainer(container); startContainer(container);
} }
@ -140,7 +141,14 @@ public class App {
SystemLogger.message("Stopping container " + container, LOG_CONTEXT); SystemLogger.message("Stopping container " + container, LOG_CONTEXT);
try { try {
device.getApiConnection().execute("/container/stop .id=" + container.getId()); 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) { } catch (MikrotikApiException | InterruptedException e) {
throw new RuntimeException("Error stopping container", 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) { private void startContainer(ContainerMetadata container) {
try { try {
if (!container.isRunning()) { if (!container.isRunning()) {
SystemLogger.message("Starting container " + container, LOG_CONTEXT); SystemLogger.message("Starting container " + container, LOG_CONTEXT);
device.getApiConnection().execute("/container/start .id=" + container.getId()); device.getApiConnection().execute("/container/start .id=" + container.getId());
Thread.sleep(10000L); waitWhile(() -> {
try {
updateContainers(); updateContainers();
var id = container.getId(); return !validate(container).isRunning();
container = containers.stream().filter(containerMetadata -> containerMetadata.getId().equals(id)).findFirst().orElse(null); } catch (MikrotikApiException e) {
if (container == null || !container.isRunning()) { throw new RuntimeException(e);
throw new RuntimeException("Something went wrong. Unable to start container.");
} }
}, 10000L);
} }
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);