|
| 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.api.types.Workflow; |
| 19 | +import io.serverlessworkflow.impl.WorkflowApplication; |
| 20 | +import io.serverlessworkflow.impl.WorkflowDefinition; |
| 21 | +import io.serverlessworkflow.impl.WorkflowInstance; |
| 22 | + |
| 23 | +public class WorkflowPersistenceApplication<T extends WorkflowMinimumPersistenceReader> |
| 24 | + extends WorkflowApplication { |
| 25 | + |
| 26 | + private final T reader; |
| 27 | + private final WorkflowPersistenceWriter writer; |
| 28 | + private final boolean resumeAfterReboot; |
| 29 | + |
| 30 | + protected WorkflowPersistenceApplication(Builder<T> builder) { |
| 31 | + super(builder); |
| 32 | + this.reader = builder.reader; |
| 33 | + this.writer = builder.writer; |
| 34 | + this.resumeAfterReboot = builder.resumeAfterReboot; |
| 35 | + } |
| 36 | + |
| 37 | + public T persitenceReader() { |
| 38 | + return reader; |
| 39 | + } |
| 40 | + |
| 41 | + public void close() { |
| 42 | + super.close(); |
| 43 | + try { |
| 44 | + reader.close(); |
| 45 | + } catch (Exception e) { |
| 46 | + } |
| 47 | + try { |
| 48 | + writer.close(); |
| 49 | + } catch (Exception e) { |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public static <T extends WorkflowMinimumPersistenceReader> Builder<T> builder( |
| 54 | + WorkflowPersistenceWriter writer, T reader) { |
| 55 | + return new Builder<>(writer, reader); |
| 56 | + } |
| 57 | + |
| 58 | + public static class Builder<T extends WorkflowMinimumPersistenceReader> |
| 59 | + extends io.serverlessworkflow.impl.WorkflowApplication.Builder { |
| 60 | + |
| 61 | + private final WorkflowPersistenceWriter writer; |
| 62 | + private final T reader; |
| 63 | + private boolean resumeAfterReboot = true; |
| 64 | + |
| 65 | + protected Builder(WorkflowPersistenceWriter writer, T reader) { |
| 66 | + this.writer = writer; |
| 67 | + this.reader = reader; |
| 68 | + super.withListener(new WorkflowPersistenceListener(writer)); |
| 69 | + } |
| 70 | + |
| 71 | + public Builder<T> resumeAfterReboot(boolean resumeAfterReboot) { |
| 72 | + this.resumeAfterReboot = resumeAfterReboot; |
| 73 | + return this; |
| 74 | + } |
| 75 | + |
| 76 | + public WorkflowPersistenceApplication<T> build() { |
| 77 | + return new WorkflowPersistenceApplication<>(this); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + protected WorkflowDefinition createDefinition(Workflow workflow) { |
| 82 | + WorkflowDefinition definition = super.createDefinition(workflow); |
| 83 | + if (resumeAfterReboot) { |
| 84 | + reader.all(definition).forEach(WorkflowInstance::resume); |
| 85 | + } |
| 86 | + return definition; |
| 87 | + } |
| 88 | +} |
0 commit comments