Skip to content

Commit

Permalink
Use loop waiting for process
Browse files Browse the repository at this point in the history
Fixes #12
  • Loading branch information
sverhoeven committed Nov 8, 2017
1 parent ca06d8c commit eca5e59
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public ExecutableFieldEditor(String name, String labelText, boolean enforceAbsol
protected boolean checkState() {
if (this.getStringValue() != null && this.getStringValue().length() > 0) {
try {
new ProcessBuilder().command(this.getStringValue()).start().waitFor();
new ProcessBuilder().command(this.getStringValue()).start().waitFor();
// Not interested in exitCode, just want to know if it can be
// executed
clearErrorMessage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected BufferedDataTable[] execute(BufferedDataTable[] inData, ExecutionConte
List<String> arguments = StreamSupport.stream(argumentsCell.spliterator(), false)
.map(c -> ((StringCell) c).getStringValue()).collect(Collectors.toList());
File workingDirectory = new File(((StringCell) inRow.getCell(workingDirectoryIndex)).getStringValue());
DataRow row = process(inRow.getKey(), workingDirectory, executable, mode, arguments);
DataRow row = process(inRow.getKey(), workingDirectory, executable, mode, arguments, exec);
container.addRowToTable(row);
exec.checkCanceled();
exec.setProgress(0.9 * currentRow / rowCount, " processing row " + currentRow);
Expand All @@ -72,8 +72,9 @@ protected BufferedDataTable[] execute(BufferedDataTable[] inData, ExecutionConte
return new BufferedDataTable[] { out };
}

public DataRow process(RowKey rowKey, File workingDirectory, String executable, String mode, List<String> arguments)
throws IOException, InterruptedException {
public DataRow process(RowKey rowKey, File workingDirectory, String executable, String mode, List<String> arguments,
ExecutionContext context)
throws IOException, InterruptedException, CanceledExecutionException {
List<String> commands = new ArrayList<>();
commands.add(executable);
commands.add("--mode");
Expand All @@ -85,7 +86,13 @@ public DataRow process(RowKey rowKey, File workingDirectory, String executable,
File stdout = new File(workingDirectory, "stdout.txt");
Process process = new ProcessBuilder(commands).directory(workingDirectory).redirectError(stderr).redirectOutput(stdout)
.start();
int exitCode = process.waitFor();
try {
process.waitFor();
} catch (InterruptedException e) {
process.destroy();
throw e;
}
int exitCode = process.exitValue();
if (exitCode != 0) {
setWarningMessage("Some rows failed to run correctly");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.knime.core.data.RowKey;
import org.knime.core.data.def.DefaultRow;
import org.knime.core.data.def.IntCell;
import org.knime.core.node.ExecutionMonitor;

public class RunNodeModelTest {
@Rule
Expand All @@ -30,8 +31,9 @@ public void testProcess() throws IOException, InterruptedException {
String executable = "/bin/echo";
String mode = "bind";
List<String> arguments = Arrays.asList("arg1", "arg2");
ExecutionMonitor context = new ExecutionMonitor();

DataRow row = node.process(rowKey, workingDirectory, executable, mode, arguments);
DataRow row = node.process(rowKey, workingDirectory, executable, mode, arguments, context);

DefaultRow expected = new DefaultRow(rowKey, new IntCell(0));
assertEquals(expected, row);
Expand Down

0 comments on commit eca5e59

Please sign in to comment.