Skip to content

Commit d202674

Browse files
committed
[WIP] second iteration with envs for js
Signed-off-by: Matheus Cruz <matheuscruz.dev@gmail.com>
1 parent e45fd3a commit d202674

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

impl/core/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
<artifactId>js</artifactId>
3030
<version>24.1.1</version>
3131
</dependency>
32+
<dependency>
33+
<groupId>org.graalvm.python</groupId>
34+
<artifactId>python</artifactId>
35+
<version>24.1.1</version>
36+
</dependency>
3237
<dependency>
3338
<groupId>org.graalvm.sdk</groupId>
3439
<artifactId>graal-sdk</artifactId>

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void init(RunScript taskConfiguration, WorkflowDefinition definition) {
4141
Script script = scriptUnion.get();
4242
String language = script.getLanguage();
4343
boolean isAwait = taskConfiguration.isAwait();
44-
RunTaskConfiguration.ProcessReturnType returnType = taskConfiguration.getReturn();
44+
4545
WorkflowApplication application = definition.application();
4646
if (language == null || !SUPPORTED_LANGUAGES.contains(language.toLowerCase())) {
4747
throw new IllegalArgumentException("Unsupported script language: " + language + ". Supported languages are: " + SUPPORTED_LANGUAGES);
@@ -99,7 +99,7 @@ public void init(RunScript taskConfiguration, WorkflowDefinition definition) {
9999
.err(stderr)
100100
.out(stdout)
101101
.environment(envs)
102-
.option("engine.WarnInterpreterOnly", "false")
102+
.option("engine.WarnInterpreterOnly", "false") // disabling it due to warnings in stderr
103103
.build()) {
104104

105105
ExecutorService executorService = application.executorService();
@@ -108,6 +108,17 @@ public void init(RunScript taskConfiguration, WorkflowDefinition definition) {
108108
jsCtx.getBindings(language.toLowerCase()).putMember(arg, val);
109109
});
110110

111+
// configure process.env for js
112+
if (language.equalsIgnoreCase("js")) {
113+
Value bindings = jsCtx.getBindings("js");
114+
Value process = jsCtx.eval("js", "({ env: {} })");
115+
116+
for (var entry : envs.entrySet()) {
117+
process.getMember("env").putMember(entry.getKey(), entry.getValue());
118+
}
119+
bindings.putMember("process", process);
120+
}
121+
111122
if (!isAwait) {
112123
executorService.submit(() -> {
113124
jsCtx.eval(scriptUnion.getInlineScript().getLanguage(), scriptUnion.getInlineScript().getCode());

impl/test/src/test/java/io/serverlessworkflow/impl/test/RunShellScriptTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,25 @@ void testConsoleLogWithArgs() throws IOException {
5252
});
5353
}
5454
}
55+
56+
@Test
57+
void testConsoleLogWithEnvs() throws IOException {
58+
Workflow workflow =
59+
WorkflowReader.readWorkflowFromClasspath(
60+
"workflows-samples/run-script/console-log-envs.yaml");
61+
try (WorkflowApplication appl = WorkflowApplication.builder().build()) {
62+
WorkflowModel model =
63+
appl.workflowDefinition(workflow)
64+
.instance(Map.of())
65+
.start()
66+
.join();
67+
68+
SoftAssertions.assertSoftly(
69+
softly -> {
70+
softly.assertThat(model.asText()).isPresent();
71+
softly.assertThat(model.asText().get()).isEqualTo("Running JavaScript code using Serverless Workflow!");
72+
});
73+
}
74+
}
75+
5576
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
document:
2+
dsl: '1.0.2'
3+
namespace: test
4+
name: run-script-example
5+
version: '0.1.0'
6+
do:
7+
- runScript:
8+
run:
9+
script:
10+
language: js
11+
code: >
12+
console.log(`Running ${process.env.LANGUAGE} code using Serverless Workflow!`);
13+
environment:
14+
LANGUAGE: JavaScript

0 commit comments

Comments
 (0)