Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ default <T> Collection<T> multiConfig(String propName, Class<T> propClass) {
}

Iterable<String> names();

default boolean scriptOutput2SystemOutput() {
return config("org.openworkflow.sdk.impl.scripts.dumpOutput", Boolean.class).orElse(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,23 @@ public class RunScriptExecutor implements CallableTask {
private final boolean isAwait;
private final RunTaskConfiguration.ProcessReturnType returnType;
private final ScriptRunner taskRunner;
private final Optional<WorkflowValueResolver<String>> stdinSupplier;

public RunScriptExecutor(
Optional<WorkflowValueResolver<Map<String, Object>>> environmentExpr,
Optional<WorkflowValueResolver<Map<String, Object>>> argumentExpr,
WorkflowValueResolver<CompletableFuture<String>> codeSupplier,
boolean isAwait,
ProcessReturnType returnType,
ScriptRunner taskRunner) {
ScriptRunner taskRunner,
Optional<WorkflowValueResolver<String>> stdinSupplier) {
this.environmentExpr = environmentExpr;
this.argumentExpr = argumentExpr;
this.codeSupplier = codeSupplier;
this.isAwait = isAwait;
this.returnType = returnType;
this.taskRunner = taskRunner;
this.stdinSupplier = stdinSupplier;
}

@Override
Expand All @@ -76,7 +79,10 @@ private CompletableFuture<WorkflowModel> runScript(
.map(m -> m.apply(workflowContext, taskContext, input))
.orElse(Map.of()),
src,
returnType),
returnType,
Comment thread
fjtirado marked this conversation as resolved.
Resolved
stdinSupplier
.map(m -> m.apply(workflowContext, taskContext, input))
.orElse(null)),
workflowContext,
taskContext,
input);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public CallableTask build(RunScript taskConfiguration, WorkflowDefinition defini
t,
m);
}

return new RunScriptExecutor(
script.getEnvironment() != null && script.getEnvironment().getAdditionalProperties() != null
? Optional.of(
Expand All @@ -79,7 +78,10 @@ public CallableTask build(RunScript taskConfiguration, WorkflowDefinition defini
.findFirst()
.orElseThrow(() ->
new IllegalStateException(
"No script runner implementation found for language " + language)));
"No script runner implementation found for language " + language)),
script.getStdin() != null
? Optional.of(WorkflowUtils.buildStringFilter(application, script.getStdin()))
: Optional.empty());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package org.openworkflow.sdk.impl.executors;

