Skip to content

Commit 6ade5fd

Browse files
committed
[Fix #1774] Adding stdin support in scripts
Signed-off-by: Francisco Javier Tirado Sarti <ftirados@ibm.com>
1 parent 7d0df86 commit 6ade5fd

5 files changed

Lines changed: 35 additions & 11 deletions

File tree

‎impl/core/src/main/java/org/openworkflow/sdk/impl/executors/RunScriptExecutor.java‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,23 @@ public class RunScriptExecutor implements CallableTask {
3737
private final boolean isAwait;
3838
private final RunTaskConfiguration.ProcessReturnType returnType;
3939
private final ScriptRunner taskRunner;
40+
private final Optional<WorkflowValueResolver<String>> stdinSupplier;
4041

4142
public RunScriptExecutor(
4243
Optional<WorkflowValueResolver<Map<String, Object>>> environmentExpr,
4344
Optional<WorkflowValueResolver<Map<String, Object>>> argumentExpr,
4445
WorkflowValueResolver<String> codeSupplier,
4546
boolean isAwait,
4647
ProcessReturnType returnType,
47-
ScriptRunner taskRunner) {
48+
ScriptRunner taskRunner,
49+
Optional<WorkflowValueResolver<String>> stdinSupplier) {
4850
this.environmentExpr = environmentExpr;
4951
this.argumentExpr = argumentExpr;
5052
this.codeSupplier = codeSupplier;
5153
this.isAwait = isAwait;
5254
this.returnType = returnType;
5355
this.taskRunner = taskRunner;
56+
this.stdinSupplier = stdinSupplier;
5457
}
5558

5659
@Override
@@ -80,7 +83,8 @@ private WorkflowModel runScript(
8083
.map(m -> m.apply(workflowContext, taskContext, input))
8184
.orElse(Map.of()),
8285
codeSupplier.apply(workflowContext, taskContext, input),
83-
returnType);
86+
returnType,
87+
stdinSupplier.map(m -> m.apply(workflowContext, taskContext, input)).orElse(null));
8488
return taskRunner.runScript(scriptContext, workflowContext, taskContext, input);
8589
} catch (Exception ex) {
8690
throw new WorkflowException(WorkflowError.runtime(taskContext, ex).build());

‎impl/core/src/main/java/org/openworkflow/sdk/impl/executors/RunScriptExecutorBuilder.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ public CallableTask build(RunScript taskConfiguration, WorkflowDefinition defini
7070
.orElseThrow(
7171
() ->
7272
new IllegalStateException(
73-
"No script runner implementation found for language " + language)));
73+
"No script runner implementation found for language " + language)),
74+
script.getStdin() != null
75+
? Optional.of(WorkflowUtils.buildStringFilter(application, script.getStdin()))
76+
: Optional.empty());
7477
}
7578

7679
@Override

‎impl/core/src/main/java/org/openworkflow/sdk/impl/scripts/ScriptContext.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ public record ScriptContext(
2222
Map<String, Object> args,
2323
Map<String, Object> envs,
2424
String code,
25-
RunTaskConfiguration.ProcessReturnType returnType) {}
25+
RunTaskConfiguration.ProcessReturnType returnType,
26+
String stdin) {}

‎impl/javascript/src/main/java/org/openworkflow/sdk/impl/executors/script/js/JavaScriptScriptTaskRunner.java‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
*/
1616
package org.openworkflow.sdk.impl.executors.script.js;
1717

18+
import java.io.ByteArrayInputStream;
1819
import java.io.ByteArrayOutputStream;
20+
import java.nio.charset.StandardCharsets;
1921
import java.util.Map;
2022
import org.graalvm.polyglot.Context;
23+
import org.graalvm.polyglot.Context.Builder;
2124
import org.graalvm.polyglot.Source;
2225
import org.graalvm.polyglot.Value;
2326
import org.openworkflow.sdk.impl.TaskContext;
@@ -66,14 +69,17 @@ public WorkflowModel runScript(
6669
WorkflowModel model) {
6770
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
6871
ByteArrayOutputStream stderr = new ByteArrayOutputStream();
69-
try (Context ctx =
72+
Builder builder =
7073
Context.newBuilder()
7174
.err(stderr)
7275
.out(stdout)
7376
.useSystemExit(true)
7477
.allowCreateProcess(false)
75-
.option("engine.WarnInterpreterOnly", "false")
76-
.build()) {
78+
.option("engine.WarnInterpreterOnly", "false");
79+
if (scriptContext.stdin() != null) {
80+
builder.in(new ByteArrayInputStream(scriptContext.stdin().getBytes(StandardCharsets.UTF_8)));
81+
}
82+
try (Context ctx = builder.build()) {
7783
scriptContext.args().forEach(ctx.getBindings(identifier().getLang())::putMember);
7884
configureProcessEnv(ctx, scriptContext.envs());
7985
ctx.eval(Source.create(identifier().getLang(), scriptContext.code()));

‎impl/python/src/main/java/org/openworkflow/sdk/impl/executors/script/python/PythonScriptTaskRunner.java‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@
1515
*/
1616
package org.openworkflow.sdk.impl.executors.script.python;
1717

18+
import java.io.IOException;
19+
import java.io.OutputStream;
20+
import java.nio.charset.StandardCharsets;
1821
import java.util.List;
1922
import org.openworkflow.sdk.impl.TaskContext;
2023
import org.openworkflow.sdk.impl.WorkflowContext;
24+
import org.openworkflow.sdk.impl.WorkflowError;
25+
import org.openworkflow.sdk.impl.WorkflowException;
2126
import org.openworkflow.sdk.impl.WorkflowModel;
2227
import org.openworkflow.sdk.impl.scripts.ScriptContext;
2328
import org.openworkflow.sdk.impl.scripts.ScriptLanguageId;
@@ -45,11 +50,16 @@ public WorkflowModel runScript(
4550
ScriptUtils.addEnviromment(builder, scriptContext.envs());
4651
scriptContext.args().forEach((k, v) -> addArg(builder.command(), k, v));
4752
logger.debug("Invoking python with command line {}", builder.command());
53+
Process process = ScriptUtils.uncheckedStart(builder);
54+
if (scriptContext.stdin() != null) {
55+
try (OutputStream out = process.getOutputStream()) {
56+
out.write(scriptContext.stdin().getBytes(StandardCharsets.UTF_8));
57+
} catch (IOException e) {
58+
throw new WorkflowException(WorkflowError.runtime(taskContext, e).build());
59+
}
60+
}
4861
return ScriptUtils.buildResultFromProcess(
49-
workflowContext.definition(),
50-
ScriptUtils.uncheckedStart(builder),
51-
scriptContext.returnType(),
52-
model);
62+
workflowContext.definition(), process, scriptContext.returnType(), model);
5363
}
5464

5565
private void addArg(List<String> list, String name, Object value) {

0 commit comments

Comments
 (0)