@@ -166,18 +166,24 @@ import (
166
166
func initBpmnEngine () {
167
167
bpmnEngine = bpmn_engine.New (" Ordering-Microservice" )
168
168
process, _ = bpmnEngine.LoadFromBytes (OrderingItemsWorkflowBpmn)
169
- bpmnEngine.AddTaskHandler (" validate-order" , businessActionHandler )
170
- bpmnEngine.AddTaskHandler (" send-bill" , businessActionHandler )
171
- bpmnEngine.AddTaskHandler (" send-friendly-reminder" , businessActionHandler )
172
- bpmnEngine.AddTaskHandler (" update-accounting" , businessActionHandler )
173
- bpmnEngine.AddTaskHandler (" package-and-deliver" , businessActionHandler )
174
- bpmnEngine.AddTaskHandler (" send-cancellation" , businessActionHandler )
169
+ bpmnEngine.AddTaskHandler (" validate-order" , printHandler )
170
+ bpmnEngine.AddTaskHandler (" send-bill" , printHandler )
171
+ bpmnEngine.AddTaskHandler (" send-friendly-reminder" , printHandler )
172
+ bpmnEngine.AddTaskHandler (" update-accounting" , updateAccountingHandler )
173
+ bpmnEngine.AddTaskHandler (" package-and-deliver" , printHandler )
174
+ bpmnEngine.AddTaskHandler (" send-cancellation" , printHandler )
175
175
}
176
176
177
- func businessActionHandler (job bpmn_engine .ActivatedJob ) {
177
+ func printHandler (job bpmn_engine .ActivatedJob ) {
178
178
// do important stuff here
179
- msg := fmt.Sprintf (" %s >>> Executing job '%s " , time.Now (), job.ElementId )
180
- println (msg)
179
+ println (fmt.Sprintf (" %s >>> Executing job '%s " , time.Now (), job.ElementId ))
180
+ job.Complete ()
181
+ }
182
+
183
+ func updateAccountingHandler (job bpmn_engine .ActivatedJob ) {
184
+ println (fmt.Sprintf (" %s >>> Executing job '%s " , time.Now (), job.ElementId ))
185
+ println (fmt.Sprintf (" %s >>> update ledger revenue account with amount=%s " , time.Now (), job.GetVariable (" amount" )))
186
+ job.Complete ()
181
187
}
182
188
```
183
189
<!-- MARKDOWN-AUTO-DOCS:END -->
@@ -215,6 +221,8 @@ func showOrderStatus(writer http.ResponseWriter, request *http.Request) {
215
221
orderId , _ := strconv.ParseInt (orderIdStr, 10 , 64 )
216
222
instance := bpmnEngine.FindProcessInstanceById (orderId)
217
223
if instance != nil {
224
+ // we re-use this GET request to ensure we catch up the timers - ideally the service uses internal timers instead
225
+ bpmnEngine.RunOrContinueInstance (instance.GetInstanceKey ())
218
226
bytes , _ := prepareJsonResponse (orderIdStr, instance.GetState (), instance.GetCreatedAt ())
219
227
writer.Header ().Set (" Content-Type" , " application/json" )
220
228
writer.Write (bytes)
@@ -235,10 +243,25 @@ package main
235
243
import (
236
244
_ " embed"
237
245
" net/http"
246
+ " strconv"
238
247
)
239
248
240
249
func handleReceivePayment (writer http .ResponseWriter , request *http .Request ) {
241
- writer.Header ().Set (" Content-Type" , " application/json" )
250
+ orderIdStr := request.FormValue (" orderId" )
251
+ amount := request.FormValue (" amount" )
252
+ if len (orderIdStr) > 0 && len (amount) > 0 {
253
+ orderId , _ := strconv.ParseInt (orderIdStr, 10 , 64 )
254
+ processInstance := bpmnEngine.FindProcessInstanceById (orderId)
255
+ if processInstance != nil {
256
+ processInstance.SetVariable (" amount" , amount)
257
+ bpmnEngine.PublishEventForInstance (processInstance.GetInstanceKey (), " payment-received" )
258
+ bpmnEngine.RunOrContinueInstance (processInstance.GetInstanceKey ())
259
+ http.Redirect (writer, request, " /" , http.StatusFound )
260
+ return
261
+ }
262
+ }
263
+ writer.WriteHeader (400 )
264
+ writer.Write ([]byte (" Bad request: the request must contain form data with 'orderId' and 'amount', and the order must exist" ))
242
265
}
243
266
```
244
267
<!-- MARKDOWN-AUTO-DOCS:END -->
0 commit comments