44
55import com .amazonaws .lambda .durable .exception .DurableOperationException ;
66import com .amazonaws .lambda .durable .execution .ExecutionManager ;
7+ import com .amazonaws .lambda .durable .execution .SuspendExecutionException ;
78import com .amazonaws .lambda .durable .model .DurableExecutionInput ;
89import com .amazonaws .lambda .durable .model .DurableExecutionOutput ;
910import com .amazonaws .lambda .durable .serde .SerDes ;
@@ -81,11 +82,7 @@ public static <I, O> DurableExecutionOutput execute(
8182 userExecutor );
8283
8384 // Get suspend future from ExecutionManager. If this future completes, it
84- // indicates
85- // that no threads are active and we can safely suspend. This is useful for
86- // async scenarios where multiple operations are scheduled concurrently and
87- // awaited
88- // at a later point.
85+ // indicates that no threads are active and we can safely suspend.
8986 var suspendFuture = executionManager .getSuspendExecutionFuture ();
9087
9188 // Wait for either handler to complete or suspension to occur
@@ -96,16 +93,6 @@ public static <I, O> DurableExecutionOutput execute(
9693 return DurableExecutionOutput .pending ();
9794 }
9895
99- if (handlerFuture .isCompletedExceptionally ()) {
100- try {
101- handlerFuture .join (); // Will throw the exception
102- } catch (Exception e ) {
103- Throwable cause = ExceptionHelper .unwrapCompletableFuture (e );
104- logger .debug ("Execution failed: {}" , cause .getMessage ());
105- return DurableExecutionOutput .failure (buildErrorObject (cause , serDes ));
106- }
107- }
108-
10996 var result = handlerFuture .get ();
11097 var outputPayload = serDes .serialize (result );
11198
@@ -137,7 +124,13 @@ public static <I, O> DurableExecutionOutput execute(
137124 logger .debug ("Execution completed" );
138125 return DurableExecutionOutput .success (outputPayload );
139126 } catch (Exception e ) {
140- return DurableExecutionOutput .failure (buildErrorObject (ExceptionHelper .unwrapCompletableFuture (e ), serDes ));
127+ Throwable cause = ExceptionHelper .unwrapCompletableFuture (e );
128+ if (cause instanceof SuspendExecutionException ) {
129+ logger .debug ("Execution suspended" );
130+ return DurableExecutionOutput .pending ();
131+ }
132+ logger .debug ("Execution failed: {}" , cause .getMessage ());
133+ return DurableExecutionOutput .failure (buildErrorObject (cause , serDes ));
141134 } finally {
142135 // We shutdown the execution to make sure remaining checkpoint calls in the queue are drained
143136 executionManager .shutdown ();
0 commit comments