From d5f50e807a4da5654c5bf2696704225812c2e0bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ne=C4=8Das?= Date: Tue, 5 Apr 2022 19:25:33 +0200 Subject: [PATCH] Ansible-runner: list artifacts one by one AnsibleRunnerHttpClient was listing all new artifacts in artifacts dir becuase ansible-runner does not create the artifacts one by one. When we are logging we need to wait for the proper artifacts to be generated and then we can log it. --- .../ansible/AnsibleRunnerHttpClient.java | 48 +++++++------------ 1 file changed, 18 insertions(+), 30 deletions(-) diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/common/utils/ansible/AnsibleRunnerHttpClient.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/common/utils/ansible/AnsibleRunnerHttpClient.java index e1f2d4937fae..7933a45810b0 100644 --- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/common/utils/ansible/AnsibleRunnerHttpClient.java +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/common/utils/ansible/AnsibleRunnerHttpClient.java @@ -16,6 +16,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.Set; import java.util.UUID; import java.util.concurrent.TimeUnit; @@ -88,6 +89,7 @@ public AnsibleReturnValue artifactHandler(UUID uuid, int lastEventID, int timeou } lastEventID = processEvents(uuid.toString(), lastEventID, fn, "", Paths.get("")); iteration += POLL_INTERVAL / 1000; + Thread.sleep(POLL_INTERVAL); } returnValue.setAnsibleReturnCode(AnsibleReturnCode.OK); return returnValue; @@ -98,27 +100,19 @@ public void setReturnValue(UUID uuid) { returnValue.setLogFile(runnerLogger.getLogFile()); } - public List getSortedEvents(String playUuid, int lastEventId) throws InterruptedException { - Boolean artifactsIsPopulated = false; - List sortedEvents = new ArrayList<>(); - - while (!artifactsIsPopulated) { - Thread.sleep(1500); - - // ignoring incompleted json files, add to list only events that haven't been handles yet. - String jobEvents = getJobEventsDir(playUuid); - if (Files.exists(Paths.get(jobEvents))) { - sortedEvents = Stream.of(new File(jobEvents).listFiles()) - .map(File::getName) - .filter(item -> !item.contains("partial")) - .filter(item -> !item.endsWith(".tmp")) - .filter(item -> (Integer.valueOf(item.split("-")[0])) > lastEventId) - .sorted() - .collect(Collectors.toList()); - artifactsIsPopulated = true; - } + public String getNextEvent(String playUuid, int lastEventId) { + Optional nextEvent = Optional.empty(); + // ignoring incompleted json files, add to list only events that haven't been handles yet. + String jobEvents = getJobEventsDir(playUuid); + if (Files.exists(Paths.get(jobEvents))) { + nextEvent = Stream.of(new File(jobEvents).listFiles()) + .map(File::getName) + .filter(item -> !item.contains("partial")) + .filter(item -> !item.endsWith(".tmp")) + .filter(item -> item.startsWith((lastEventId + 1) + "-")) + .findFirst(); } - return sortedEvents; + return nextEvent.isPresent() ? nextEvent.get() : null; } public int getLastEventId() { @@ -134,17 +128,9 @@ public int processEvents(String playUuid, BiConsumer fn, String msg, Path logFile) { - List sortedEvents = new ArrayList<>(); - try { - sortedEvents = getSortedEvents(playUuid, lastEventId); - } catch (InterruptedException ex) { - throw new AnsibleRunnerCallException( - "Failed to get list of events for play: %1$s", - playUuid); - } - String jobEvents = getJobEventsDir(playUuid); - for (String event : sortedEvents) { + String event = getNextEvent(playUuid, lastEventId); + while(event != null){ JsonNode currentNode = getEvent(jobEvents + event); String stdout = RunnerJsonNode.getStdout(currentNode); @@ -205,6 +191,8 @@ public int processEvents(String playUuid, } lastEvent = event; returnValue.setLastEventId(getLastEventId()); + lastEventId++; + event = getNextEvent(playUuid, lastEventId); } return lastEvent.isEmpty() ? lastEventId : getLastEventId(); }