|
| 1 | +/* |
| 2 | + * Copyright 2020-Present The Serverless Workflow Specification Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.serverlessworkflow.impl.persistence; |
| 17 | + |
| 18 | +import io.serverlessworkflow.impl.WorkflowContextData; |
| 19 | +import io.serverlessworkflow.impl.WorkflowDefinitionData; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.Optional; |
| 22 | +import java.util.concurrent.CompletableFuture; |
| 23 | +import java.util.concurrent.ConcurrentHashMap; |
| 24 | +import java.util.concurrent.ExecutionException; |
| 25 | +import java.util.concurrent.ExecutorService; |
| 26 | +import java.util.function.Consumer; |
| 27 | +import org.slf4j.Logger; |
| 28 | +import org.slf4j.LoggerFactory; |
| 29 | + |
| 30 | +public abstract class AsyncPersistenceInstanceWriter extends AbstractPersistenceInstanceWriter { |
| 31 | + |
| 32 | + private static final Logger logger = |
| 33 | + LoggerFactory.getLogger(AsyncPersistenceInstanceWriter.class); |
| 34 | + |
| 35 | + private final Map<String, CompletableFuture<Void>> futuresMap = new ConcurrentHashMap<>(); |
| 36 | + |
| 37 | + @Override |
| 38 | + protected CompletableFuture<Void> doTransaction( |
| 39 | + Consumer<PersistenceInstanceOperations> operation, WorkflowContextData context) { |
| 40 | + final ExecutorService service = |
| 41 | + executorService().orElse(context.definition().application().executorService()); |
| 42 | + final Runnable runnable = () -> doTransaction(operation, context.definition()); |
| 43 | + return futuresMap.compute( |
| 44 | + context.instanceData().id(), |
| 45 | + (k, v) -> |
| 46 | + v == null |
| 47 | + ? CompletableFuture.runAsync(runnable, service) |
| 48 | + : v.thenRunAsync(runnable, service)); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + protected CompletableFuture<Void> removeProcessInstance(WorkflowContextData workflowContext) { |
| 53 | + return super.removeProcessInstance(workflowContext) |
| 54 | + .thenRun(() -> futuresMap.remove(workflowContext.instanceData().id())); |
| 55 | + } |
| 56 | + |
| 57 | + protected abstract void doTransaction( |
| 58 | + Consumer<PersistenceInstanceOperations> operation, WorkflowDefinitionData definition); |
| 59 | + |
| 60 | + protected Optional<ExecutorService> executorService() { |
| 61 | + return Optional.empty(); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public void close() { |
| 66 | + for (CompletableFuture<Void> future : futuresMap.values()) { |
| 67 | + try { |
| 68 | + future.get(); |
| 69 | + } catch (InterruptedException ex) { |
| 70 | + logger.warn("Thread interrupted while writing to db", ex); |
| 71 | + Thread.currentThread().interrupt(); |
| 72 | + } catch (ExecutionException ex) { |
| 73 | + logger.warn("Exception while writing to db", ex.getCause()); |
| 74 | + } |
| 75 | + } |
| 76 | + futuresMap.clear(); |
| 77 | + } |
| 78 | +} |
0 commit comments