Skip to content

Commit

Permalink
Merge pull request #70 from rundeck/rundeck-cli-65
Browse files Browse the repository at this point in the history
Fix #65 run -f halts before job execution finishes
  • Loading branch information
gschueler authored Feb 2, 2017
2 parents 9ae691c + a375011 commit 13f0eea
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 12 deletions.
58 changes: 53 additions & 5 deletions src/main/java/org/rundeck/client/tool/commands/Executions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -108,6 +109,21 @@ public static Call<ExecOutput> 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<RundeckApi> client,
final Call<ExecOutput> output,
Expand All @@ -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<RundeckApi> client,
final Call<ExecOutput> output,
final boolean progress,
final boolean quiet,
final String id,
long max,
CommandOutput out,
BooleanSupplier waitFunc
) throws IOException
{
boolean done = false;
String status = null;
Expand All @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

}
}

0 comments on commit 13f0eea

Please sign in to comment.