|
29 | 29 | import static io.serverlessworkflow.impl.LifecycleEvents.WORKFLOW_STARTED; |
30 | 30 | import static io.serverlessworkflow.impl.LifecycleEvents.WORKFLOW_STATUS_CHANGED; |
31 | 31 | import static io.serverlessworkflow.impl.LifecycleEvents.WORKFLOW_SUSPENDED; |
32 | | -import static io.serverlessworkflow.impl.WorkflowError.error; |
33 | | -import static io.serverlessworkflow.impl.lifecycle.ce.WorkflowDefinitionCEData.ref; |
34 | 32 |
|
35 | 33 | import io.cloudevents.CloudEvent; |
36 | 34 | import io.cloudevents.CloudEventData; |
37 | 35 | import io.cloudevents.core.builder.CloudEventBuilder; |
38 | 36 | import io.cloudevents.core.data.PojoCloudEventData; |
39 | 37 | import io.cloudevents.core.data.PojoCloudEventData.ToBytes; |
40 | 38 | import io.serverlessworkflow.impl.WorkflowApplication; |
41 | | -import io.serverlessworkflow.impl.WorkflowModel; |
42 | 39 | import io.serverlessworkflow.impl.events.CloudEventUtils; |
43 | 40 | import io.serverlessworkflow.impl.lifecycle.TaskCancelledEvent; |
44 | 41 | import io.serverlessworkflow.impl.lifecycle.TaskCompletedEvent; |
45 | | -import io.serverlessworkflow.impl.lifecycle.TaskEvent; |
46 | 42 | import io.serverlessworkflow.impl.lifecycle.TaskFailedEvent; |
47 | 43 | import io.serverlessworkflow.impl.lifecycle.TaskResumedEvent; |
48 | 44 | import io.serverlessworkflow.impl.lifecycle.TaskRetriedEvent; |
@@ -82,202 +78,177 @@ public static Collection<String> getLifeCycleTypes() { |
82 | 78 | WORKFLOW_STATUS_CHANGED); |
83 | 79 | } |
84 | 80 |
|
| 81 | + private WorkflowLifeCycleCloudEventFactory lifeCycleFactory(WorkflowEvent ev) { |
| 82 | + return ev.workflowContext().definition().application().lifeCycleCloudEventFactory(); |
| 83 | + } |
| 84 | + |
85 | 85 | @Override |
86 | 86 | public void onTaskStarted(TaskStartedEvent event) { |
| 87 | + WorkflowLifeCycleCloudEventFactory factory = lifeCycleFactory(event); |
87 | 88 | publish( |
88 | 89 | event, |
89 | 90 | ev -> |
90 | | - builder() |
91 | | - .withData( |
92 | | - cloudEventData( |
93 | | - new TaskStartedCEData(id(ev), pos(ev), ref(ev), ev.eventDate()), |
94 | | - this::convert)) |
95 | | - .withType(TASK_STARTED) |
96 | | - .build()); |
| 91 | + factory.build( |
| 92 | + builder() |
| 93 | + .withData(cloudEventData(factory.build(event), this::convert)) |
| 94 | + .withType(TASK_STARTED))); |
97 | 95 | } |
98 | 96 |
|
99 | 97 | @Override |
100 | 98 | public void onTaskRetried(TaskRetriedEvent event) { |
| 99 | + WorkflowLifeCycleCloudEventFactory factory = lifeCycleFactory(event); |
101 | 100 | publish( |
102 | 101 | event, |
103 | 102 | ev -> |
104 | | - builder() |
105 | | - .withData( |
106 | | - cloudEventData( |
107 | | - new TaskRetriedCEData(id(ev), pos(ev), ref(ev), ev.eventDate()), |
108 | | - this::convert)) |
109 | | - .withType(TASK_STARTED) |
110 | | - .build()); |
| 103 | + factory.build( |
| 104 | + builder() |
| 105 | + .withData(cloudEventData(factory.build(event), this::convert)) |
| 106 | + .withType(TASK_RETRIED))); |
111 | 107 | } |
112 | 108 |
|
113 | 109 | @Override |
114 | 110 | public void onTaskCompleted(TaskCompletedEvent event) { |
| 111 | + WorkflowLifeCycleCloudEventFactory factory = lifeCycleFactory(event); |
115 | 112 | publish( |
116 | 113 | event, |
117 | 114 | ev -> |
118 | | - builder() |
119 | | - .withData( |
120 | | - cloudEventData( |
121 | | - new TaskCompletedCEData( |
122 | | - id(ev), pos(ev), ref(ev), ev.eventDate(), output(ev)), |
123 | | - this::convert)) |
124 | | - .withType(TASK_COMPLETED) |
125 | | - .build()); |
| 115 | + factory.build( |
| 116 | + builder() |
| 117 | + .withData(cloudEventData(factory.build(event), this::convert)) |
| 118 | + .withType(TASK_COMPLETED))); |
126 | 119 | } |
127 | 120 |
|
128 | 121 | @Override |
129 | 122 | public void onTaskSuspended(TaskSuspendedEvent event) { |
| 123 | + WorkflowLifeCycleCloudEventFactory factory = lifeCycleFactory(event); |
130 | 124 | publish( |
131 | 125 | event, |
132 | 126 | ev -> |
133 | | - builder() |
134 | | - .withData( |
135 | | - cloudEventData( |
136 | | - new TaskSuspendedCEData(id(ev), pos(ev), ref(ev), ev.eventDate()), |
137 | | - this::convert)) |
138 | | - .withType(TASK_SUSPENDED) |
139 | | - .build()); |
| 127 | + factory.build( |
| 128 | + builder() |
| 129 | + .withData(cloudEventData(factory.build(event), this::convert)) |
| 130 | + .withType(TASK_SUSPENDED))); |
140 | 131 | } |
141 | 132 |
|
142 | 133 | @Override |
143 | 134 | public void onTaskResumed(TaskResumedEvent event) { |
| 135 | + WorkflowLifeCycleCloudEventFactory factory = lifeCycleFactory(event); |
144 | 136 | publish( |
145 | 137 | event, |
146 | 138 | ev -> |
147 | | - builder() |
148 | | - .withData( |
149 | | - cloudEventData( |
150 | | - new TaskResumedCEData(id(ev), pos(ev), ref(ev), ev.eventDate()), |
151 | | - this::convert)) |
152 | | - .withType(TASK_RESUMED) |
153 | | - .build()); |
| 139 | + factory.build( |
| 140 | + builder() |
| 141 | + .withData(cloudEventData(factory.build(event), this::convert)) |
| 142 | + .withType(TASK_RESUMED))); |
154 | 143 | } |
155 | 144 |
|
156 | 145 | @Override |
157 | 146 | public void onTaskCancelled(TaskCancelledEvent event) { |
| 147 | + WorkflowLifeCycleCloudEventFactory factory = lifeCycleFactory(event); |
158 | 148 | publish( |
159 | 149 | event, |
160 | 150 | ev -> |
161 | | - builder() |
162 | | - .withData( |
163 | | - cloudEventData( |
164 | | - new TaskCancelledCEData(id(ev), pos(ev), ref(ev), ev.eventDate()), |
165 | | - this::convert)) |
166 | | - .withType(TASK_CANCELLED) |
167 | | - .build()); |
| 151 | + factory.build( |
| 152 | + builder() |
| 153 | + .withData(cloudEventData(factory.build(event), this::convert)) |
| 154 | + .withType(TASK_CANCELLED))); |
168 | 155 | } |
169 | 156 |
|
170 | 157 | @Override |
171 | 158 | public void onTaskFailed(TaskFailedEvent event) { |
| 159 | + WorkflowLifeCycleCloudEventFactory factory = lifeCycleFactory(event); |
172 | 160 | publish( |
173 | 161 | event, |
174 | 162 | ev -> |
175 | | - builder() |
176 | | - .withData( |
177 | | - cloudEventData( |
178 | | - new TaskFailedCEData(id(ev), pos(ev), ref(ev), ev.eventDate(), error(ev)), |
179 | | - this::convert)) |
180 | | - .withType(TASK_FAULTED) |
181 | | - .build()); |
| 163 | + factory.build( |
| 164 | + builder() |
| 165 | + .withData(cloudEventData(factory.build(event), this::convert)) |
| 166 | + .withType(TASK_FAULTED))); |
182 | 167 | } |
183 | 168 |
|
184 | 169 | @Override |
185 | 170 | public void onWorkflowStarted(WorkflowStartedEvent event) { |
| 171 | + WorkflowLifeCycleCloudEventFactory factory = lifeCycleFactory(event); |
186 | 172 | publish( |
187 | 173 | event, |
188 | 174 | ev -> |
189 | | - builder() |
190 | | - .withData( |
191 | | - cloudEventData( |
192 | | - new WorkflowStartedCEData(id(ev), ref(ev), ev.eventDate()), this::convert)) |
193 | | - .withType(WORKFLOW_STARTED) |
194 | | - .build()); |
| 175 | + factory.build( |
| 176 | + builder() |
| 177 | + .withData(cloudEventData(factory.build(event), this::convert)) |
| 178 | + .withType(WORKFLOW_STARTED))); |
195 | 179 | } |
196 | 180 |
|
197 | 181 | @Override |
198 | 182 | public void onWorkflowSuspended(WorkflowSuspendedEvent event) { |
| 183 | + WorkflowLifeCycleCloudEventFactory factory = lifeCycleFactory(event); |
199 | 184 | publish( |
200 | 185 | event, |
201 | 186 | ev -> |
202 | | - builder() |
203 | | - .withData( |
204 | | - cloudEventData( |
205 | | - new WorkflowSuspendedCEData(id(ev), ref(ev), ev.eventDate()), |
206 | | - this::convert)) |
207 | | - .withType(WORKFLOW_SUSPENDED) |
208 | | - .build()); |
| 187 | + factory.build( |
| 188 | + builder() |
| 189 | + .withData(cloudEventData(factory.build(event), this::convert)) |
| 190 | + .withType(WORKFLOW_SUSPENDED))); |
209 | 191 | } |
210 | 192 |
|
211 | 193 | @Override |
212 | 194 | public void onWorkflowCancelled(WorkflowCancelledEvent event) { |
| 195 | + WorkflowLifeCycleCloudEventFactory factory = lifeCycleFactory(event); |
213 | 196 | publish( |
214 | 197 | event, |
215 | 198 | ev -> |
216 | | - builder() |
217 | | - .withData( |
218 | | - cloudEventData( |
219 | | - new WorkflowCancelledCEData(id(ev), ref(ev), ev.eventDate()), |
220 | | - this::convert)) |
221 | | - .withType(WORKFLOW_CANCELLED) |
222 | | - .build()); |
| 199 | + factory.build( |
| 200 | + builder() |
| 201 | + .withData(cloudEventData(factory.build(event), this::convert)) |
| 202 | + .withType(WORKFLOW_CANCELLED))); |
223 | 203 | } |
224 | 204 |
|
225 | 205 | @Override |
226 | 206 | public void onWorkflowResumed(WorkflowResumedEvent event) { |
| 207 | + WorkflowLifeCycleCloudEventFactory factory = lifeCycleFactory(event); |
227 | 208 | publish( |
228 | 209 | event, |
229 | 210 | ev -> |
230 | | - builder() |
231 | | - .withData( |
232 | | - cloudEventData( |
233 | | - new WorkflowResumedCEData(id(ev), ref(ev), ev.eventDate()), this::convert)) |
234 | | - .withType(WORKFLOW_RESUMED) |
235 | | - .build()); |
| 211 | + factory.build( |
| 212 | + builder() |
| 213 | + .withData(cloudEventData(factory.build(event), this::convert)) |
| 214 | + .withType(WORKFLOW_RESUMED))); |
236 | 215 | } |
237 | 216 |
|
238 | 217 | @Override |
239 | 218 | public void onWorkflowCompleted(WorkflowCompletedEvent event) { |
| 219 | + WorkflowLifeCycleCloudEventFactory factory = lifeCycleFactory(event); |
240 | 220 | publish( |
241 | 221 | event, |
242 | 222 | ev -> |
243 | | - builder() |
244 | | - .withData( |
245 | | - cloudEventData( |
246 | | - new WorkflowCompletedCEData( |
247 | | - id(ev), ref(ev), ev.eventDate(), from(event.output())), |
248 | | - this::convert)) |
249 | | - .withType(WORKFLOW_COMPLETED) |
250 | | - .build()); |
| 223 | + factory.build( |
| 224 | + builder() |
| 225 | + .withData(cloudEventData(factory.build(event), this::convert)) |
| 226 | + .withType(WORKFLOW_COMPLETED))); |
251 | 227 | } |
252 | 228 |
|
253 | 229 | @Override |
254 | 230 | public void onWorkflowFailed(WorkflowFailedEvent event) { |
| 231 | + WorkflowLifeCycleCloudEventFactory factory = lifeCycleFactory(event); |
255 | 232 | publish( |
256 | 233 | event, |
257 | 234 | ev -> |
258 | | - builder() |
259 | | - .withData( |
260 | | - cloudEventData( |
261 | | - new WorkflowFailedCEData(id(ev), ref(ev), ev.eventDate(), error(ev)), |
262 | | - this::convert)) |
263 | | - .withType(WORKFLOW_FAULTED) |
264 | | - .build()); |
| 235 | + factory.build( |
| 236 | + builder() |
| 237 | + .withData(cloudEventData(factory.build(event), this::convert)) |
| 238 | + .withType(WORKFLOW_FAULTED))); |
265 | 239 | } |
266 | 240 |
|
267 | 241 | @Override |
268 | 242 | public void onWorkflowStatusChanged(WorkflowStatusEvent event) { |
269 | 243 | if (appl(event).isStatusChangePublishingEnabled()) { |
| 244 | + WorkflowLifeCycleCloudEventFactory factory = lifeCycleFactory(event); |
270 | 245 | publish( |
271 | 246 | event, |
272 | 247 | ev -> |
273 | | - builder() |
274 | | - .withData( |
275 | | - cloudEventData( |
276 | | - new WorkflowStatusCEDataEvent( |
277 | | - id(ev), ref(ev), ev.eventDate(), ev.status().toString()), |
278 | | - this::convert)) |
279 | | - .withType(WORKFLOW_STATUS_CHANGED) |
280 | | - .build()); |
| 248 | + factory.build( |
| 249 | + builder() |
| 250 | + .withData(cloudEventData(factory.build(event), this::convert)) |
| 251 | + .withType(WORKFLOW_STATUS_CHANGED))); |
281 | 252 | } |
282 | 253 | } |
283 | 254 |
|
@@ -367,20 +338,4 @@ private static CloudEventBuilder builder() { |
367 | 338 | private static WorkflowApplication appl(WorkflowEvent ev) { |
368 | 339 | return ev.workflowContext().definition().application(); |
369 | 340 | } |
370 | | - |
371 | | - private static String id(WorkflowEvent ev) { |
372 | | - return ev.workflowContext().instanceData().id(); |
373 | | - } |
374 | | - |
375 | | - private static String pos(TaskEvent ev) { |
376 | | - return ev.taskContext().position().jsonPointer(); |
377 | | - } |
378 | | - |
379 | | - private static Object output(TaskEvent ev) { |
380 | | - return from(ev.taskContext().output()); |
381 | | - } |
382 | | - |
383 | | - private static Object from(WorkflowModel model) { |
384 | | - return model.asJavaObject(); |
385 | | - } |
386 | 341 | } |
0 commit comments