Skip to content

Commit 86bef90

Browse files
authored
Merge pull request #694 from fjtirado/Fix_#693
[Fix #693] Adding lifecycle events
2 parents da3ad8e + 8c44360 commit 86bef90

File tree

67 files changed

+1530
-187
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+1530
-187
lines changed

experimental/lambda/src/main/java/io/serverlessworkflow/impl/executors/func/JavaForExecutorBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import io.serverlessworkflow.api.types.func.ForTaskFunction;
2424
import io.serverlessworkflow.api.types.func.TypedFunction;
2525
import io.serverlessworkflow.impl.WorkflowApplication;
26-
import io.serverlessworkflow.impl.WorkflowPosition;
26+
import io.serverlessworkflow.impl.WorkflowMutablePosition;
2727
import io.serverlessworkflow.impl.WorkflowPredicate;
2828
import io.serverlessworkflow.impl.WorkflowValueResolver;
2929
import io.serverlessworkflow.impl.executors.ForExecutor.ForExecutorBuilder;
@@ -36,7 +36,7 @@
3636
public class JavaForExecutorBuilder extends ForExecutorBuilder {
3737

3838
protected JavaForExecutorBuilder(
39-
WorkflowPosition position,
39+
WorkflowMutablePosition position,
4040
ForTask task,
4141
Workflow workflow,
4242
WorkflowApplication application,

experimental/lambda/src/main/java/io/serverlessworkflow/impl/executors/func/JavaSwitchExecutorBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import io.serverlessworkflow.api.types.func.SwitchCaseFunction;
2323
import io.serverlessworkflow.api.types.func.TypedPredicate;
2424
import io.serverlessworkflow.impl.WorkflowApplication;
25-
import io.serverlessworkflow.impl.WorkflowPosition;
25+
import io.serverlessworkflow.impl.WorkflowMutablePosition;
2626
import io.serverlessworkflow.impl.WorkflowPredicate;
2727
import io.serverlessworkflow.impl.executors.SwitchExecutor.SwitchExecutorBuilder;
2828
import io.serverlessworkflow.impl.expressions.ExpressionDescriptor;
@@ -33,7 +33,7 @@
3333
public class JavaSwitchExecutorBuilder extends SwitchExecutorBuilder {
3434

3535
protected JavaSwitchExecutorBuilder(
36-
WorkflowPosition position,
36+
WorkflowMutablePosition position,
3737
SwitchTask task,
3838
Workflow workflow,
3939
WorkflowApplication application,

experimental/lambda/src/main/java/io/serverlessworkflow/impl/executors/func/JavaTaskExecutorFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@
1919
import io.serverlessworkflow.api.types.TaskBase;
2020
import io.serverlessworkflow.api.types.Workflow;
2121
import io.serverlessworkflow.impl.WorkflowApplication;
22-
import io.serverlessworkflow.impl.WorkflowPosition;
22+
import io.serverlessworkflow.impl.WorkflowMutablePosition;
2323
import io.serverlessworkflow.impl.executors.DefaultTaskExecutorFactory;
2424
import io.serverlessworkflow.impl.executors.TaskExecutorBuilder;
2525
import io.serverlessworkflow.impl.resources.ResourceLoader;
2626

2727
public class JavaTaskExecutorFactory extends DefaultTaskExecutorFactory {
2828

2929
public TaskExecutorBuilder<? extends TaskBase> getTaskExecutor(
30-
WorkflowPosition position,
30+
WorkflowMutablePosition position,
3131
Task task,
3232
Workflow workflow,
3333
WorkflowApplication application,

impl/core/src/main/java/io/serverlessworkflow/impl/QueueWorkflowPosition.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import java.util.Deque;
2020
import java.util.stream.Collectors;
2121

22-
public class QueueWorkflowPosition implements WorkflowPosition {
22+
public class QueueWorkflowPosition implements WorkflowMutablePosition {
2323

2424
private Deque<Object> queue;
2525

@@ -36,13 +36,13 @@ public QueueWorkflowPosition copy() {
3636
}
3737

3838
@Override
39-
public WorkflowPosition addIndex(int index) {
39+
public WorkflowMutablePosition addIndex(int index) {
4040
queue.add(index);
4141
return this;
4242
}
4343

4444
@Override
45-
public WorkflowPosition addProperty(String prop) {
45+
public WorkflowMutablePosition addProperty(String prop) {
4646
queue.add(prop);
4747
return this;
4848
}
@@ -58,7 +58,7 @@ public String toString() {
5858
}
5959

6060
@Override
61-
public WorkflowPosition back() {
61+
public WorkflowMutablePosition back() {
6262
queue.removeLast();
6363
return this;
6464
}

impl/core/src/main/java/io/serverlessworkflow/impl/StringBufferWorkflowPosition.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package io.serverlessworkflow.impl;
1717

18-
public class StringBufferWorkflowPosition implements WorkflowPosition {
18+
public class StringBufferWorkflowPosition implements WorkflowMutablePosition {
1919

2020
private StringBuilder sb;
2121

@@ -32,13 +32,13 @@ public StringBufferWorkflowPosition copy() {
3232
}
3333

3434
@Override
35-
public WorkflowPosition addIndex(int index) {
35+
public WorkflowMutablePosition addIndex(int index) {
3636
sb.append('/').append(index);
3737
return this;
3838
}
3939

4040
@Override
41-
public WorkflowPosition addProperty(String prop) {
41+
public WorkflowMutablePosition addProperty(String prop) {
4242
sb.append('/').append(prop);
4343
return this;
4444
}
@@ -54,7 +54,7 @@ public String toString() {
5454
}
5555

5656
@Override
57-
public WorkflowPosition back() {
57+
public WorkflowMutablePosition back() {
5858
int indexOf = sb.lastIndexOf("/");
5959
if (indexOf != -1) {
6060
sb.substring(0, indexOf);

impl/core/src/main/java/io/serverlessworkflow/impl/TaskContext.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import java.util.Map;
2323
import java.util.Optional;
2424

25-
public class TaskContext {
25+
public class TaskContext implements TaskContextData {
2626

2727
private final WorkflowModel rawInput;
2828
private final TaskBase task;
@@ -81,14 +81,17 @@ public void input(WorkflowModel input) {
8181
this.output = input;
8282
}
8383

84+
@Override
8485
public WorkflowModel input() {
8586
return input;
8687
}
8788

89+
@Override
8890
public WorkflowModel rawInput() {
8991
return rawInput;
9092
}
9193

94+
@Override
9295
public TaskBase task() {
9396
return task;
9497
}
@@ -99,6 +102,7 @@ public TaskContext rawOutput(WorkflowModel output) {
99102
return this;
100103
}
101104

105+
@Override
102106
public WorkflowModel rawOutput() {
103107
return rawOutput;
104108
}
@@ -108,26 +112,32 @@ public TaskContext output(WorkflowModel output) {
108112
return this;
109113
}
110114

115+
@Override
111116
public WorkflowModel output() {
112117
return output;
113118
}
114119

120+
@Override
115121
public WorkflowPosition position() {
116122
return position;
117123
}
118124

125+
@Override
119126
public Map<String, Object> variables() {
120127
return contextVariables;
121128
}
122129

130+
@Override
123131
public Instant startedAt() {
124132
return startedAt;
125133
}
126134

135+
@Override
127136
public Optional<TaskContext> parent() {
128137
return parentContext;
129138
}
130139

140+
@Override
131141
public String taskName() {
132142
return taskName;
133143
}
@@ -137,6 +147,7 @@ public TaskContext completedAt(Instant instant) {
137147
return this;
138148
}
139149

150+
@Override
140151
public Instant completedAt() {
141152
return completedAt;
142153
}
@@ -149,4 +160,17 @@ public TaskContext transition(TransitionInfo transition) {
149160
this.transition = transition;
150161
return this;
151162
}
163+
164+
@Override
165+
public String toString() {
166+
return "TaskContext [position="
167+
+ position
168+
+ ", startedAt="
169+
+ startedAt
170+
+ ", taskName="
171+
+ taskName
172+
+ ", completedAt="
173+
+ completedAt
174+
+ "]";
175+
}
152176
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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;
17+
18+
import io.serverlessworkflow.api.types.TaskBase;
19+
import java.time.Instant;
20+
import java.util.Map;
21+
import java.util.Optional;
22+
23+
public interface TaskContextData {
24+
25+
WorkflowModel input();
26+
27+
WorkflowModel rawInput();
28+
29+
TaskBase task();
30+
31+
WorkflowModel rawOutput();
32+
33+
WorkflowModel output();
34+
35+
WorkflowPosition position();
36+
37+
Map<String, Object> variables();
38+
39+
Instant startedAt();
40+
41+
Optional<TaskContext> parent();
42+
43+
String taskName();
44+
45+
Instant completedAt();
46+
}

impl/core/src/main/java/io/serverlessworkflow/impl/WorkflowApplication.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,21 @@
2626
import io.serverlessworkflow.impl.executors.TaskExecutorFactory;
2727
import io.serverlessworkflow.impl.expressions.ExpressionFactory;
2828
import io.serverlessworkflow.impl.expressions.RuntimeDescriptor;
29+
import io.serverlessworkflow.impl.lifecycle.WorkflowExecutionListener;
2930
import io.serverlessworkflow.impl.resources.DefaultResourceLoaderFactory;
3031
import io.serverlessworkflow.impl.resources.ResourceLoaderFactory;
3132
import io.serverlessworkflow.impl.resources.StaticResource;
3233
import io.serverlessworkflow.impl.schema.SchemaValidator;
3334
import io.serverlessworkflow.impl.schema.SchemaValidatorFactory;
3435
import java.util.Collection;
3536
import java.util.Collections;
36-
import java.util.HashSet;
3737
import java.util.Map;
3838
import java.util.ServiceLoader;
39+
import java.util.ServiceLoader.Provider;
3940
import java.util.concurrent.ConcurrentHashMap;
4041
import java.util.concurrent.ExecutorService;
4142
import java.util.concurrent.Executors;
43+
import java.util.stream.Collectors;
4244

4345
public class WorkflowApplication implements AutoCloseable {
4446

@@ -127,7 +129,10 @@ public SchemaValidator getValidator(SchemaInline inline) {
127129

128130
private TaskExecutorFactory taskFactory;
129131
private ExpressionFactory exprFactory;
130-
private Collection<WorkflowExecutionListener> listeners;
132+
private Collection<WorkflowExecutionListener> listeners =
133+
ServiceLoader.load(WorkflowExecutionListener.class).stream()
134+
.map(Provider::get)
135+
.collect(Collectors.toList());
131136
private ResourceLoaderFactory resourceLoaderFactory = DefaultResourceLoaderFactory.get();
132137
private SchemaValidatorFactory schemaValidatorFactory;
133138
private WorkflowPositionFactory positionFactory = () -> new QueueWorkflowPosition();
@@ -141,9 +146,6 @@ public SchemaValidator getValidator(SchemaInline inline) {
141146
private Builder() {}
142147

143148
public Builder withListener(WorkflowExecutionListener listener) {
144-
if (listeners == null) {
145-
listeners = new HashSet<>();
146-
}
147149
listeners.add(listener);
148150
return this;
149151
}

impl/core/src/main/java/io/serverlessworkflow/impl/WorkflowContext.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,26 @@
1515
*/
1616
package io.serverlessworkflow.impl;
1717

18-
public class WorkflowContext {
18+
public class WorkflowContext implements WorkflowContextData {
1919
private final WorkflowDefinition definition;
20-
private final WorkflowInstance instance;
20+
private final WorkflowMutableInstance instance;
2121
private WorkflowModel context;
2222

23-
WorkflowContext(WorkflowDefinition definition, WorkflowInstance instance) {
23+
WorkflowContext(WorkflowDefinition definition, WorkflowMutableInstance instance) {
2424
this.definition = definition;
2525
this.instance = instance;
2626
}
2727

28-
public WorkflowInstance instance() {
28+
@Override
29+
public WorkflowInstanceData instanceData() {
2930
return instance;
3031
}
3132

33+
public WorkflowMutableInstance instance() {
34+
return instance;
35+
}
36+
37+
@Override
3238
public WorkflowModel context() {
3339
return context;
3440
}
@@ -37,7 +43,19 @@ public void context(WorkflowModel context) {
3743
this.context = context;
3844
}
3945

46+
@Override
4047
public WorkflowDefinition definition() {
4148
return definition;
4249
}
50+
51+
@Override
52+
public String toString() {
53+
return "WorkflowContext [definition="
54+
+ definition.workflow().getDocument().getName()
55+
+ ", instance="
56+
+ instance
57+
+ ", context="
58+
+ context
59+
+ "]";
60+
}
4361
}

impl/core/src/main/java/io/serverlessworkflow/impl/WorkflowExecutionListener.java renamed to impl/core/src/main/java/io/serverlessworkflow/impl/WorkflowContextData.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
*/
1616
package io.serverlessworkflow.impl;
1717

18-
import io.serverlessworkflow.api.types.TaskBase;
18+
public interface WorkflowContextData {
1919

20-
public interface WorkflowExecutionListener {
20+
WorkflowInstanceData instanceData();
2121

22-
void onTaskStarted(WorkflowPosition currentPos, TaskBase task);
22+
WorkflowModel context();
2323

24-
void onTaskEnded(WorkflowPosition currentPos, TaskBase task);
24+
WorkflowDefinitionData definition();
2525
}

0 commit comments

Comments
 (0)