Skip to content

Commit 0ae53e7

Browse files
authored
[Fix #1774] Update schema to latest specification version (#1817)
* [Fix #1774] Update schema to latest specification version Signed-off-by: Francisco Javier Tirado Sarti <ftirados@ibm.com> * [Fix #1774] Adding stdin support in scripts Signed-off-by: Francisco Javier Tirado Sarti <ftirados@ibm.com> * [Fix #1774] Discaring output and error recollection for await false Signed-off-by: Francisco Javier Tirado Sarti <ftirados@ibm.com> --------- Signed-off-by: Francisco Javier Tirado Sarti <ftirados@ibm.com>
1 parent d37e8b5 commit 0ae53e7

14 files changed

Lines changed: 249 additions & 80 deletions

File tree

‎impl/core/src/main/java/org/openworkflow/sdk/impl/config/ConfigManager.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,8 @@ default <T> Collection<T> multiConfig(String propName, Class<T> propClass) {
2929
}
3030

3131
Iterable<String> names();
32+
33+
default boolean scriptOutput2SystemOutput() {
34+
return config("org.openworkflow.sdk.impl.scripts.dumpOutput", Boolean.class).orElse(true);
35+
}
3236
}

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

Lines changed: 8 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<CompletableFuture<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
@@ -76,7 +79,10 @@ private CompletableFuture<WorkflowModel> runScript(
7679
.map(m -> m.apply(workflowContext, taskContext, input))
7780
.orElse(Map.of()),
7881
src,
79-
returnType),
82+
isAwait ? returnType : null,
83+
stdinSupplier
84+
.map(m -> m.apply(workflowContext, taskContext, input))
85+
.orElse(null)),
8086
workflowContext,
8187
taskContext,
8288
input);

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ public CallableTask build(RunScript taskConfiguration, WorkflowDefinition defini
5959
t,
6060
m);
6161
}
62-
6362
return new RunScriptExecutor(
6463
script.getEnvironment() != null && script.getEnvironment().getAdditionalProperties() != null
6564
? Optional.of(
@@ -79,7 +78,10 @@ public CallableTask build(RunScript taskConfiguration, WorkflowDefinition defini
7978
.findFirst()
8079
.orElseThrow(() ->
8180
new IllegalStateException(
82-
"No script runner implementation found for language " + language)));
81+
"No script runner implementation found for language " + language)),
82+
script.getStdin() != null
83+
? Optional.of(WorkflowUtils.buildStringFilter(application, script.getStdin()))
84+
: Optional.empty());
8385
}
8486

8587
@Override

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

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package org.openworkflow.sdk.impl.executors;
1717

