Skip to content

Commit 1653e43

Browse files
committed
Adjust code
Signed-off-by: Matheus Cruz <matheuscruz.dev@gmail.com>
1 parent 9ed2ec2 commit 1653e43

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

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

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ public static boolean isSupported(String lang) {
6767
}
6868

6969
@FunctionalInterface
70-
private interface FnExecutor {
70+
private interface ScriptExecutor {
7171
WorkflowModel apply(WorkflowContext workflowContext, TaskContext taskContext);
7272
}
7373

74-
private FnExecutor fnExecutor;
74+
private ScriptExecutor scriptExecutor;
7575

7676
@Override
7777
public void init(RunScript taskConfiguration, WorkflowDefinition definition) {
@@ -90,7 +90,9 @@ public void init(RunScript taskConfiguration, WorkflowDefinition definition) {
9090
Arrays.stream(ScriptLanguage.values()).map(ScriptLanguage::getLang).toArray()));
9191
}
9292

93-
fnExecutor =
93+
String lowerLang = language.toLowerCase();
94+
95+
scriptExecutor =
9496
(workflowContext, taskContext) -> {
9597
String source;
9698
if (scriptUnion.getInlineScript() != null) {
@@ -112,11 +114,6 @@ taskContext, new IllegalStateException("No script source defined."))
112114
taskContext.input());
113115
}
114116

115-
ByteArrayOutputStream stderr = new ByteArrayOutputStream();
116-
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
117-
118-
String lowerLang = language.toLowerCase();
119-
120117
Map<String, String> envs = new HashMap<>();
121118
if (script.getEnvironment() != null) {
122119
for (Map.Entry<String, Object> entry :
@@ -149,6 +146,8 @@ taskContext, new IllegalStateException("No script source defined."))
149146
}
150147
}
151148

149+
ByteArrayOutputStream stderr = new ByteArrayOutputStream();
150+
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
152151
try (Context context =
153152
Context.newBuilder(lowerLang)
154153
.err(stderr)
@@ -168,14 +167,14 @@ taskContext, new IllegalStateException("No script source defined."))
168167
});
169168

170169
// configure process.env for js environment variables
171-
if (language.equalsIgnoreCase(ScriptLanguage.JS.lang)) {
170+
if (lowerLang.equalsIgnoreCase(ScriptLanguage.JS.lang)) {
172171
configureProcessEnv(context, envs);
173172
}
174173

175174
if (!isAwait) {
176175
executorService.submit(
177176
() -> {
178-
context.eval(scriptUnion.getInlineScript().getLanguage(), source);
177+
context.eval(lowerLang, source);
179178
});
180179
return application.modelFactory().fromAny(taskContext.input());
181180
}
@@ -205,13 +204,11 @@ taskContext, new IllegalStateException("No script source defined."))
205204
case ALL ->
206205
modelFactory.fromAny(
207206
new ProcessResult(
208-
e.getExitStatus(),
209-
stdout.toString().trim(),
210-
getStderrMessage(e, stderr)));
207+
e.getExitStatus(), stdout.toString().trim(), buildStderr(e, stderr)));
211208
case NONE -> modelFactory.fromNull();
212209
case CODE -> modelFactory.from(e.getExitStatus());
213210
case STDOUT -> modelFactory.from(stdout.toString().trim());
214-
case STDERR -> modelFactory.from(getStderrMessage(e, stderr));
211+
case STDERR -> modelFactory.from(buildStderr(e, stderr));
215212
};
216213
}
217214
}
@@ -225,7 +222,7 @@ taskContext, new IllegalStateException("No script source defined."))
225222
* @param stderr the stderr stream
226223
* @return the stderr message
227224
*/
228-
private String getStderrMessage(PolyglotException e, ByteArrayOutputStream stderr) {
225+
private String buildStderr(PolyglotException e, ByteArrayOutputStream stderr) {
229226
String err = stderr.toString();
230227
return err.isBlank() ? e.getMessage() : err.trim();
231228
}
@@ -251,7 +248,8 @@ private void configureProcessEnv(Context context, Map<String, String> envs) {
251248
@Override
252249
public CompletableFuture<WorkflowModel> apply(
253250
WorkflowContext workflowContext, TaskContext taskContext, WorkflowModel input) {
254-
return CompletableFuture.supplyAsync(() -> this.fnExecutor.apply(workflowContext, taskContext));
251+
return CompletableFuture.supplyAsync(
252+
() -> this.scriptExecutor.apply(workflowContext, taskContext));
255253
}
256254

257255
@Override

0 commit comments

Comments
 (0)