-
Notifications
You must be signed in to change notification settings - Fork 52
[Fix #1286] Non blocking persistence #1288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -70,30 +70,43 @@ public CompletableFuture<WorkflowModel> start() { | |||||||||||||||||||||||||||||||||||||||||||||
| return startExecution( | ||||||||||||||||||||||||||||||||||||||||||||||
| () -> { | ||||||||||||||||||||||||||||||||||||||||||||||
| startedAt = Instant.now(); | ||||||||||||||||||||||||||||||||||||||||||||||
| publishEvent( | ||||||||||||||||||||||||||||||||||||||||||||||
| return publishEvent( | ||||||||||||||||||||||||||||||||||||||||||||||
| workflowContext, l -> l.onWorkflowStarted(new WorkflowStartedEvent(workflowContext))); | ||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| protected final CompletableFuture<WorkflowModel> startExecution(Runnable runnable) { | ||||||||||||||||||||||||||||||||||||||||||||||
| protected final CompletableFuture<WorkflowModel> startExecution( | ||||||||||||||||||||||||||||||||||||||||||||||
| Supplier<CompletableFuture<?>> runnable) { | ||||||||||||||||||||||||||||||||||||||||||||||
| CompletableFuture<WorkflowModel> future = futureRef.get(); | ||||||||||||||||||||||||||||||||||||||||||||||
| if (future != null) { | ||||||||||||||||||||||||||||||||||||||||||||||
| return future; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| status(WorkflowStatus.RUNNING); | ||||||||||||||||||||||||||||||||||||||||||||||
| runnable.run(); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| future = | ||||||||||||||||||||||||||||||||||||||||||||||
| TaskExecutorHelper.processTaskList( | ||||||||||||||||||||||||||||||||||||||||||||||
| workflowContext.definition().startTask(), | ||||||||||||||||||||||||||||||||||||||||||||||
| workflowContext, | ||||||||||||||||||||||||||||||||||||||||||||||
| Optional.empty(), | ||||||||||||||||||||||||||||||||||||||||||||||
| workflowContext | ||||||||||||||||||||||||||||||||||||||||||||||
| .definition() | ||||||||||||||||||||||||||||||||||||||||||||||
| .inputFilter() | ||||||||||||||||||||||||||||||||||||||||||||||
| .map(f -> f.apply(workflowContext, null, input)) | ||||||||||||||||||||||||||||||||||||||||||||||
| .orElse(input)) | ||||||||||||||||||||||||||||||||||||||||||||||
| .whenComplete(this::whenCompleted) | ||||||||||||||||||||||||||||||||||||||||||||||
| .thenApply(this::whenSuccess); | ||||||||||||||||||||||||||||||||||||||||||||||
| runnable | ||||||||||||||||||||||||||||||||||||||||||||||
| .get() | ||||||||||||||||||||||||||||||||||||||||||||||
| .thenCompose( | ||||||||||||||||||||||||||||||||||||||||||||||
| v -> | ||||||||||||||||||||||||||||||||||||||||||||||
| TaskExecutorHelper.processTaskList( | ||||||||||||||||||||||||||||||||||||||||||||||
| workflowContext.definition().startTask(), | ||||||||||||||||||||||||||||||||||||||||||||||
| workflowContext, | ||||||||||||||||||||||||||||||||||||||||||||||
| Optional.empty(), | ||||||||||||||||||||||||||||||||||||||||||||||
| workflowContext | ||||||||||||||||||||||||||||||||||||||||||||||
| .definition() | ||||||||||||||||||||||||||||||||||||||||||||||
| .inputFilter() | ||||||||||||||||||||||||||||||||||||||||||||||
| .map(f -> f.apply(workflowContext, null, input)) | ||||||||||||||||||||||||||||||||||||||||||||||
| .orElse(input)) | ||||||||||||||||||||||||||||||||||||||||||||||
| .whenComplete(this::whenCompleted) | ||||||||||||||||||||||||||||||||||||||||||||||
| .thenApply(this::whenSuccess) | ||||||||||||||||||||||||||||||||||||||||||||||
| .thenCompose( | ||||||||||||||||||||||||||||||||||||||||||||||
| model -> | ||||||||||||||||||||||||||||||||||||||||||||||
| publishEvent( | ||||||||||||||||||||||||||||||||||||||||||||||
| workflowContext, | ||||||||||||||||||||||||||||||||||||||||||||||
| l -> | ||||||||||||||||||||||||||||||||||||||||||||||
| l.onWorkflowCompleted( | ||||||||||||||||||||||||||||||||||||||||||||||
| new WorkflowCompletedEvent(workflowContext, model))) | ||||||||||||||||||||||||||||||||||||||||||||||
| .thenApply(__ -> model))); | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+102
to
+109
|
||||||||||||||||||||||||||||||||||||||||||||||
| .thenCompose( | |
| model -> | |
| publishEvent( | |
| workflowContext, | |
| l -> | |
| l.onWorkflowCompleted( | |
| new WorkflowCompletedEvent(workflowContext, model))) | |
| .thenApply(__ -> model))); | |
| .thenApply( | |
| model -> { | |
| publishEvent( | |
| workflowContext, | |
| l -> | |
| l.onWorkflowCompleted( | |
| new WorkflowCompletedEvent(workflowContext, model))) | |
| .exceptionally( | |
| ex -> { | |
| // TODO: handle workflow-completed listener failure (log, metrics, etc.) | |
| return null; | |
| }); | |
| return model; | |
| }); |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -213,17 +213,24 @@ public CompletableFuture<TaskContext> apply( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| completable = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| completable | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .thenCompose(workflowContext.instance()::suspendedCheck) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .thenCompose( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| t -> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| CompletableFuture<?> events = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| t.isRetrying() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? publishEvent( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| workflowContext, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| l -> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| l.onTaskRetried( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| new TaskRetriedEvent(workflowContext, taskContext))) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| : publishEvent( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| workflowContext, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| l -> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| l.onTaskStarted( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| new TaskStartedEvent(workflowContext, taskContext))); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return events.thenApply(v -> t); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+216
to
+230
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .thenCompose( | |
| t -> { | |
| CompletableFuture<?> events = | |
| t.isRetrying() | |
| ? publishEvent( | |
| workflowContext, | |
| l -> | |
| l.onTaskRetried( | |
| new TaskRetriedEvent(workflowContext, taskContext))) | |
| : publishEvent( | |
| workflowContext, | |
| l -> | |
| l.onTaskStarted( | |
| new TaskStartedEvent(workflowContext, taskContext))); | |
| return events.thenApply(v -> t); | |
| .thenApply( | |
| t -> { | |
| if (t.isRetrying()) { | |
| publishEvent( | |
| workflowContext, | |
| l -> | |
| l.onTaskRetried( | |
| new TaskRetriedEvent(workflowContext, taskContext))); | |
| } else { | |
| publishEvent( | |
| workflowContext, | |
| l -> | |
| l.onTaskStarted( | |
| new TaskStartedEvent(workflowContext, taskContext))); | |
| } | |
| return t; |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -16,29 +16,19 @@ | |||||||||||||||||||||||||||||||||
| package io.serverlessworkflow.impl.lifecycle; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| import io.serverlessworkflow.impl.WorkflowContext; | ||||||||||||||||||||||||||||||||||
| import java.util.function.Consumer; | ||||||||||||||||||||||||||||||||||
| import org.slf4j.Logger; | ||||||||||||||||||||||||||||||||||
| import org.slf4j.LoggerFactory; | ||||||||||||||||||||||||||||||||||
| import java.util.concurrent.CompletableFuture; | ||||||||||||||||||||||||||||||||||
| import java.util.function.Function; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| public class LifecycleEventsUtils { | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| private LifecycleEventsUtils() {} | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| private static final Logger logger = LoggerFactory.getLogger(LifecycleEventsUtils.class); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| public static <T extends TaskEvent> void publishEvent( | ||||||||||||||||||||||||||||||||||
| WorkflowContext workflowContext, Consumer<WorkflowExecutionListener> consumer) { | ||||||||||||||||||||||||||||||||||
| workflowContext | ||||||||||||||||||||||||||||||||||
| .definition() | ||||||||||||||||||||||||||||||||||
| .application() | ||||||||||||||||||||||||||||||||||
| .listeners() | ||||||||||||||||||||||||||||||||||
| .forEach( | ||||||||||||||||||||||||||||||||||
| v -> { | ||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||
| consumer.accept(v); | ||||||||||||||||||||||||||||||||||
| } catch (Exception ex) { | ||||||||||||||||||||||||||||||||||
| logger.error("Error processing listener. Ignoring and going on", ex); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||
| public static CompletableFuture<?> publishEvent( | ||||||||||||||||||||||||||||||||||
| WorkflowContext workflowContext, | ||||||||||||||||||||||||||||||||||
| Function<WorkflowExecutionCompletableListener, CompletableFuture<?>> function) { | ||||||||||||||||||||||||||||||||||
| return CompletableFuture.allOf( | ||||||||||||||||||||||||||||||||||
| workflowContext.definition().application().listeners().stream() | ||||||||||||||||||||||||||||||||||
| .map(v -> function.apply(v)) | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
| .map(v -> function.apply(v)) | |
| .map( | |
| listener -> { | |
| try { | |
| CompletableFuture<?> future = function.apply(listener); | |
| if (future == null) { | |
| // Treat null as an already-completed listener to avoid NPEs | |
| return CompletableFuture.completedFuture(null); | |
| } | |
| // Swallow exceptional completion to preserve best-effort semantics | |
| return future.exceptionally(ex -> null); | |
| } catch (Throwable t) { | |
| // Ensure one listener throwing synchronously doesn't prevent others | |
| return CompletableFuture.completedFuture(null); | |
| } | |
| }) |
Copilot
AI
Apr 1, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This changes listener failure semantics: previously exceptions from listeners were caught/logged and execution continued; now (1) a synchronous exception thrown by function.apply(v) will abort publishing immediately, and (2) any returned future completing exceptionally will cause allOf(...) to complete exceptionally. Since callers now thenCompose on publishEvent, a single bad listener can fail the workflow/task execution. Consider wrapping each invocation to convert synchronous throws into a completed future, and attaching exceptionally(...)/handle(...) per-listener (with logging) so publishing remains best-effort like before.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /* | ||
| * 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.lifecycle; | ||
|
|
||
| import io.serverlessworkflow.impl.ServicePriority; | ||
| import java.util.concurrent.CompletableFuture; | ||
|
|
||
| public interface WorkflowExecutionCompletableListener extends AutoCloseable, ServicePriority { | ||
|
|
||
| default CompletableFuture<?> onWorkflowStarted(WorkflowStartedEvent ev) { | ||
| return CompletableFuture.completedFuture(null); | ||
| } | ||
|
|
||
| default CompletableFuture<?> onWorkflowSuspended(WorkflowSuspendedEvent ev) { | ||
| return CompletableFuture.completedFuture(null); | ||
| } | ||
|
|
||
| default CompletableFuture<?> onWorkflowResumed(WorkflowResumedEvent ev) { | ||
| return CompletableFuture.completedFuture(null); | ||
| } | ||
|
|
||
| default CompletableFuture<?> onWorkflowCompleted(WorkflowCompletedEvent ev) { | ||
| return CompletableFuture.completedFuture(null); | ||
| } | ||
|
|
||
| default CompletableFuture<?> onWorkflowFailed(WorkflowFailedEvent ev) { | ||
| return CompletableFuture.completedFuture(null); | ||
| } | ||
|
|
||
| default CompletableFuture<?> onWorkflowCancelled(WorkflowCancelledEvent ev) { | ||
| return CompletableFuture.completedFuture(null); | ||
| } | ||
|
|
||
| default CompletableFuture<?> onTaskStarted(TaskStartedEvent ev) { | ||
| return CompletableFuture.completedFuture(null); | ||
| } | ||
|
|
||
| default CompletableFuture<?> onTaskCompleted(TaskCompletedEvent ev) { | ||
| return CompletableFuture.completedFuture(null); | ||
| } | ||
|
|
||
| default CompletableFuture<?> onTaskFailed(TaskFailedEvent ev) { | ||
| return CompletableFuture.completedFuture(null); | ||
| } | ||
|
|
||
| default CompletableFuture<?> onTaskCancelled(TaskCancelledEvent ev) { | ||
| return CompletableFuture.completedFuture(null); | ||
| } | ||
|
|
||
| default CompletableFuture<?> onTaskSuspended(TaskSuspendedEvent ev) { | ||
| return CompletableFuture.completedFuture(null); | ||
| } | ||
|
|
||
| default CompletableFuture<?> onTaskResumed(TaskResumedEvent ev) { | ||
| return CompletableFuture.completedFuture(null); | ||
| } | ||
|
|
||
| default CompletableFuture<?> onTaskRetried(TaskRetriedEvent ev) { | ||
| return CompletableFuture.completedFuture(null); | ||
| } | ||
|
|
||
| default CompletableFuture<?> onWorkflowStatusChanged(WorkflowStatusEvent ev) { | ||
| return CompletableFuture.completedFuture(null); | ||
| } | ||
|
|
||
| @Override | ||
| default void close() {} | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WorkflowExecutionCompletableListeneralready extendsAutoCloseable, so listingAutoCloseablehere is redundant. Dropping the extra interface improves clarity without changing behavior.