18-
import static org.openworkflow.sdk.impl.scripts.ScriptUtils.uncheckedStart;
19-
2018
import java.io.File;
2119
import java.util.ArrayList;
2220
import java.util.List;
@@ -36,26 +34,21 @@ public class RunShellExecutor implements CallableTask {
3634
private final Optional<WorkflowValueResolver<String>> shellDirectory;
3735
private final Optional<WorkflowValueResolver<Map<String, Object>>> shellEnv;
3836
private final Optional<ProcessReturnType> returnType;
37+
private final Optional<WorkflowValueResolver<String>> stdinSupplier;
3938

4039
public RunShellExecutor(
4140
WorkflowValueResolver<String> shellCommand,
4241
List<WorkflowValueResolver<String>> shellArguments,
4342
Optional<WorkflowValueResolver<Map<String, Object>>> shellEnv,
4443
Optional<ProcessReturnType> returnType,
45-
Optional<WorkflowValueResolver<String>> shellDirectory) {
44+
Optional<WorkflowValueResolver<String>> shellDirectory,
45+
Optional<WorkflowValueResolver<String>> stdinSupplier) {
4646
this.shellCommand = shellCommand;
4747
this.shellArguments = shellArguments;
4848
this.shellEnv = shellEnv;
4949
this.returnType = returnType;
5050
this.shellDirectory = shellDirectory;
51-
}
52-
53-
public RunShellExecutor(
54-
WorkflowValueResolver<String> shellCommand,
55-
List<WorkflowValueResolver<String>> shellArguments,
56-
Optional<WorkflowValueResolver<Map<String, Object>>> shellEnv,
57-
Optional<ProcessReturnType> returnType) {
58-
this(shellCommand, shellArguments, shellEnv, returnType, Optional.empty());
51+
this.stdinSupplier = stdinSupplier;
5952
}
6053

6154
@Override
@@ -81,20 +74,14 @@ public CompletableFuture<WorkflowModel> apply(
8174
shellEnv.ifPresent(
8275
map -> ScriptUtils.addEnviromment(builder, map.apply(workflowContext, taskContext, model)));
8376

84-
return returnType
85-
.map(type ->
86-
CompletableFuture.supplyAsync(
87-
() ->
88-
ScriptUtils.buildResultFromProcess(
89-
workflowContext.definition(), uncheckedStart(builder), type, model),
90-
workflowContext.definition().application().executorService()))
91-
.orElseGet(() -> {
92-
workflowContext
93-
.definition()
94-
.application()
95-
.executorService()
96-
.submit(() -> uncheckedStart(builder));
97-
return CompletableFuture.completedFuture(model);
98-
});
77+
String stdin =
78+
stdinSupplier.map(m -> m.apply(workflowContext, taskContext, model)).orElse(null);
79+
CompletableFuture<WorkflowModel> future =
80+
CompletableFuture.supplyAsync(
81+
() ->
82+
ScriptUtils.buildResultFromProcess(
83+
builder, returnType.orElse(null), stdin, workflowContext, taskContext, model),
84+
workflowContext.definition().application().executorService());
85+
return returnType.isPresent() ? future : CompletableFuture.completedFuture(model);
9986
}
10087
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ public CallableTask build(RunShell taskConfiguration, WorkflowDefinition definit
4848
shell.getDirectory() != null
4949
? Optional.of(
5050
WorkflowUtils.buildStringFilter(definition.application(), shell.getDirectory()))
51+
: Optional.empty(),
52+
shell.getStdin() != null
53+
? Optional.of(
54+
WorkflowUtils.buildStringFilter(definition.application(), shell.getStdin()))
5155
: Optional.empty());
5256
}
5357

‎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/core/src/main/java/org/openworkflow/sdk/impl/scripts/ScriptUtils.java‎

Lines changed: 76 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,20 @@
1616
package org.openworkflow.sdk.impl.scripts;
1717