import static org.openworkflow.sdk.impl.scripts.ScriptUtils.uncheckedStart;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -36,26 +34,21 @@ public class RunShellExecutor implements CallableTask {
private final Optional<WorkflowValueResolver<String>> shellDirectory;
private final Optional<WorkflowValueResolver<Map<String, Object>>> shellEnv;
private final Optional<ProcessReturnType> returnType;
private final Optional<WorkflowValueResolver<String>> stdinSupplier;

public RunShellExecutor(
WorkflowValueResolver<String> shellCommand,
List<WorkflowValueResolver<String>> shellArguments,
Optional<WorkflowValueResolver<Map<String, Object>>> shellEnv,
Optional<ProcessReturnType> returnType,
Optional<WorkflowValueResolver<String>> shellDirectory) {
Optional<WorkflowValueResolver<String>> shellDirectory,
Optional<WorkflowValueResolver<String>> stdinSupplier) {
this.shellCommand = shellCommand;
this.shellArguments = shellArguments;
this.shellEnv = shellEnv;
this.returnType = returnType;
this.shellDirectory = shellDirectory;
}

public RunShellExecutor(
WorkflowValueResolver<String> shellCommand,
List<WorkflowValueResolver<String>> shellArguments,
Optional<WorkflowValueResolver<Map<String, Object>>> shellEnv,
Optional<ProcessReturnType> returnType) {
this(shellCommand, shellArguments, shellEnv, returnType, Optional.empty());
this.stdinSupplier = stdinSupplier;
}

@Override
Expand All @@ -81,20 +74,19 @@ public CompletableFuture<WorkflowModel> apply(
shellEnv.ifPresent(
map -> ScriptUtils.addEnviromment(builder, map.apply(workflowContext, taskContext, model)));

return returnType
.map(type ->
CompletableFuture.supplyAsync(
() ->
ScriptUtils.buildResultFromProcess(
workflowContext.definition(), uncheckedStart(builder), type, model),
workflowContext.definition().application().executorService()))
.orElseGet(() -> {
workflowContext
.definition()
.application()
.executorService()
.submit(() -> uncheckedStart(builder));
return CompletableFuture.completedFuture(model);
});
String stdin =
stdinSupplier.map(m -> m.apply(workflowContext, taskContext, model)).orElse(null);
CompletableFuture<WorkflowModel> future =
CompletableFuture.supplyAsync(
() ->
ScriptUtils.buildResultFromProcess(
Comment thread
fjtirado marked this conversation as resolved.
Resolved
builder,
returnType.orElse(ProcessReturnType.NONE),
Comment thread
fjtirado marked this conversation as resolved.
Resolved
stdin,
workflowContext,
taskContext,
model),
workflowContext.definition().application().executorService());
return returnType.isPresent() ? future : CompletableFuture.completedFuture(model);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public CallableTask build(RunShell taskConfiguration, WorkflowDefinition definit
shell.getDirectory() != null
? Optional.of(
WorkflowUtils.buildStringFilter(definition.application(), shell.getDirectory()))
: Optional.empty(),
shell.getStdin() != null
? Optional.of(
WorkflowUtils.buildStringFilter(definition.application(), shell.getStdin()))
: Optional.empty());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ public record ScriptContext(
Map<String, Object> args,
Map<String, Object> envs,
String code,
RunTaskConfiguration.ProcessReturnType returnType) {}
RunTaskConfiguration.ProcessReturnType returnType,
String stdin) {}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@
package org.openworkflow.sdk.impl.scripts;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.OutputStream;
import java.lang.ProcessBuilder.Redirect;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import org.openworkflow.sdk.api.types.RunTaskConfiguration;
import org.openworkflow.sdk.api.types.RunTaskConfiguration.ProcessReturnType;
import org.openworkflow.sdk.impl.WorkflowDefinition;
import org.openworkflow.sdk.impl.TaskContext;
import org.openworkflow.sdk.impl.WorkflowApplication;
import org.openworkflow.sdk.impl.WorkflowContext;
import org.openworkflow.sdk.impl.WorkflowError;
import org.openworkflow.sdk.impl.WorkflowException;
import org.openworkflow.sdk.impl.WorkflowModel;
import org.openworkflow.sdk.impl.WorkflowModelFactory;
import org.openworkflow.sdk.impl.executors.ProcessResult;
Expand All @@ -44,6 +50,7 @@ public static WorkflowModel modelFromOutput(
}
}
return switch (returnType) {
case null -> model;
case ALL ->
modelFactory.fromAny(
new ProcessResult(
Expand All @@ -55,38 +62,75 @@ public static WorkflowModel modelFromOutput(
};
}

public static Process uncheckedStart(ProcessBuilder builder) {
try {
return builder.start();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

public static void addEnviromment(ProcessBuilder builder, Map<String, Object> env) {
for (Map.Entry<String, Object> entry : env.entrySet()) {
builder.environment().put(entry.getKey(), (String) entry.getValue());
}
}

private static boolean keepOut(ProcessReturnType type, boolean dumpOutput) {
return dumpOutput || type == ProcessReturnType.ALL || type == ProcessReturnType.STDOUT;
}

private static boolean keepError(ProcessReturnType type) {
return logger.isDebugEnabled()
|| type == ProcessReturnType.ALL
|| type == ProcessReturnType.STDERR;
}

public static WorkflowModel buildResultFromProcess(
WorkflowDefinition definition, Process process, ProcessReturnType type, WorkflowModel model) {
try {
ProcessBuilder builder,
ProcessReturnType type,
String stdin,
WorkflowContext workflowContext,
TaskContext taskContext,
WorkflowModel model) {
WorkflowApplication app = workflowContext.definition().application();
boolean dumpOutput = app.configManager().scriptOutput2SystemOutput();
boolean keepOut = keepOut(type, dumpOutput);
boolean keepError = keepError(type);
if (!keepOut) {
builder.redirectOutput(Redirect.DISCARD);
}
if (!keepError) {
builder.redirectError(Redirect.DISCARD);
}
Process process = uncheckedStart(builder, taskContext);
Comment thread
fjtirado marked this conversation as resolved.
Resolved
try (StreamSuppliers suppliers =
StreamSuppliers.from(process, app.executorService(), keepOut, keepError)) {
addStdin(process, stdin, taskContext);
int code = process.waitFor();
Comment thread
fjtirado marked this conversation as resolved.
Resolved
Comment thread
fjtirado marked this conversation as resolved.
Resolved
StreamSuppliers suppliers = StreamSuppliers.from(process);
if (definition
.application()
.configManager()
.config("org.openworkflow.sdk.impl.scripts.dumpOutput", Boolean.class)
.orElse(true)) {
if (dumpOutput) {
Comment thread
fjtirado marked this conversation as resolved.
Resolved
System.out.println(suppliers.outputStream().get());
}
return modelFromOutput(code, suppliers, type, definition.application().modelFactory(), model);
return modelFromOutput(code, suppliers, type, app.modelFactory(), model);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return model;
}
}

private static Process uncheckedStart(ProcessBuilder builder, TaskContext taskContext) {
try {
return builder.start();
} catch (IOException e) {
throw new WorkflowException(WorkflowError.runtime(taskContext, e).build());
}
}

private static void addStdin(Process process, String stdin, TaskContext taskContext) {
if (stdin != null) {
try (OutputStream out = process.getOutputStream()) {
out.write(stdin.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
if (process.toHandle().isAlive()) {
logger.warn(
"Unexpected IO exception while writing to process input while the process is still alive, does the process close the standard input before ending?",
e);
}
}
}
}

private ScriptUtils() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2020-Present The Open Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openworkflow.sdk.impl.scripts;

import java.util.function.Supplier;

public interface StreamSupplier extends Supplier<String>, AutoCloseable {

default void close() {}
}
Loading
Loading