diff --git a/src/main/java/org/rundeck/client/tool/commands/Executions.java b/src/main/java/org/rundeck/client/tool/commands/Executions.java index e2c10f79..defc6d0d 100644 --- a/src/main/java/org/rundeck/client/tool/commands/Executions.java +++ b/src/main/java/org/rundeck/client/tool/commands/Executions.java @@ -16,6 +16,7 @@ import java.io.IOException; import java.util.*; +import java.util.function.BooleanSupplier; import java.util.function.Function; import java.util.stream.Collectors; @@ -108,6 +109,21 @@ public static Call startFollowOutput( return out; } + /** + * Follow output, wait 2s between refreshing data from server, halts when interrupted + * + * @param client + * @param output + * @param progress + * @param quiet + * @param id + * @param max + * @param out + * + * @return + * + * @throws IOException + */ public static boolean followOutput( final Client client, final Call output, @@ -117,6 +133,41 @@ public static boolean followOutput( long max, CommandOutput out ) throws IOException + { + return followOutput(client, output, progress, quiet, id, max, out, () -> { + try { + Thread.sleep(2000); + return true; + } catch (InterruptedException e) { + return false; + } + }); + } + + /** + * @param client + * @param output + * @param progress + * @param quiet + * @param id + * @param max + * @param out + * @param waitFunc function for waiting, return false to halt + * + * @return + * + * @throws IOException + */ + public static boolean followOutput( + final Client client, + final Call output, + final boolean progress, + final boolean quiet, + final String id, + long max, + CommandOutput out, + BooleanSupplier waitFunc + ) throws IOException { boolean done = false; String status = null; @@ -125,14 +176,11 @@ public static boolean followOutput( ExecOutput execOutput = client.checkError(callOutput); printLogOutput(execOutput.entries, progress, quiet, out); status = execOutput.execState; - done = !"running".equals(status); + done = execOutput.execCompleted && execOutput.completed; if (!done) { - try { - Thread.sleep(2000); - } catch (InterruptedException e) { + if (!waitFunc.getAsBoolean()){ break; } - callOutput = client.getService().getOutput(id, execOutput.offset, execOutput.lastModified, max); } } diff --git a/src/test/groovy/org/rundeck/client/tool/commands/ExecutionsSpec.groovy b/src/test/groovy/org/rundeck/client/tool/commands/ExecutionsSpec.groovy index 47e3c24c..e04aeec8 100644 --- a/src/test/groovy/org/rundeck/client/tool/commands/ExecutionsSpec.groovy +++ b/src/test/groovy/org/rundeck/client/tool/commands/ExecutionsSpec.groovy @@ -22,6 +22,8 @@ class ExecutionsSpec extends Specification { ExecOutput execOutputFinal = new ExecOutput() execOutputFinal.execState = finalState + execOutputFinal.execCompleted = true + execOutputFinal.completed = true execOutputFinal.entries = [] def api = Mock(RundeckApi) @@ -30,28 +32,40 @@ class ExecutionsSpec extends Specification { def client = new Client(api, retrofit, 18) ExecOutput execOutput = new ExecOutput() - execOutput.execState = 'running' + execOutput.execState = initState execOutput.offset = 123 execOutput.lastModified = 01L execOutput.entries = [] + execOutput.execCompleted = initExecCompleted + execOutput.completed = initCompleted def output = Calls.response(execOutput) when: - boolean result = Executions.followOutput(client, output, progress, quiet, id, max, Mock(CommandOutput)) + boolean result = Executions.followOutput(client, output, progress, quiet, id, max, Mock(CommandOutput)) { + -> true + } then: 1 * api.getOutput(id, 123, 01L, max) >> Calls.response(execOutputFinal) result == exit where: - finalState | exit - 'succeeded' | true - 'succeeded' | true - 'failed' | false - 'failed' | false + initState | initExecCompleted | initCompleted | finalState | exit + 'running' | false | false | 'succeeded' | true + 'running' | true | false | 'succeeded' | true + 'running' | false | true | 'succeeded' | true + 'running' | false | false | 'failed' | false + 'running' | true | false | 'failed' | false + 'running' | false | true | 'failed' | false + 'scheduled' | false | false | 'succeeded' | true + 'scheduled' | true | false | 'succeeded' | true + 'scheduled' | false | true | 'succeeded' | true + 'scheduled' | false | false | 'failed' | false + 'scheduled' | true | false | 'failed' | false + 'scheduled' | false | true | 'failed' | false } }