1818
import java.io.IOException;
19-
import java.io.UncheckedIOException;
19+
import java.io.OutputStream;
20+
import java.lang.ProcessBuilder.Redirect;
21+
import java.nio.charset.StandardCharsets;
2022
import java.util.Map;
2123
import org.openworkflow.sdk.api.types.RunTaskConfiguration;
2224
import org.openworkflow.sdk.api.types.RunTaskConfiguration.ProcessReturnType;
23-
import org.openworkflow.sdk.impl.WorkflowDefinition;
25+
import org.openworkflow.sdk.impl.TaskContext;
26+
import org.openworkflow.sdk.impl.WorkflowApplication;
27+
import org.openworkflow.sdk.impl.WorkflowContext;
28+
import org.openworkflow.sdk.impl.WorkflowError;
29+
import org.openworkflow.sdk.impl.WorkflowException;
2430
import org.openworkflow.sdk.impl.WorkflowModel;
2531
import org.openworkflow.sdk.impl.WorkflowModelFactory;
32+
import org.openworkflow.sdk.impl.WorkflowUtils;
2633
import org.openworkflow.sdk.impl.executors.ProcessResult;
2734
import org.slf4j.Logger;
2835
import org.slf4j.LoggerFactory;
@@ -40,10 +47,14 @@ public static WorkflowModel modelFromOutput(
4047
if (code != 0) {
4148
logger.warn("Process call failed with code {}", code);
4249
if (logger.isDebugEnabled()) {
43-
logger.debug(streamSuppliers.errorStream().get());
50+
String str = streamSuppliers.errorStream().get();
51+
if (WorkflowUtils.isValid(str)) {
52+
logger.debug(str);
53+
}
4454
}
4555
}
4656
return switch (returnType) {
57+
case null -> model;
4758
case ALL ->
4859
modelFactory.fromAny(
4960
new ProcessResult(
@@ -55,38 +66,81 @@ public static WorkflowModel modelFromOutput(
5566
};
5667
}
5768

58-
public static Process uncheckedStart(ProcessBuilder builder) {
59-
try {
60-
return builder.start();
61-
} catch (IOException e) {
62-
throw new UncheckedIOException(e);
63-
}
64-
}
65-
6669
public static void addEnviromment(ProcessBuilder builder, Map<String, Object> env) {
6770
for (Map.Entry<String, Object> entry : env.entrySet()) {
6871
builder.environment().put(entry.getKey(), (String) entry.getValue());
6972
}
7073
}
7174

75+
private static boolean keepOut(ProcessReturnType type, boolean dumpOutput) {
76+
return type != null
77+
&& (dumpOutput || type == ProcessReturnType.ALL || type == ProcessReturnType.STDOUT);
78+
}
79+
80+
private static boolean keepError(ProcessReturnType type) {
81+
return type != null
82+
&& (logger.isDebugEnabled()
83+
|| type == ProcessReturnType.ALL
84+
|| type == ProcessReturnType.STDERR);
85+
}
86+
7287
public static WorkflowModel buildResultFromProcess(
73-
WorkflowDefinition definition, Process process, ProcessReturnType type, WorkflowModel model) {
74-
try {
75-
int code = process.waitFor();
76-
StreamSuppliers suppliers = StreamSuppliers.from(process);
77-
if (definition
78-
.application()
79-
.configManager()
80-
.config("org.openworkflow.sdk.impl.scripts.dumpOutput", Boolean.class)
81-
.orElse(true)) {
82-
System.out.println(suppliers.outputStream().get());
88+
ProcessBuilder builder,
89+
ProcessReturnType type,
90+
String stdin,
91+
WorkflowContext workflowContext,
92+
TaskContext taskContext,
93+
WorkflowModel model) {
94+
WorkflowApplication app = workflowContext.definition().application();
95+
boolean dumpOutput = app.configManager().scriptOutput2SystemOutput();
96+
boolean keepOut = keepOut(type, dumpOutput);
97+
boolean keepError = keepError(type);
98+
if (!keepOut) {
99+
builder.redirectOutput(Redirect.DISCARD);
100+
}
101+
if (!keepError) {
102+
builder.redirectError(Redirect.DISCARD);
103+
}
104+
Process process = uncheckedStart(builder, taskContext);
105+
try (StreamSuppliers suppliers =
106+
StreamSuppliers.from(process, app.executorService(), keepOut, keepError)) {
107+
addStdin(process, stdin, taskContext);
108+
// do not wait if await was false (type is null)
109+
int code = type == null ? 0 : process.waitFor();
110+
if (dumpOutput) {
111+
String str = suppliers.outputStream().get();
112+
if (WorkflowUtils.isValid(str)) {
113+
System.out.println(str);
114+
}
83115
}
84-
return modelFromOutput(code, suppliers, type, definition.application().modelFactory(), model);
116+
return modelFromOutput(code, suppliers, type, app.modelFactory(), model);
85117
} catch (InterruptedException e) {
86118
Thread.currentThread().interrupt();
87119
return model;
88120
}
89121
}
90122

123+
private static Process uncheckedStart(ProcessBuilder builder, TaskContext taskContext) {
124+
try {
125+
return builder.start();
126+
} catch (IOException e) {
127+
throw new WorkflowException(WorkflowError.runtime(taskContext, e).build());
128+
}
129+
}
130+
131+
private static void addStdin(Process process, String stdin, TaskContext taskContext) {
132+
if (stdin != null) {
133+
try (OutputStream out = process.getOutputStream()) {
134+
out.write(stdin.getBytes(StandardCharsets.UTF_8));
135+
} catch (IOException e) {
136+
if (process.toHandle().isAlive()) {
137+
logger.warn(
138+
"Unexpected IO exception while writing to process input while the process is still alive, does the process close the standard input before ending?",
139+
e);
140+
}
141+
}
142+
}
143+
}
144+
91145
private ScriptUtils() {}
92146
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2020-Present The Open Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openworkflow.sdk.impl.scripts;
17+
18+
import java.util.function.Supplier;
19+
20+
public interface StreamSupplier extends Supplier<String>, AutoCloseable {
21+
22+
default void close() {}
23+
}

0 commit comments

Comments
 (0)