1515 */
1616package io .serverlessworkflow .impl .persistence ;
1717
18- import io .serverlessworkflow .impl .TaskContextData ;
1918import io .serverlessworkflow .impl .WorkflowContextData ;
2019import io .serverlessworkflow .impl .WorkflowDefinitionData ;
21- import io .serverlessworkflow .impl .WorkflowStatus ;
2220import java .time .Duration ;
2321import java .util .Map ;
2422import java .util .Optional ;
2523import java .util .concurrent .CompletableFuture ;
2624import java .util .concurrent .ConcurrentHashMap ;
2725import java .util .concurrent .ExecutorService ;
28- import java .util .concurrent .TimeUnit ;
2926import java .util .function .Consumer ;
27+ import org .slf4j .Logger ;
28+ import org .slf4j .LoggerFactory ;
3029
31- public class DefaultPersistenceInstanceWriter implements PersistenceInstanceWriter {
30+ public class DefaultPersistenceInstanceWriter extends AbstractPersistenceInstanceWriter {
3231
3332 private final PersistenceInstanceStore store ;
3433 private final Map <String , CompletableFuture <Void >> futuresMap = new ConcurrentHashMap <>();
3534 private final Optional <ExecutorService > executorService ;
3635 private final Duration closeTimeout ;
3736
37+ private static final Logger logger =
38+ LoggerFactory .getLogger (DefaultPersistenceInstanceWriter .class );
39+
3840 protected DefaultPersistenceInstanceWriter (
3941 PersistenceInstanceStore store ,
4042 Optional <ExecutorService > executorService ,
@@ -45,61 +47,14 @@ protected DefaultPersistenceInstanceWriter(
4547 }
4648
4749 @ Override
48- public CompletableFuture <Void > started (WorkflowContextData workflowContext ) {
49- return doTransaction (t -> t .writeInstanceData (workflowContext ), workflowContext );
50- }
51-
52- @ Override
53- public CompletableFuture <Void > completed (WorkflowContextData workflowContext ) {
54- return removeProcessInstance (workflowContext );
55- }
56-
57- @ Override
58- public CompletableFuture <Void > failed (WorkflowContextData workflowContext , Throwable ex ) {
59- return removeProcessInstance (workflowContext );
60- }
61-
62- @ Override
63- public CompletableFuture <Void > aborted (WorkflowContextData workflowContext ) {
64- return removeProcessInstance (workflowContext );
65- }
66-
6750 protected CompletableFuture <Void > removeProcessInstance (WorkflowContextData workflowContext ) {
68- return doTransaction ( t -> t .removeProcessInstance (workflowContext ), workflowContext )
51+ return super .removeProcessInstance (workflowContext )
6952 .thenRun (() -> futuresMap .remove (workflowContext .instanceData ().id ()));
7053 }
7154
7255 @ Override
73- public CompletableFuture <Void > taskStarted (
74- WorkflowContextData workflowContext , TaskContextData taskContext ) {
75- return CompletableFuture .completedFuture (null );
76- }
77-
78- @ Override
79- public CompletableFuture <Void > taskRetried (
80- WorkflowContextData workflowContext , TaskContextData taskContext ) {
81- return doTransaction (t -> t .writeRetryTask (workflowContext , taskContext ), workflowContext );
82- }
83-
84- @ Override
85- public CompletableFuture <Void > taskCompleted (
86- WorkflowContextData workflowContext , TaskContextData taskContext ) {
87- return doTransaction (t -> t .writeCompletedTask (workflowContext , taskContext ), workflowContext );
88- }
89-
90- @ Override
91- public CompletableFuture <Void > suspended (WorkflowContextData workflowContext ) {
92- return doTransaction (
93- t -> t .writeStatus (workflowContext , WorkflowStatus .SUSPENDED ), workflowContext );
94- }
95-
96- @ Override
97- public CompletableFuture <Void > resumed (WorkflowContextData workflowContext ) {
98- return doTransaction (t -> t .clearStatus (workflowContext ), workflowContext );
99- }
100-
101- private CompletableFuture <Void > doTransaction (
102- Consumer <PersistenceInstanceTransaction > operation , WorkflowContextData context ) {
56+ protected CompletableFuture <Void > doTransaction (
57+ Consumer <PersistenceInstanceOperations > operation , WorkflowContextData context ) {
10358 final ExecutorService service =
10459 this .executorService .orElse (context .definition ().application ().executorService ());
10560 final Runnable runnable = () -> executeTransaction (operation , context .definition ());
@@ -112,28 +67,23 @@ private CompletableFuture<Void> doTransaction(
11267 }
11368
11469 private void executeTransaction (
115- Consumer <PersistenceInstanceTransaction > operation , WorkflowDefinitionData definition ) {
70+ Consumer <PersistenceInstanceOperations > operation , WorkflowDefinitionData definition ) {
11671 PersistenceInstanceTransaction transaction = store .begin ();
11772 try {
11873 operation .accept (transaction );
11974 transaction .commit (definition );
12075 } catch (Exception ex ) {
121- transaction .rollback (definition );
76+ try {
77+ transaction .rollback (definition );
78+ } catch (Exception rollEx ) {
79+ logger .warn ("Exception during rollback. Ignoring it" , ex );
80+ }
12281 throw ex ;
12382 }
12483 }
12584
12685 @ Override
12786 public void close () {
12887 futuresMap .clear ();
129- executorService .ifPresent (
130- e -> {
131- try {
132- e .awaitTermination (closeTimeout .toMillis (), TimeUnit .MILLISECONDS );
133- e .shutdown ();
134- } catch (InterruptedException ex ) {
135- Thread .currentThread ().interrupt ();
136- }
137- });
13888 }
13989}
0 commit comments