mirror of https://github.com/dapr/quickstarts.git
Add readme
Signed-off-by: Deepanshu Agarwal <deepanshu.agarwal1984@gmail.com>
This commit is contained in:
parent
471d67837d
commit
9645fe90c1
|
|
@ -0,0 +1,71 @@
|
|||
# Dapr workflows
|
||||
|
||||
In this quickstart, you'll create a simple console application to demonstrate Dapr's workflow programming model and the workflow management API. The console app starts and manages the lifecycle of a workflow that stores and retrieves data in a state store.
|
||||
|
||||
This quickstart includes one project:
|
||||
|
||||
- python console app `order-processor`
|
||||
|
||||
The quickstart contains 1 workflow order_processing_workflow to simulate purchasing items from a store, and 5 unique activities within the workflow. These 5 activities are as follows:
|
||||
|
||||
- notify_activity: 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.
|
||||
- process_payment_activity: This activity is responsible for processing and authorizing the payment.
|
||||
- verify_inventory_activity: This activity checks the state store to ensure that there is enough inventory present for purchase.
|
||||
- update_inventory_activity: This activity removes the requested items from the state store and updates the store with the new remaining inventory value.
|
||||
- requst_approval_activity: This activity seeks approval from Manager, if payment is greater than 50000 USD.
|
||||
|
||||
### Run the order processor workflow
|
||||
|
||||
1. Open a new terminal window and navigate to `order-processor` directory:
|
||||
|
||||
<!-- STEP
|
||||
name: Install requirements
|
||||
-->
|
||||
|
||||
```sh
|
||||
pip3 install -r requirements.txt
|
||||
```
|
||||
|
||||
<!-- END_STEP -->
|
||||
2. Run the console app with Dapr:
|
||||
<!-- STEP
|
||||
name: Running this example
|
||||
expected_stdout_lines:
|
||||
- "== APP == New counter value is: 1!"
|
||||
- "== APP == New counter value is: 11!"
|
||||
- "== APP == New counter value is: 111!"
|
||||
- "== APP == New counter value is: 1111!"
|
||||
output_match_mode: substring
|
||||
background: true
|
||||
timeout_seconds: 30
|
||||
sleep: 15
|
||||
-->
|
||||
|
||||
```sh
|
||||
dapr run --app-id order-processor --app-protocol grpc --dapr-grpc-port 50001 --components-path ../../../components/ --placement-host-address localhost:50005 -- python3 app.py
|
||||
```
|
||||
|
||||
<!-- END_STEP -->
|
||||
|
||||
3. Expected output
|
||||
|
||||
```
|
||||
==========Begin the purchase of item:==========
|
||||
To restock items, type 'restock'.
|
||||
Enter the name of one of the following items to order: paperclip, cars, computers: cars
|
||||
How many cars would you like to purchase? 11
|
||||
Starting order workflow, purchasing 11 of cars
|
||||
INFO:NotifyActivity:Received order b903d749cd814e099f06ebf4a56a2f90 for 11 cars at $165000 !
|
||||
INFO:VerifyInventoryActivity:Verifying inventory for order b903d749cd814e099f06ebf4a56a2f90 of 11 cars
|
||||
INFO:VerifyInventoryActivity:There are 100 Cars available for purchase
|
||||
INFO:RequestApprovalActivity:Requesting approval for payment of 165000 USD for 11 cars
|
||||
(ID = b903d749cd814e099f06ebf4a56a2f90) requires approval. Approve? [Y/N] y
|
||||
INFO:NotifyActivity:Payment for order b903d749cd814e099f06ebf4a56a2f90 has been approved!
|
||||
INFO:ProcessPaymentActivity:Processing payment: b903d749cd814e099f06ebf4a56a2f90 for 11 cars at 165000 USD
|
||||
INFO:ProcessPaymentActivity:Payment for request ID b903d749cd814e099f06ebf4a56a2f90 processed successfully
|
||||
INFO:UpdateInventoryActivity:Checking inventory for order b903d749cd814e099f06ebf4a56a2f90 for 11 cars
|
||||
INFO:UpdateInventoryActivity:There are now 89 cars left in stock
|
||||
INFO:NotifyActivity:Order b903d749cd814e099f06ebf4a56a2f90 has completed!
|
||||
Workflow completed! Result: COMPLETED
|
||||
Purchase of item is COMPLETED
|
||||
```
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
import threading
|
||||
from time import sleep
|
||||
from dapr.ext.workflow import WorkflowRuntime, DaprWorkflowClient
|
||||
from workflow_activities import order_processing_workflow, notify_activity, process_payment_activity, reserve_inventory_activity, update_inventory_activity, requst_approval_activity
|
||||
from workflow_activities import order_processing_workflow, notify_activity, process_payment_activity, verify_inventory_activity, update_inventory_activity, requst_approval_activity
|
||||
from dapr.clients import DaprClient
|
||||
from model import InventoryItem, OrderPayload
|
||||
from util import get_address
|
||||
|
|
@ -31,7 +31,7 @@ class WorkflowConsoleApp:
|
|||
workflowRuntime.register_workflow(order_processing_workflow)
|
||||
workflowRuntime.register_activity(notify_activity)
|
||||
workflowRuntime.register_activity(requst_approval_activity)
|
||||
workflowRuntime.register_activity(reserve_inventory_activity)
|
||||
workflowRuntime.register_activity(verify_inventory_activity)
|
||||
workflowRuntime.register_activity(process_payment_activity)
|
||||
workflowRuntime.register_activity(update_inventory_activity)
|
||||
workflowRuntime.start()
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ def order_processing_workflow(ctx: DaprWorkflowContext, orderPayload: OrderPaylo
|
|||
logging.basicConfig(level=logging.INFO)
|
||||
order_id = ctx.instance_id
|
||||
yield ctx.call_activity(notify_activity, input=Notification(message=f'Received order {order_id} for {orderPayload.quantity} {orderPayload.item_name} at ${orderPayload.total_cost} !'))
|
||||
result = yield ctx.call_activity(reserve_inventory_activity, input=InventoryRequest(request_id=order_id, item_name=orderPayload.item_name, quantity=orderPayload.quantity))
|
||||
result = yield ctx.call_activity(verify_inventory_activity, input=InventoryRequest(request_id=order_id, item_name=orderPayload.item_name, quantity=orderPayload.quantity))
|
||||
if not result.success:
|
||||
yield ctx.call_activity(notify_activity, input=Notification(message=f'Insufficient inventory for {orderPayload.item_name}!'))
|
||||
return OrderResult(processed=False)
|
||||
|
|
@ -60,11 +60,11 @@ def process_payment_activity(ctx: WorkflowActivityContext, input: PaymentRequest
|
|||
logger.info(f'Payment for request ID {input.request_id} processed successfully')
|
||||
|
||||
|
||||
def reserve_inventory_activity(ctx: WorkflowActivityContext,
|
||||
def verify_inventory_activity(ctx: WorkflowActivityContext,
|
||||
input: InventoryRequest) -> InventoryResult:
|
||||
logger = logging.getLogger('ReserveInventoryActivity')
|
||||
logger = logging.getLogger('VerifyInventoryActivity')
|
||||
|
||||
logger.info(f'Reserving inventory for order {input.request_id} of {input.quantity} {input.item_name}')
|
||||
logger.info(f'Verifying inventory for order {input.request_id} of {input.quantity} {input.item_name}')
|
||||
inventoryItem: InventoryItem
|
||||
with DaprClient(f'{address["host"]}:{address["port"]}') as client:
|
||||
result = client.get_state(store_name, input.item_name)
|
||||
|
|
|
|||
Loading…
Reference in New Issue