Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
@@ -0,0 +1,127 @@
/*
* Copyright 2020-Present The Serverless 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 io.serverlessworkflow.impl.executors;

import io.serverlessworkflow.api.types.RunScript;
import io.serverlessworkflow.api.types.RunTaskConfiguration;
import io.serverlessworkflow.api.types.Script;
import io.serverlessworkflow.api.types.ScriptUnion;
import io.serverlessworkflow.impl.TaskContext;
import io.serverlessworkflow.impl.WorkflowApplication;
import io.serverlessworkflow.impl.WorkflowContext;
import io.serverlessworkflow.impl.WorkflowDefinition;
import io.serverlessworkflow.impl.WorkflowModel;
import io.serverlessworkflow.impl.WorkflowUtils;
import io.serverlessworkflow.impl.WorkflowValueResolver;
import io.serverlessworkflow.impl.resources.ResourceLoaderUtils;
import io.serverlessworkflow.impl.scripts.ScriptContext;
import io.serverlessworkflow.impl.scripts.ScriptLanguageId;
import io.serverlessworkflow.impl.scripts.ScriptRunner;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.concurrent.CompletableFuture;

public class RunScriptExecutor implements RunnableTask<RunScript> {

private Optional<WorkflowValueResolver<Map<String, Object>>> environmentExpr;

private Optional<WorkflowValueResolver<Map<String, Object>>> argumentExpr;

private WorkflowValueResolver<String> codeSupplier;
private boolean isAwait;
private RunTaskConfiguration.ProcessReturnType returnType;
private ScriptRunner taskRunner;

@Override
public void init(RunScript taskConfiguration, WorkflowDefinition definition) {
ScriptUnion scriptUnion = taskConfiguration.getScript();
Script script = scriptUnion.get();
ScriptLanguageId language = ScriptLanguageId.from(script.getLanguage());

this.taskRunner =
ServiceLoader.load(ScriptRunner.class).stream()
.map(ServiceLoader.Provider::get)
.filter(s -> s.identifier().equals(language))
.findFirst()
.orElseThrow(
() ->
new IllegalStateException(
"No script runner implementation found for language " + language));

this.isAwait = taskConfiguration.isAwait();

this.returnType = taskConfiguration.getReturn();

WorkflowApplication application = definition.application();
this.environmentExpr =
script.getEnvironment() != null && script.getEnvironment().getAdditionalProperties() != null
? Optional.of(
WorkflowUtils.buildMapResolver(
application, null, script.getEnvironment().getAdditionalProperties()))
: Optional.empty();

this.argumentExpr =
script.getArguments() != null && script.getArguments().getAdditionalProperties() != null
? Optional.of(
WorkflowUtils.buildMapResolver(
application, null, script.getArguments().getAdditionalProperties()))
: Optional.empty();

this.codeSupplier =
scriptUnion.getInlineScript() != null
? WorkflowUtils.buildStringFilter(application, scriptUnion.getInlineScript().getCode())
: (w, t, m) ->
definition
.resourceLoader()
.load(
Objects.requireNonNull(
scriptUnion.getExternalScript(),
"External script is required if inline script was not set")
.getSource(),
ResourceLoaderUtils::readString,
w,
t,
m);
}

@Override
public CompletableFuture<WorkflowModel> apply(
WorkflowContext workflowContext, TaskContext taskContext, WorkflowModel input) {
return CompletableFuture.supplyAsync(
() ->
taskRunner.runScript(
new ScriptContext(
argumentExpr
.map(m -> m.apply(workflowContext, taskContext, input))
.orElse(Map.of()),
environmentExpr
.map(m -> m.apply(workflowContext, taskContext, input))
.orElse(Map.of()),
codeSupplier.apply(workflowContext, taskContext, input),
isAwait,
returnType),
workflowContext,
taskContext,
input));
}

@Override
public boolean accept(Class<? extends RunTaskConfiguration> clazz) {
return RunScript.class.equals(clazz);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2020-Present The Serverless 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 io.serverlessworkflow.impl.scripts;

import io.serverlessworkflow.api.types.RunTaskConfiguration;
import java.util.Map;

public record ScriptContext(
Map<String, Object> args,
Map<String, Object> envs,
String code,
boolean isAwait,
RunTaskConfiguration.ProcessReturnType returnType) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2020-Present The Serverless 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 io.serverlessworkflow.impl.scripts;

import java.util.Arrays;

public enum ScriptLanguageId {
JS("js"),
PYTHON("python");

private final String lang;

ScriptLanguageId(String lang) {
this.lang = lang;
}

public String getLang() {
return lang;
}

public static ScriptLanguageId from(String lang) {
for (ScriptLanguageId l : ScriptLanguageId.values()) {
if (l.getLang().equalsIgnoreCase(lang)) {
return l;
}
}
throw new IllegalStateException(
"Unsupported script language: "
+ lang
+ ". Supported languages are: "
+ Arrays.toString(
Arrays.stream(ScriptLanguageId.values()).map(ScriptLanguageId::getLang).toArray()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2020-Present The Serverless 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 io.serverlessworkflow.impl.scripts;

import io.serverlessworkflow.impl.ServicePriority;
import io.serverlessworkflow.impl.TaskContext;
import io.serverlessworkflow.impl.WorkflowContext;
import io.serverlessworkflow.impl.WorkflowModel;
import io.serverlessworkflow.impl.executors.RunScriptExecutor;

/** Represents a script task that executes a script in a specific scripting language. */
public interface ScriptRunner extends ServicePriority {

/**
* The scripting language supported by this script task runner.
*
* @return the scripting language as {@link RunScriptExecutor.LanguageId} enum.
*/
ScriptLanguageId identifier();

WorkflowModel runScript(
ScriptContext script,
WorkflowContext workflowContext,
TaskContext taskContext,
WorkflowModel input);
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
io.serverlessworkflow.impl.executors.RunWorkflowExecutor
io.serverlessworkflow.impl.executors.RunShellExecutor
io.serverlessworkflow.impl.executors.RunShellExecutor
io.serverlessworkflow.impl.executors.RunScriptExecutor
24 changes: 21 additions & 3 deletions impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<version.org.glassfish.jersey>3.1.11</version.org.glassfish.jersey>
<version.com.cronutils>9.2.1</version.com.cronutils>
<version.docker.java>3.6.0</version.docker.java>
<version.org.graalvm.polyglot>23.1.1</version.org.graalvm.polyglot>
</properties>
<dependencyManagement>
<dependencies>
Expand Down Expand Up @@ -130,12 +131,17 @@
<groupId>com.h2database</groupId>
<artifactId>h2-mvstore</artifactId>
<version>${version.com.h2database}</version>
</dependency>
<dependency>
</dependency>
<dependency>
<groupId>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-impl-script-js</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.cronutils</groupId>
<artifactId>cron-utils</artifactId>
<version>${version.com.cronutils}</version>
</dependency>
</dependency>
<dependency>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java-core</artifactId>
Expand All @@ -146,6 +152,17 @@
<artifactId>docker-java-transport-httpclient5</artifactId>
<version>${version.docker.java}</version>
</dependency>
<dependency>
<groupId>org.graalvm.polyglot</groupId>
<artifactId>js</artifactId>
<version>${version.org.graalvm.polyglot}</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.graalvm.polyglot</groupId>
<artifactId>polyglot</artifactId>
<version>${version.org.graalvm.polyglot}</version>
</dependency>
</dependencies>
</dependencyManagement>
<modules>
Expand All @@ -163,5 +180,6 @@
<module>validation</module>
<module>container</module>
<module>test</module>
<module>script-js</module>
</modules>
</project>
25 changes: 25 additions & 0 deletions impl/script-js/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-impl</artifactId>
<version>8.0.0-SNAPSHOT</version>
</parent>
<artifactId>serverlessworkflow-impl-script-js</artifactId>
<name>Serverless Workflow :: Impl :: Script JavaScript</name>
<dependencies>
<dependency>
<groupId>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-impl-core</artifactId>
</dependency>
<dependency>
<groupId>org.graalvm.polyglot</groupId>
<artifactId>polyglot</artifactId>
</dependency>
<dependency>
<groupId>org.graalvm.polyglot</groupId>
<artifactId>js</artifactId>
<type>pom</type>
</dependency>
</dependencies>
</project>
Loading