|
23 | 23 | import io.serverlessworkflow.impl.WorkflowApplication; |
24 | 24 | import io.serverlessworkflow.impl.WorkflowContext; |
25 | 25 | import io.serverlessworkflow.impl.WorkflowDefinition; |
26 | | -import io.serverlessworkflow.impl.WorkflowError; |
27 | | -import io.serverlessworkflow.impl.WorkflowException; |
28 | 26 | import io.serverlessworkflow.impl.WorkflowModel; |
29 | 27 | import io.serverlessworkflow.impl.WorkflowUtils; |
30 | 28 | import io.serverlessworkflow.impl.WorkflowValueResolver; |
31 | 29 | import io.serverlessworkflow.impl.resources.ResourceLoaderUtils; |
32 | | -import java.util.Arrays; |
33 | | -import java.util.HashMap; |
| 30 | +import io.serverlessworkflow.impl.scripts.ScriptContext; |
| 31 | +import io.serverlessworkflow.impl.scripts.ScriptLanguageId; |
| 32 | +import io.serverlessworkflow.impl.scripts.ScriptRunner; |
34 | 33 | import java.util.Map; |
| 34 | +import java.util.Objects; |
| 35 | +import java.util.Optional; |
35 | 36 | import java.util.ServiceLoader; |
36 | 37 | import java.util.concurrent.CompletableFuture; |
37 | 38 |
|
38 | 39 | public class RunScriptExecutor implements RunnableTask<RunScript> { |
39 | 40 |
|
40 | | - public enum LanguageId { |
41 | | - JS("js"), |
42 | | - PYTHON("python"); |
| 41 | + private Optional<WorkflowValueResolver<Map<String, Object>>> environmentExpr; |
43 | 42 |
|
44 | | - private final String lang; |
| 43 | + private Optional<WorkflowValueResolver<Map<String, Object>>> argumentExpr; |
45 | 44 |
|
46 | | - LanguageId(String lang) { |
47 | | - this.lang = lang; |
48 | | - } |
49 | | - |
50 | | - public String getLang() { |
51 | | - return lang; |
52 | | - } |
53 | | - |
54 | | - public static boolean isSupported(String lang) { |
55 | | - for (LanguageId l : LanguageId.values()) { |
56 | | - if (l.getLang().equalsIgnoreCase(lang)) { |
57 | | - return true; |
58 | | - } |
59 | | - } |
60 | | - return false; |
61 | | - } |
62 | | - } |
63 | | - |
64 | | - @FunctionalInterface |
65 | | - private interface CodeSupplier { |
66 | | - String apply(WorkflowContext workflowContext, TaskContext taskContext); |
67 | | - } |
68 | | - |
69 | | - @SuppressWarnings("rawtypes") |
70 | | - private Map<String, WorkflowValueResolver> environmentExpr; |
71 | | - |
72 | | - @SuppressWarnings("rawtypes") |
73 | | - private Map<String, WorkflowValueResolver> argumentExpr; |
74 | | - |
75 | | - private CodeSupplier codeSupplier; |
| 45 | + private WorkflowValueResolver<String> codeSupplier; |
76 | 46 | private boolean isAwait; |
77 | 47 | private RunTaskConfiguration.ProcessReturnType returnType; |
78 | | - private ScriptTaskRunner taskRunner; |
| 48 | + private ScriptRunner taskRunner; |
79 | 49 |
|
80 | 50 | @Override |
81 | 51 | public void init(RunScript taskConfiguration, WorkflowDefinition definition) { |
82 | 52 | ScriptUnion scriptUnion = taskConfiguration.getScript(); |
83 | 53 | Script script = scriptUnion.get(); |
84 | | - String language = scriptUnion.get().getLanguage(); |
85 | | - |
86 | | - WorkflowApplication application = definition.application(); |
87 | | - if (language == null || !LanguageId.isSupported(language)) { |
88 | | - throw new IllegalArgumentException( |
89 | | - "Unsupported script language: " |
90 | | - + language |
91 | | - + ". Supported languages are: " |
92 | | - + Arrays.toString( |
93 | | - Arrays.stream(LanguageId.values()).map(LanguageId::getLang).toArray())); |
94 | | - } |
| 54 | + ScriptLanguageId language = ScriptLanguageId.from(script.getLanguage()); |
95 | 55 |
|
96 | 56 | this.taskRunner = |
97 | | - ServiceLoader.load(ScriptTaskRunner.class) |
| 57 | + ServiceLoader.load(ScriptRunner.class).stream() |
| 58 | + .map(ServiceLoader.Provider::get) |
| 59 | + .filter(s -> s.identifier().equals(language)) |
98 | 60 | .findFirst() |
99 | 61 | .orElseThrow( |
100 | | - () -> new IllegalStateException("No implementation found for ScriptTaskRunner")); |
| 62 | + () -> |
| 63 | + new IllegalStateException( |
| 64 | + "No script runner implementation found for language " + language)); |
101 | 65 |
|
102 | 66 | this.isAwait = taskConfiguration.isAwait(); |
103 | 67 |
|
104 | 68 | this.returnType = taskConfiguration.getReturn(); |
105 | 69 |
|
106 | | - if (script.getEnvironment() != null |
107 | | - && script.getEnvironment().getAdditionalProperties() != null) { |
108 | | - this.environmentExpr = |
109 | | - buildMapResolvers(application, script.getEnvironment().getAdditionalProperties()); |
110 | | - } else { |
111 | | - this.environmentExpr = Map.of(); |
112 | | - } |
113 | | - |
114 | | - if (script.getArguments() != null && script.getArguments().getAdditionalProperties() != null) { |
115 | | - this.argumentExpr = |
116 | | - buildMapResolvers(application, script.getArguments().getAdditionalProperties()); |
117 | | - } else { |
118 | | - this.argumentExpr = Map.of(); |
119 | | - } |
| 70 | + WorkflowApplication application = definition.application(); |
| 71 | + this.environmentExpr = |
| 72 | + script.getEnvironment() != null && script.getEnvironment().getAdditionalProperties() != null |
| 73 | + ? Optional.of( |
| 74 | + WorkflowUtils.buildMapResolver( |
| 75 | + application, null, script.getEnvironment().getAdditionalProperties())) |
| 76 | + : Optional.empty(); |
| 77 | + |
| 78 | + this.argumentExpr = |
| 79 | + script.getArguments() != null && script.getArguments().getAdditionalProperties() != null |
| 80 | + ? Optional.of( |
| 81 | + WorkflowUtils.buildMapResolver( |
| 82 | + application, null, script.getArguments().getAdditionalProperties())) |
| 83 | + : Optional.empty(); |
120 | 84 |
|
121 | 85 | this.codeSupplier = |
122 | | - (workflowContext, taskContext) -> { |
123 | | - if (scriptUnion.getInlineScript() != null) { |
124 | | - return scriptUnion.getInlineScript().getCode(); |
125 | | - } else if (scriptUnion.getExternalScript() == null) { |
126 | | - throw new WorkflowException( |
127 | | - WorkflowError.runtime( |
128 | | - taskContext, new IllegalStateException("No script source defined.")) |
129 | | - .build()); |
130 | | - } else { |
131 | | - return definition |
132 | | - .resourceLoader() |
133 | | - .load( |
134 | | - scriptUnion.getExternalScript().getSource(), |
135 | | - ResourceLoaderUtils::readString, |
136 | | - workflowContext, |
137 | | - taskContext, |
138 | | - taskContext.input()); |
139 | | - } |
140 | | - }; |
| 86 | + scriptUnion.getInlineScript() != null |
| 87 | + ? WorkflowUtils.buildStringFilter(application, scriptUnion.getInlineScript().getCode()) |
| 88 | + : (w, t, m) -> |
| 89 | + definition |
| 90 | + .resourceLoader() |
| 91 | + .load( |
| 92 | + Objects.requireNonNull( |
| 93 | + scriptUnion.getExternalScript(), |
| 94 | + "External script is required if inline script was not set") |
| 95 | + .getSource(), |
| 96 | + ResourceLoaderUtils::readString, |
| 97 | + w, |
| 98 | + t, |
| 99 | + m); |
141 | 100 | } |
142 | 101 |
|
143 | 102 | @Override |
144 | 103 | public CompletableFuture<WorkflowModel> apply( |
145 | 104 | WorkflowContext workflowContext, TaskContext taskContext, WorkflowModel input) { |
146 | | - |
147 | | - RunScriptContext.RunScriptContextBuilder builder = |
148 | | - new RunScriptContext.RunScriptContextBuilder(); |
149 | | - |
150 | | - Map<String, String> envs = new HashMap<>(); |
151 | | - this.environmentExpr.forEach( |
152 | | - (k, v) -> { |
153 | | - Object resolved = v.apply(workflowContext, taskContext, input); |
154 | | - envs.put(k, resolved.toString()); |
155 | | - }); |
156 | | - |
157 | | - Map<String, Object> args = new HashMap<>(); |
158 | | - this.argumentExpr.forEach( |
159 | | - (k, v) -> { |
160 | | - Object resolved = v.apply(workflowContext, taskContext, input); |
161 | | - args.put(k, resolved); |
162 | | - }); |
163 | | - |
164 | | - String code = this.codeSupplier.apply(workflowContext, taskContext); |
165 | | - |
166 | | - RunScriptContext scriptContext = |
167 | | - builder |
168 | | - .withApplication(workflowContext.definition().application()) |
169 | | - .withReturnType(returnType) |
170 | | - .withCode(code) |
171 | | - .withArguments(args) |
172 | | - .withEnvironment(envs) |
173 | | - .withAwait(isAwait) |
174 | | - .build(); |
175 | | - |
176 | 105 | return CompletableFuture.supplyAsync( |
177 | | - () -> taskRunner.buildRun(taskContext).apply(scriptContext, input)); |
| 106 | + () -> |
| 107 | + taskRunner.runScript( |
| 108 | + new ScriptContext( |
| 109 | + argumentExpr |
| 110 | + .map(m -> m.apply(workflowContext, taskContext, input)) |
| 111 | + .orElse(Map.of()), |
| 112 | + environmentExpr |
| 113 | + .map(m -> m.apply(workflowContext, taskContext, input)) |
| 114 | + .orElse(Map.of()), |
| 115 | + codeSupplier.apply(workflowContext, taskContext, input), |
| 116 | + isAwait, |
| 117 | + returnType), |
| 118 | + workflowContext, |
| 119 | + taskContext, |
| 120 | + input)); |
178 | 121 | } |
179 | 122 |
|
180 | 123 | @Override |
181 | 124 | public boolean accept(Class<? extends RunTaskConfiguration> clazz) { |
182 | 125 | return RunScript.class.equals(clazz); |
183 | 126 | } |
184 | | - |
185 | | - /** Builds a map of WorkflowValueResolvers from the provided properties. */ |
186 | | - @SuppressWarnings("rawtypes") |
187 | | - private Map<String, WorkflowValueResolver> buildMapResolvers( |
188 | | - WorkflowApplication application, Map<String, Object> properties) { |
189 | | - Map<String, WorkflowValueResolver> resolvers = new HashMap<>(); |
190 | | - if (properties != null) { |
191 | | - for (Map.Entry<String, Object> entry : properties.entrySet()) { |
192 | | - WorkflowValueResolver<String> valueResolver = |
193 | | - WorkflowUtils.buildStringFilter(application, entry.getValue().toString()); |
194 | | - resolvers.put(entry.getKey(), valueResolver); |
195 | | - } |
196 | | - } |
197 | | - return resolvers; |
198 | | - } |
199 | | - |
200 | | - public static class RunScriptContext { |
201 | | - private final WorkflowApplication application; |
202 | | - private final Map<String, Object> args; |
203 | | - private final Map<String, String> envs; |
204 | | - private final String code; |
205 | | - private final boolean isAwait; |
206 | | - private final RunTaskConfiguration.ProcessReturnType returnType; |
207 | | - |
208 | | - public RunScriptContext(RunScriptContextBuilder builder) { |
209 | | - this.application = builder.application; |
210 | | - this.args = builder.args; |
211 | | - this.envs = builder.envs; |
212 | | - this.code = builder.code; |
213 | | - this.isAwait = builder.awaiting; |
214 | | - this.returnType = builder.returnType; |
215 | | - } |
216 | | - |
217 | | - public Map<String, Object> getArgs() { |
218 | | - return args; |
219 | | - } |
220 | | - |
221 | | - public Map<String, String> getEnvs() { |
222 | | - return envs; |
223 | | - } |
224 | | - |
225 | | - public String getCode() { |
226 | | - return code; |
227 | | - } |
228 | | - |
229 | | - public boolean isAwait() { |
230 | | - return isAwait; |
231 | | - } |
232 | | - |
233 | | - public WorkflowApplication getApplication() { |
234 | | - return application; |
235 | | - } |
236 | | - |
237 | | - public RunTaskConfiguration.ProcessReturnType getReturnType() { |
238 | | - return returnType; |
239 | | - } |
240 | | - |
241 | | - public static class RunScriptContextBuilder { |
242 | | - private Map<String, Object> args; |
243 | | - private Map<String, String> envs; |
244 | | - private String code; |
245 | | - private boolean awaiting; |
246 | | - private WorkflowApplication application; |
247 | | - private RunTaskConfiguration.ProcessReturnType returnType; |
248 | | - |
249 | | - public RunScriptContextBuilder withArguments(Map<String, Object> args) { |
250 | | - this.args = args; |
251 | | - return this; |
252 | | - } |
253 | | - |
254 | | - public RunScriptContextBuilder withEnvironment(Map<String, String> envs) { |
255 | | - this.envs = envs; |
256 | | - return this; |
257 | | - } |
258 | | - |
259 | | - public RunScriptContextBuilder withCode(String code) { |
260 | | - this.code = code; |
261 | | - return this; |
262 | | - } |
263 | | - |
264 | | - public RunScriptContextBuilder withAwait(boolean awaiting) { |
265 | | - this.awaiting = awaiting; |
266 | | - return this; |
267 | | - } |
268 | | - |
269 | | - public RunScriptContextBuilder withApplication(WorkflowApplication application) { |
270 | | - this.application = application; |
271 | | - return this; |
272 | | - } |
273 | | - |
274 | | - public RunScriptContextBuilder withReturnType( |
275 | | - RunTaskConfiguration.ProcessReturnType returnType) { |
276 | | - this.returnType = returnType; |
277 | | - return this; |
278 | | - } |
279 | | - |
280 | | - public RunScriptContext build() { |
281 | | - return new RunScriptContext(this); |
282 | | - } |
283 | | - } |
284 | | - } |
285 | 127 | } |
0 commit comments