mirror of https://github.com/dapr/quickstarts.git
update Go Workflow to use WaitForCompletion
Signed-off-by: Marc Duiker <marcduiker@users.noreply.github.com>
This commit is contained in:
parent
5c04837c8b
commit
55239f059a
|
|
@ -225,11 +225,11 @@ docker run -d -p 9411:9411 openzipkin/zipkin
|
|||
When you ran `dapr run -f .`:
|
||||
|
||||
1. An OrderPayload is made containing one car.
|
||||
2. A unique order ID for the workflow is generated (in the above example, `898fd553`) and the workflow is scheduled.
|
||||
2. A unique order ID for the workflow is generated (in the above example, `571a6e25`) and the workflow is scheduled.
|
||||
3. The `NotifyActivity` workflow activity sends a notification saying an order for one car has been received.
|
||||
4. The `VerifyInventoryActivity` workflow activity checks the inventory data, determines if you can supply the ordered item, and responds with the number of cars in stock. The inventory is sufficient so the workflow continues.
|
||||
5. The total cost of the order is 5000, so the workflow will not call the `RequestApprovalActivity` activity.
|
||||
6. The `ProcessPaymentActivity` workflow activity begins processing payment for order `898fd553` and confirms if successful.
|
||||
6. The `ProcessPaymentActivity` workflow activity begins processing payment for order `571a6e25` and confirms if successful.
|
||||
7. The `UpdateInventoryActivity` workflow activity updates the inventory with the current available cars after the order has been processed.
|
||||
8. The `NotifyActivity` workflow activity sends a notification saying that order `898fd553` has completed.
|
||||
8. The `NotifyActivity` workflow activity sends a notification saying that order `571a6e25` has completed.
|
||||
9. The workflow terminates as completed and the OrderResult is set to processed.
|
||||
|
|
|
|||
|
|
@ -13,10 +13,10 @@ activities are as follows:
|
|||
|
||||
- NotifyActivity: This activity utilizes a logger to print out messages throughout the workflow. These messages notify the user when there is insufficient
|
||||
§inventory, their payment couldn't be processed, and more.
|
||||
- ProcessPaymentActivity: This activity is responsible for processing and authorizing the payment.
|
||||
- VerifyInventoryActivity: This activity checks the state store to ensure that there is enough inventory present for purchase.
|
||||
- RequestApprovalActivity: This activity seeks approval from a manager, if payment is greater than 5000 USD.
|
||||
- ProcessPaymentActivity: This activity is responsible for processing and authorizing the payment.
|
||||
- UpdateInventoryActivity: This activity removes the requested items from the state store and updates the store with the new remaining inventory value.
|
||||
- RequestApprovalActivity: This activity seeks approval from Manager, if payment is greater than 50000 USD.
|
||||
|
||||
### Run the order processor workflow
|
||||
|
||||
|
|
@ -25,12 +25,10 @@ activities are as follows:
|
|||
<!-- STEP
|
||||
name: Running this example
|
||||
expected_stdout_lines:
|
||||
- "for 10 cars - $150000"
|
||||
- "There are 100 cars available for purchase"
|
||||
- "Requesting approval for payment of 150000USD for 10 cars"
|
||||
- "has been approved!"
|
||||
- "There are now 90 cars left in stock"
|
||||
- "Workflow completed - result: COMPLETED"
|
||||
- "for 1 cars - $5000"
|
||||
- "There are 10 cars available for purchase"
|
||||
- "There are now 9 cars left in stock"
|
||||
- "workflow status: COMPLETED"
|
||||
output_match_mode: substring
|
||||
background: false
|
||||
timeout_seconds: 120
|
||||
|
|
@ -47,23 +45,22 @@ dapr run -f .
|
|||
```
|
||||
== APP - order-processor == *** Welcome to the Dapr Workflow console app sample!
|
||||
== APP - order-processor == *** Using this app, you can place orders that start workflows.
|
||||
== APP - order-processor == dapr client initializing for: 127.0.0.1:50056
|
||||
== APP - order-processor == dapr client initializing for: 127.0.0.1:46533
|
||||
== APP - order-processor == INFO: 2025/02/13 13:18:33 connecting work item listener stream
|
||||
== APP - order-processor == 2025/02/13 13:18:33 work item listener started
|
||||
== APP - order-processor == INFO: 2025/02/13 13:18:33 starting background processor
|
||||
== APP - order-processor == adding base stock item: paperclip
|
||||
== APP - order-processor == 2024/02/01 12:59:52 work item listener started
|
||||
== APP - order-processor == INFO: 2024/02/01 12:59:52 starting background processor
|
||||
== APP - order-processor == adding base stock item: cars
|
||||
== APP - order-processor == adding base stock item: computers
|
||||
== APP - order-processor == ==========Begin the purchase of item:==========
|
||||
== APP - order-processor == NotifyActivity: Received order 48ee83b7-5d80-48d5-97f9-6b372f5480a5 for 10 cars - $150000
|
||||
== APP - order-processor == VerifyInventoryActivity: Verifying inventory for order 48ee83b7-5d80-48d5-97f9-6b372f5480a5 of 10 cars
|
||||
== APP - order-processor == VerifyInventoryActivity: There are 100 cars available for purchase
|
||||
== APP - order-processor == RequestApprovalActivity: Requesting approval for payment of 150000USD for 10 cars
|
||||
== APP - order-processor == NotifyActivity: Payment for order 48ee83b7-5d80-48d5-97f9-6b372f5480a5 has been approved!
|
||||
== APP - order-processor == ProcessPaymentActivity: 48ee83b7-5d80-48d5-97f9-6b372f5480a5 for 10 - cars (150000USD)
|
||||
== APP - order-processor == UpdateInventoryActivity: Checking Inventory for order 48ee83b7-5d80-48d5-97f9-6b372f5480a5 for 10 * cars
|
||||
== APP - order-processor == UpdateInventoryActivity: There are now 90 cars left in stock
|
||||
== APP - order-processor == NotifyActivity: Order 48ee83b7-5d80-48d5-97f9-6b372f5480a5 has completed!
|
||||
== APP - order-processor == Workflow completed - result: COMPLETED
|
||||
== APP - order-processor == NotifyActivity: Received order b4cb2687-1af0-4f8d-9659-eb6389c07ade for 1 cars - $5000
|
||||
== APP - order-processor == VerifyInventoryActivity: Verifying inventory for order b4cb2687-1af0-4f8d-9659-eb6389c07ade of 1 cars
|
||||
== APP - order-processor == VerifyInventoryActivity: There are 10 cars available for purchase
|
||||
== APP - order-processor == ProcessPaymentActivity: b4cb2687-1af0-4f8d-9659-eb6389c07ade for 1 - cars (5000USD)
|
||||
== APP - order-processor == UpdateInventoryActivity: Checking Inventory for order b4cb2687-1af0-4f8d-9659-eb6389c07ade for 1 * cars
|
||||
== APP - order-processor == UpdateInventoryActivity: There are now 9 cars left in stock
|
||||
== APP - order-processor == NotifyActivity: Order b4cb2687-1af0-4f8d-9659-eb6389c07ade has completed!
|
||||
== APP - order-processor == workflow status: COMPLETED
|
||||
== APP - order-processor == Purchase of item is complete
|
||||
```
|
||||
|
||||
|
|
@ -97,18 +94,16 @@ launched on running `dapr init`.
|
|||
|
||||
When you ran the above comands:
|
||||
|
||||
1. First the "user" inputs an order for 10 cars into the concole app.
|
||||
2. A unique order ID for the workflow is generated (in the above example, `b903d749cd814e099f06ebf4a56a2f90`) and the workflow is scheduled.
|
||||
1. An OrderPayload is made containing one car.
|
||||
2. A unique order ID for the workflow is generated (in the above example, `b4cb2687-1af0-4f8d-9659-eb6389c07ade`) and the workflow is scheduled.
|
||||
3. The `NotifyActivity` workflow activity sends a notification saying an order for 10 cars has been received.
|
||||
4. The `VerifyInventoryActivity` workflow activity checks the inventory data, determines if you can supply the ordered item, and responds with the number of cars
|
||||
in stock.
|
||||
5. The `RequestApprovalActivity` workflow activity is triggered due to buisness logic for orders exceeding $50k and user is prompted to manually approve the
|
||||
purchase before continuing the order.
|
||||
6. The workflow starts and notifies you of its status.
|
||||
7. The `ProcessPaymentActivity` workflow activity begins processing payment for order `b903d749cd814e099f06ebf4a56a2f90` and confirms if successful.
|
||||
8. The `UpdateInventoryActivity` workflow activity updates the inventory with the current available cars after the order has been processed.
|
||||
9. The `NotifyActivity` workflow activity sends a notification saying that order `b903d749cd814e099f06ebf4a56a2f90` has completed.
|
||||
10. The workflow terminates as completed.
|
||||
4. The `VerifyInventoryActivity` workflow activity checks the inventory data, determines if you can supply the ordered item, and responds with the number of cars in stock.
|
||||
5. The total cost of the order is 5000, so the workflow will not call the `RequestApprovalActivity` activity.
|
||||
6. The `ProcessPaymentActivity` workflow activity begins processing payment for order `b4cb2687-1af0-4f8d-9659-eb6389c07ade` and confirms if successful.
|
||||
7. The `UpdateInventoryActivity` workflow activity updates the inventory with the current available cars after the order has been processed.
|
||||
8. The `NotifyActivity` workflow activity sends a notification saying that order `b4cb2687-1af0-4f8d-9659-eb6389c07ade` has completed.
|
||||
9. The workflow terminates as completed and the OrderResult is set to processed.
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -61,7 +61,7 @@ func main() {
|
|||
|
||||
inventory := []InventoryItem{
|
||||
{ItemName: "paperclip", PerItemCost: 5, Quantity: 100},
|
||||
{ItemName: "cars", PerItemCost: 15000, Quantity: 100},
|
||||
{ItemName: "cars", PerItemCost: 5000, Quantity: 10},
|
||||
{ItemName: "computers", PerItemCost: 500, Quantity: 100},
|
||||
}
|
||||
if err := restockInventory(daprClient, inventory); err != nil {
|
||||
|
|
@ -71,7 +71,7 @@ func main() {
|
|||
fmt.Println("==========Begin the purchase of item:==========")
|
||||
|
||||
itemName := defaultItemName
|
||||
orderQuantity := 10
|
||||
orderQuantity := 1
|
||||
|
||||
totalCost := inventory[1].PerItemCost * orderQuantity
|
||||
|
||||
|
|
@ -86,49 +86,23 @@ func main() {
|
|||
log.Fatalf("failed to start workflow: %v", err)
|
||||
}
|
||||
|
||||
approvalSought := false
|
||||
|
||||
startTime := time.Now()
|
||||
|
||||
for {
|
||||
timeDelta := time.Since(startTime)
|
||||
metadata, err := wfClient.FetchWorkflowMetadata(context.Background(), id)
|
||||
waitCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
_, err = wfClient.WaitForWorkflowCompletion(waitCtx, id)
|
||||
cancel()
|
||||
if err != nil {
|
||||
log.Fatalf("failed to fetch workflow: %v", err)
|
||||
log.Fatalf("failed to wait for workflow: %v", err)
|
||||
}
|
||||
if (metadata.RuntimeStatus == workflow.StatusCompleted) || (metadata.RuntimeStatus == workflow.StatusFailed) || (metadata.RuntimeStatus == workflow.StatusTerminated) {
|
||||
fmt.Printf("Workflow completed - result: %v\n", metadata.RuntimeStatus.String())
|
||||
break
|
||||
}
|
||||
if timeDelta.Seconds() >= 10 {
|
||||
metadata, err := wfClient.FetchWorkflowMetadata(context.Background(), id)
|
||||
|
||||
respFetch, err := wfClient.FetchWorkflowMetadata(context.Background(), id, workflow.WithFetchPayloads(true))
|
||||
if err != nil {
|
||||
log.Fatalf("failed to fetch workflow: %v", err)
|
||||
}
|
||||
if totalCost > 50000 && !approvalSought && ((metadata.RuntimeStatus != workflow.StatusCompleted) || (metadata.RuntimeStatus != workflow.StatusFailed) || (metadata.RuntimeStatus != workflow.StatusTerminated)) {
|
||||
approvalSought = true
|
||||
promptForApproval(id)
|
||||
}
|
||||
}
|
||||
// Sleep before the next iteration
|
||||
time.Sleep(time.Second)
|
||||
log.Fatalf("failed to get workflow: %v", err)
|
||||
}
|
||||
|
||||
fmt.Printf("workflow status: %v\n", respFetch.RuntimeStatus)
|
||||
|
||||
fmt.Println("Purchase of item is complete")
|
||||
}
|
||||
|
||||
// promptForApproval is an example case. There is no user input required here due to this being for testing purposes only.
|
||||
// It would be perfectly valid to add a wait here or display a prompt to continue the process.
|
||||
func promptForApproval(id string) {
|
||||
wfClient, err := workflow.NewClient()
|
||||
if err != nil {
|
||||
log.Fatalf("failed to initialise wfClient: %v", err)
|
||||
}
|
||||
if err := wfClient.RaiseEvent(context.Background(), id, "manager_approval"); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func restockInventory(daprClient client.Client, inventory []InventoryItem) error {
|
||||
for _, item := range inventory {
|
||||
itemSerialized, err := json.Marshal(item)
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@ func OrderProcessingWorkflow(ctx *workflow.WorkflowContext) (any, error) {
|
|||
return OrderResult{Processed: false}, err
|
||||
}
|
||||
|
||||
if orderPayload.TotalCost > 50000 {
|
||||
if orderPayload.TotalCost > 5000 {
|
||||
var approvalRequired ApprovalRequired
|
||||
if err := ctx.CallActivity(RequestApprovalActivity, workflow.ActivityInput(orderPayload)).Await(&approvalRequired); err != nil {
|
||||
return OrderResult{Processed: false}, err
|
||||
|
|
|
|||
Loading…
Reference in New Issue