Skip to content

Commit

Permalink
core: fix lastEventId tracking
Browse files Browse the repository at this point in the history
due to singleton usage we may be reading wrong lastEventId, but it is
not really needed so let's siplify the logic and use the existing
AnsibleReturnValue for keeping track of it.
  • Loading branch information
michalskrivanek committed Jul 21, 2022
1 parent 596d12d commit f0ef650
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ public AnsibleReturnValue runCommand(AnsibleCommandConfig commandConfig, int tim
}

AnsibleReturnValue ret = new AnsibleReturnValue(AnsibleReturnCode.ERROR);
int lastEventId = 0;

String playUuid = null;
String msg = "";
Expand All @@ -158,6 +157,7 @@ public AnsibleReturnValue runCommand(AnsibleCommandConfig commandConfig, int tim
ret.setLogFile(runnerClient.getLogger().getLogFile());
ret.setPlayUuid(playUuid);
ret.setStdout(String.format("%1$s/%2$s/artifacts/%2$s/stdout", AnsibleConstants.ANSIBLE_RUNNER_PATH, playUuid));
ret.setLastEventId(0);
List<String> command = commandConfig.build();

// Run the playbook:
Expand All @@ -168,7 +168,7 @@ public AnsibleReturnValue runCommand(AnsibleCommandConfig commandConfig, int tim
return ret;
}

ret = runnerClient.artifactHandler(commandConfig.getUuid(), lastEventId, timeout, fn);
ret = runnerClient.artifactHandler(commandConfig.getUuid(), ret.getLastEventId(), timeout, fn);
} catch (InventoryException ex) {
String message = ex.getMessage();
log.error("Error executing playbook: {}", message);
Expand All @@ -182,8 +182,7 @@ public AnsibleReturnValue runCommand(AnsibleCommandConfig commandConfig, int tim
} finally {
// Make sure all events are proccessed even in case of failure:
if (playUuid != null && runnerClient != null && !async) {
lastEventId = runnerClient.getLastEventId();
runnerClient.processEvents(playUuid, lastEventId, fn, msg, ret.getLogFile());
runnerClient.processEvents(playUuid, ret.getLastEventId(), fn, msg, ret.getLogFile());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public class AnsibleRunnerClient {
private static Logger log = LoggerFactory.getLogger(AnsibleRunnerClient.class);
private ObjectMapper mapper;
private AnsibleRunnerLogger runnerLogger;
private String lastEvent = "";
private static final int POLL_INTERVAL = 3000;
private AnsibleReturnValue returnValue;

Expand Down Expand Up @@ -108,10 +107,6 @@ public String getNextEvent(String playUuid, int lastEventId) {
.orElse(null);
}

public int getLastEventId() {
return Integer.valueOf(lastEvent.split("-")[0]);
}

public String getJobEventsDir(String playUuid) {
return String.format("%1$s/%2$s/artifacts/%2$s/job_events/", AnsibleConstants.ANSIBLE_RUNNER_PATH, playUuid);
}
Expand Down Expand Up @@ -187,11 +182,10 @@ public int processEvents(String playUuid,
}
}
}
lastEvent = event;
returnValue.setLastEventId(getLastEventId());
lastEventId++;
lastEventId = Integer.valueOf(event.split("-")[0]);
returnValue.setLastEventId(lastEventId);
}
return lastEvent.isEmpty() ? lastEventId : getLastEventId();
return lastEventId;
}

private Boolean jsonIsValid(String content) {
Expand Down

0 comments on commit f0ef650

Please sign in to comment.