Skip to content

Commit 1675d67

Browse files
committed
Support await
Signed-off-by: Matheus Cruz <[email protected]>
1 parent 01102dd commit 1675d67

File tree

3 files changed

+61
-13
lines changed

3 files changed

+61
-13
lines changed

impl/core/src/main/java/io/serverlessworkflow/impl/executors/RunShellExecutor.java

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import io.serverlessworkflow.impl.expressions.ExpressionUtils;
2828
import java.io.IOException;
2929
import java.io.OutputStream;
30+
import java.nio.file.Path;
3031
import java.util.Map;
3132
import java.util.concurrent.CompletableFuture;
3233

@@ -37,7 +38,8 @@ public class RunShellExecutor implements RunnableTask<RunShell> {
3738

3839
@FunctionalInterface
3940
private interface ProcessResultSupplier {
40-
ProcessResult apply(TaskContext taskContext, ProcessBuilder processBuilder);
41+
WorkflowModel apply(
42+
TaskContext taskContext, ProcessBuilder processBuilder, WorkflowModel input);
4143
}
4244

4345
private interface CommandSupplier {
@@ -48,9 +50,9 @@ private interface CommandSupplier {
4850
public CompletableFuture<WorkflowModel> apply(
4951
WorkflowContext workflowContext, TaskContext taskContext, WorkflowModel input) {
5052
ProcessBuilder processBuilder = this.commandSupplier.apply(taskContext, workflowContext);
51-
ProcessResult processResult = this.processResultSupplier.apply(taskContext, processBuilder);
52-
return CompletableFuture.completedFuture(
53-
workflowContext.definition().application().modelFactory().fromAny(processResult));
53+
WorkflowModel workflowModel =
54+
this.processResultSupplier.apply(taskContext, processBuilder, input);
55+
return CompletableFuture.completedFuture(workflowModel);
5456
}
5557

5658
@Override
@@ -87,27 +89,37 @@ public void init(RunShell taskConfiguration, WorkflowDefinition definition) {
8789
}
8890
}
8991

92+
builder.directory(Path.of(System.getProperty("user.dir")).toFile());
93+
9094
return builder;
9195
};
9296

9397
this.processResultSupplier =
94-
(taskContext, processBuilder) -> {
98+
(taskContext, processBuilder, input) -> {
9599
if (shellCommand == null || shellCommand.isBlank()) {
96100
throw new IllegalStateException(
97101
"Missing shell command in RunShell task: " + taskContext.taskName());
98102
}
99103

100104
try {
101105
Process process = processBuilder.start();
102-
StringBuilder stdout = new StringBuilder();
103-
StringBuilder stderr = new StringBuilder();
104-
105-
process.getInputStream().transferTo(the(stdout));
106-
process.getErrorStream().transferTo(the(stderr));
107106

108-
int exitCode = process.waitFor();
109-
110-
return new ProcessResult(exitCode, stdout.toString().trim(), stderr.toString().trim());
107+
if (taskConfiguration.isAwait()) {
108+
StringBuilder stdout = new StringBuilder();
109+
StringBuilder stderr = new StringBuilder();
110+
111+
process.getInputStream().transferTo(the(stdout));
112+
process.getErrorStream().transferTo(the(stderr));
113+
int exitCode = process.waitFor();
114+
return definition
115+
.application()
116+
.modelFactory()
117+
.fromAny(
118+
new ProcessResult(
119+
exitCode, stdout.toString().trim(), stderr.toString().trim()));
120+
} else {
121+
return input;
122+
}
111123

112124
} catch (IOException | InterruptedException e) {
113125
throw new RuntimeException(e);

impl/test/src/test/java/io/serverlessworkflow/impl/test/shell/RunTaskExecutorTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import io.serverlessworkflow.impl.WorkflowModel;
2222
import io.serverlessworkflow.impl.executors.ProcessResult;
2323
import java.io.IOException;
24+
import java.nio.file.Files;
25+
import java.nio.file.Path;
2426
import java.util.Map;
2527
import org.assertj.core.api.SoftAssertions;
2628
import org.junit.jupiter.api.Test;
@@ -117,6 +119,27 @@ void testMissingShellCommand() throws IOException {
117119
}
118120
}
119121

122+
@Test
123+
void testAwaitBehavior() throws IOException {
124+
Workflow workflow =
125+
WorkflowReader.readWorkflowFromClasspath(
126+
"workflows-samples/run-shell/echo-not-awaiting.yaml");
127+
try (WorkflowApplication appl = WorkflowApplication.builder().build()) {
128+
Map<String, String> inputMap = Map.of("full_name", "Matheus Cruz");
129+
130+
WorkflowModel outputModel =
131+
appl.workflowDefinition(workflow).instance(inputMap).start().join();
132+
133+
String content = Files.readString(Path.of("/tmp/hello.txt"));
134+
135+
SoftAssertions.assertSoftly(
136+
softly -> {
137+
softly.assertThat(content).contains("hello world not awaiting (Matheus Cruz)");
138+
softly.assertThat(outputModel.asMap().get()).isEqualTo(inputMap);
139+
});
140+
}
141+
}
142+
120143
record Input(User user) {}
121144

122145
record User(String name) {}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
document:
2+
dsl: '1.0.1'
3+
namespace: test
4+
name: run-shell-example
5+
version: '0.1.0'
6+
do:
7+
- runShell:
8+
run:
9+
shell:
10+
command: echo "hello world not awaiting ($FULL_NAME)" > /tmp/hello.txt && cat /tmp/hello.txt
11+
environment:
12+
FULL_NAME: ${.full_name}
13+
await: false

0 commit comments

Comments
 (0)