Skip to content

Commit

Permalink
Ansible-runner: list artifacts one by one
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mnecas committed Apr 5, 2022
1 parent fad80f2 commit d5f50e8
Showing 1 changed file with 18 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -98,27 +100,19 @@ public void setReturnValue(UUID uuid) {
returnValue.setLogFile(runnerLogger.getLogFile());
}

public List<String> getSortedEvents(String playUuid, int lastEventId) throws InterruptedException {
Boolean artifactsIsPopulated = false;
List<String> 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<String> 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() {
Expand All @@ -134,17 +128,9 @@ public int processEvents(String playUuid,
BiConsumer<String, String> fn,
String msg,
Path logFile) {
List<String> 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);

Expand Down Expand Up @@ -205,6 +191,8 @@ public int processEvents(String playUuid,
}
lastEvent = event;
returnValue.setLastEventId(getLastEventId());
lastEventId++;
event = getNextEvent(playUuid, lastEventId);
}
return lastEvent.isEmpty() ? lastEventId : getLastEventId();
}
Expand Down

0 comments on commit d5f50e8

Please sign in to comment.