|
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; |
@@ -82,202 +80,177 @@ public static Collection<String> getLifeCycleTypes() { |
82 | 80 | WORKFLOW_STATUS_CHANGED); |
83 | 81 | } |
84 | 82 |
|
| 83 | + private WorkflowLifeCycleCloudEventFactory lifeCycleFactory(WorkflowEvent ev) { |
| 84 | + return ev.workflowContext().definition().application().lifeCycleCloudEventFactory(); |
| 85 | + } |
| 86 | + |
85 | 87 | @Override |
86 | 88 | public void onTaskStarted(TaskStartedEvent event) { |
| 89 | + WorkflowLifeCycleCloudEventFactory factory = lifeCycleFactory(event); |
87 | 90 | publish( |
88 | 91 | event, |
89 | 92 | 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()); |
| 93 | + factory.build( |
| 94 | + builder() |
| 95 | + .withData(cloudEventData(factory.build(event), this::convert)) |
| 96 | + .withType(TASK_STARTED))); |
97 | 97 | } |
98 | 98 |
|
99 | 99 | @Override |
100 | 100 | public void onTaskRetried(TaskRetriedEvent event) { |
| 101 | + WorkflowLifeCycleCloudEventFactory factory = lifeCycleFactory(event); |
101 | 102 | publish( |
102 | 103 | event, |
103 | 104 | 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()); |
| 105 | + factory.build( |
| 106 | + builder() |
| 107 | + .withData(cloudEventData(factory.build(event), this::convert)) |
| 108 | + .withType(TASK_RETRIED))); |
111 | 109 | } |
112 | 110 |
|
113 | 111 | @Override |
114 | 112 | public void onTaskCompleted(TaskCompletedEvent event) { |
| 113 | + WorkflowLifeCycleCloudEventFactory factory = lifeCycleFactory(event); |
115 | 114 | publish( |
116 | 115 | event, |
117 | 116 | 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()); |
| 117 | + factory.build( |
| 118 | + builder() |
| 119 | + .withData(cloudEventData(factory.build(event), this::convert)) |
| 120 | + .withType(TASK_COMPLETED))); |
126 | 121 | } |
127 | 122 |
|
128 | 123 | @Override |
129 | 124 | public void onTaskSuspended(TaskSuspendedEvent event) { |
| 125 | + WorkflowLifeCycleCloudEventFactory factory = lifeCycleFactory(event); |
130 | 126 | publish( |
131 | 127 | event, |
132 | 128 | 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()); |
| 129 | + factory.build( |
| 130 | + builder() |
| 131 | + .withData(cloudEventData(factory.build(event), this::convert)) |
| 132 | + .withType(TASK_SUSPENDED))); |
140 | 133 | } |
141 | 134 |
|
142 | 135 | @Override |
143 | 136 | public void onTaskResumed(TaskResumedEvent event) { |
| 137 | + WorkflowLifeCycleCloudEventFactory factory = lifeCycleFactory(event); |
144 | 138 | publish( |
145 | 139 | event, |
146 | 140 | 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()); |
| 141 | + factory.build( |
| 142 | + builder() |
| 143 | + .withData(cloudEventData(factory.build(event), this::convert)) |
| 144 | + .withType(TASK_RESUMED))); |
154 | 145 | } |
155 | 146 |
|
156 | 147 | @Override |
157 | 148 | public void onTaskCancelled(TaskCancelledEvent event) { |
| 149 | + WorkflowLifeCycleCloudEventFactory factory = lifeCycleFactory(event); |
158 | 150 | publish( |
159 | 151 | event, |
160 | 152 | 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()); |
| 153 | + factory.build( |
| 154 | + builder() |
| 155 | + .withData(cloudEventData(factory.build(event), this::convert)) |
| 156 | + .withType(TASK_CANCELLED))); |
168 | 157 | } |
169 | 158 |
|
170 | 159 | @Override |
171 | 160 | public void onTaskFailed(TaskFailedEvent event) { |
| 161 | + WorkflowLifeCycleCloudEventFactory factory = lifeCycleFactory(event); |
172 | 162 | publish( |
173 | 163 | event, |
174 | 164 | 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()); |
| 165 | + factory.build( |
| 166 | + builder() |
| 167 | + .withData(cloudEventData(factory.build(event), this::convert)) |
| 168 | + .withType(TASK_FAULTED))); |
182 | 169 | } |
183 | 170 |
|
184 | 171 | @Override |
185 | 172 | public void onWorkflowStarted(WorkflowStartedEvent event) { |
| 173 | + WorkflowLifeCycleCloudEventFactory factory = lifeCycleFactory(event); |
186 | 174 | publish( |
187 | 175 | event, |
188 | 176 | ev -> |
189 | | - builder() |
190 | | - .withData( |
191 | | - cloudEventData( |
192 | | - new WorkflowStartedCEData(id(ev), ref(ev), ev.eventDate()), this::convert)) |
193 | | - .withType(WORKFLOW_STARTED) |
194 | | - .build()); |
| 177 | + factory.build( |
| 178 | + builder() |
| 179 | + .withData(cloudEventData(factory.build(event), this::convert)) |
| 180 | + .withType(WORKFLOW_STARTED))); |
195 | 181 | } |
196 | 182 |
|
197 | 183 | @Override |
198 | 184 | public void onWorkflowSuspended(WorkflowSuspendedEvent event) { |
| 185 | + WorkflowLifeCycleCloudEventFactory factory = lifeCycleFactory(event); |
199 | 186 | publish( |
200 | 187 | event, |
201 | 188 | 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()); |
| 189 | + factory.build( |
| 190 | + builder() |
| 191 | + .withData(cloudEventData(factory.build(event), this::convert)) |
| 192 | + .withType(WORKFLOW_SUSPENDED))); |
209 | 193 | } |
210 | 194 |
|
211 | 195 | @Override |
212 | 196 | public void onWorkflowCancelled(WorkflowCancelledEvent event) { |
| 197 | + WorkflowLifeCycleCloudEventFactory factory = lifeCycleFactory(event); |
213 | 198 | publish( |
214 | 199 | event, |
215 | 200 | 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()); |
| 201 | + factory.build( |
| 202 | + builder() |
| 203 | + .withData(cloudEventData(factory.build(event), this::convert)) |
| 204 | + .withType(WORKFLOW_CANCELLED))); |
223 | 205 | } |
224 | 206 |
|
225 | 207 | @Override |
226 | 208 | public void onWorkflowResumed(WorkflowResumedEvent event) { |
| 209 | + WorkflowLifeCycleCloudEventFactory factory = lifeCycleFactory(event); |
227 | 210 | publish( |
228 | 211 | event, |
229 | 212 | ev -> |
230 | | - builder() |
231 | | - .withData( |
232 | | - cloudEventData( |
233 | | - new WorkflowResumedCEData(id(ev), ref(ev), ev.eventDate()), this::convert)) |
234 | | - .withType(WORKFLOW_RESUMED) |
235 | | - .build()); |
| 213 | + factory.build( |
| 214 | + builder() |
| 215 | + .withData(cloudEventData(factory.build(event), this::convert)) |
| 216 | + .withType(WORKFLOW_RESUMED))); |
236 | 217 | } |
237 | 218 |
|
238 | 219 | @Override |
239 | 220 | public void onWorkflowCompleted(WorkflowCompletedEvent event) { |
| 221 | + WorkflowLifeCycleCloudEventFactory factory = lifeCycleFactory(event); |
240 | 222 | publish( |
241 | 223 | event, |
242 | 224 | 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()); |
| 225 | + factory.build( |
| 226 | + builder() |
| 227 | + .withData(cloudEventData(factory.build(event), this::convert)) |
| 228 | + .withType(WORKFLOW_COMPLETED))); |
251 | 229 | } |
252 | 230 |
|
253 | 231 | @Override |
254 | 232 | public void onWorkflowFailed(WorkflowFailedEvent event) { |
| 233 | + WorkflowLifeCycleCloudEventFactory factory = lifeCycleFactory(event); |
255 | 234 | publish( |
256 | 235 | event, |
257 | 236 | 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()); |
| 237 | + factory.build( |
| 238 | + builder() |
| 239 | + .withData(cloudEventData(factory.build(event), this::convert)) |
| 240 | + .withType(WORKFLOW_FAULTED))); |
265 | 241 | } |
266 | 242 |
|
267 | 243 | @Override |
268 | 244 | public void onWorkflowStatusChanged(WorkflowStatusEvent event) { |
269 | 245 | if (appl(event).isStatusChangePublishingEnabled()) { |
| 246 | + WorkflowLifeCycleCloudEventFactory factory = lifeCycleFactory(event); |
270 | 247 | publish( |
271 | 248 | event, |
272 | 249 | 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()); |
| 250 | + factory.build( |
| 251 | + builder() |
| 252 | + .withData(cloudEventData(factory.build(event), this::convert)) |
| 253 | + .withType(WORKFLOW_STATUS_CHANGED))); |
281 | 254 | } |
282 | 255 | } |
283 | 256 |
|
@@ -368,14 +341,6 @@ private static WorkflowApplication appl(WorkflowEvent ev) { |
368 | 341 | return ev.workflowContext().definition().application(); |
369 | 342 | } |
370 | 343 |
|
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 | 344 | private static Object output(TaskEvent ev) { |
380 | 345 | return from(ev.taskContext().output()); |
381 | 346 | } |
|
0 commit comments