From d2d2787e71f1d0e88761f8674f3df5a2fae56833 Mon Sep 17 00:00:00 2001 From: fjtirado Date: Wed, 3 Dec 2025 17:54:48 +0100 Subject: [PATCH] Adding support for instances object Some callable tasks might require to share internal implementation resources per workflow instance (for example a Python embedded interpreter or a l4cj agenticscope instance) Signed-off-by: fjtirado --- .../impl/WorkflowMutableInstance.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/impl/core/src/main/java/io/serverlessworkflow/impl/WorkflowMutableInstance.java b/impl/core/src/main/java/io/serverlessworkflow/impl/WorkflowMutableInstance.java index 6aadf211..a808650c 100644 --- a/impl/core/src/main/java/io/serverlessworkflow/impl/WorkflowMutableInstance.java +++ b/impl/core/src/main/java/io/serverlessworkflow/impl/WorkflowMutableInstance.java @@ -34,6 +34,7 @@ import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; +import java.util.function.Supplier; public class WorkflowMutableInstance implements WorkflowInstance { @@ -47,6 +48,8 @@ public class WorkflowMutableInstance implements WorkflowInstance { protected AtomicReference> futureRef = new AtomicReference<>(); protected Instant completedAt; + protected final Map additionalObjects = new ConcurrentHashMap(); + private Lock statusLock = new ReentrantLock(); private Map, TaskContext> suspended; @@ -84,14 +87,18 @@ protected final CompletableFuture startExecution(Runnable runnabl .inputFilter() .map(f -> f.apply(workflowContext, null, input)) .orElse(input)) - .whenComplete(this::whenFailed) + .whenComplete(this::whenCompleted) .thenApply(this::whenSuccess); futureRef.set(future); return future; } - private void whenFailed(WorkflowModel result, Throwable ex) { + private void whenCompleted(WorkflowModel result, Throwable ex) { completedAt = Instant.now(); + additionalObjects.values().stream() + .filter(AutoCloseable.class::isInstance) + .map(AutoCloseable.class::cast) + .forEach(WorkflowUtils::safeClose); if (ex != null) { handleException(ex instanceof CompletionException ? ex = ex.getCause() : ex); } @@ -278,5 +285,9 @@ public boolean cancel() { } } + public T additionalObject(String key, Supplier supplier) { + return (T) additionalObjects.computeIfAbsent(key, k -> supplier.get()); + } + public void restoreContext(WorkflowContext workflow, TaskContext context) {} }