From 7866737054dcccf2801f24227a912c5e9c070141 Mon Sep 17 00:00:00 2001 From: "kirill.labutin" Date: Tue, 11 Feb 2025 02:18:16 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D1=84=D0=B8=D0=BA=D1=81=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=BF=D0=BE=D0=BB=D1=83=D1=87=D0=B5=D0=BD=D0=B8=D0=B5?= =?UTF-8?q?=20=D1=81=D1=82=D0=B0=D1=82=D1=83=D1=81=D0=B0=20=D0=BA=D0=BE?= =?UTF-8?q?=D0=BD=D1=82=D0=B5=D0=B9=D0=BD=D0=B5=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ru/kirillius/mktbk/App.java | 47 +++++++++++++++++------ 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/src/main/java/ru/kirillius/mktbk/App.java b/src/main/java/ru/kirillius/mktbk/App.java index 15e1d69..f87bd24 100644 --- a/src/main/java/ru/kirillius/mktbk/App.java +++ b/src/main/java/ru/kirillius/mktbk/App.java @@ -25,7 +25,7 @@ public class App { backupFolder = new File(BACKUP_FOLDER); - if(!backupFolder.exists()){ + if (!backupFolder.exists()) { backupFolder.mkdir(); } @@ -102,7 +102,7 @@ public class App { backupName = suggestion; } - var outputFile = new File(backupFolder,backupName + ".tar.gz"); + var outputFile = new File(backupFolder, backupName + ".tar.gz"); List files; @@ -112,7 +112,7 @@ public class App { 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); } container = validate(container); @@ -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 apply(); + } + + @SuppressWarnings("BusyWait") + private void waitWhile(Functor 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); - 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."); - } + waitWhile(() -> { + try { + updateContainers(); + return !validate(container).isRunning(); + } catch (MikrotikApiException e) { + throw new RuntimeException(e); + } + }, 10000L); } } catch (Exception e) { throw new RuntimeException(e);