44
55from dapr .clients import DaprClient
66from dapr .conf import settings
7- from dapr .ext .workflow import WorkflowRuntime
7+ from dapr .ext .workflow import DaprWorkflowClient , WorkflowStatus
88
9- from workflow import order_processing_workflow , notify_activity , process_payment_activity , \
10- verify_inventory_activity , update_inventory_activity , requst_approval_activity
9+ from workflow import wfr , order_processing_workflow
1110from model import InventoryItem , OrderPayload
1211
1312store_name = "statestore"
14- workflow_component = "dapr"
1513workflow_name = "order_processing_workflow"
1614default_item_name = "cars"
1715
1816class WorkflowConsoleApp :
1917 def main (self ):
2018 print ("*** Welcome to the Dapr Workflow console app sample!" , flush = True )
2119 print ("*** Using this app, you can place orders that start workflows." , flush = True )
20+
21+ wfr .start ()
2222 # Wait for the sidecar to become available
2323 sleep (5 )
2424
25- workflowRuntime = WorkflowRuntime (settings .DAPR_RUNTIME_HOST , settings .DAPR_GRPC_PORT )
26- workflowRuntime .register_workflow (order_processing_workflow )
27- workflowRuntime .register_activity (notify_activity )
28- workflowRuntime .register_activity (requst_approval_activity )
29- workflowRuntime .register_activity (verify_inventory_activity )
30- workflowRuntime .register_activity (process_payment_activity )
31- workflowRuntime .register_activity (update_inventory_activity )
32- workflowRuntime .start ()
25+ wfClient = DaprWorkflowClient ()
26+
27+ baseInventory = {
28+ "paperclip" : InventoryItem ("Paperclip" , 5 , 100 ),
29+ "cars" : InventoryItem ("Cars" , 15000 , 100 ),
30+ "computers" : InventoryItem ("Computers" , 500 , 100 ),
31+ }
3332
34- daprClient = DaprClient (address = f'{ settings .DAPR_RUNTIME_HOST } :{ settings .DAPR_GRPC_PORT } ' )
35- baseInventory = {}
36- baseInventory ["paperclip" ] = InventoryItem ("Paperclip" , 5 , 100 )
37- baseInventory ["cars" ] = InventoryItem ("Cars" , 15000 , 100 )
38- baseInventory ["computers" ] = InventoryItem ("Computers" , 500 , 100 )
3933
34+ daprClient = DaprClient (address = f'{ settings .DAPR_RUNTIME_HOST } :{ settings .DAPR_GRPC_PORT } ' )
4035 self .restock_inventory (daprClient , baseInventory )
4136
4237 print ("==========Begin the purchase of item:==========" , flush = True )
4338 item_name = default_item_name
4439 order_quantity = 10
45-
4640 total_cost = int (order_quantity ) * baseInventory [item_name ].per_item_cost
4741 order = OrderPayload (item_name = item_name , quantity = int (order_quantity ), total_cost = total_cost )
42+
4843 print (f'Starting order workflow, purchasing { order_quantity } of { item_name } ' , flush = True )
49- start_resp = daprClient .start_workflow (workflow_component = workflow_component ,
50- workflow_name = workflow_name ,
51- input = order )
52- _id = start_resp .instance_id
44+ instance_id = wfClient .schedule_new_workflow (
45+ workflow = order_processing_workflow , input = order .to_json ())
46+ _id = instance_id
5347
54- def prompt_for_approval (daprClient : DaprClient ):
48+ def prompt_for_approval (wfClient : DaprWorkflowClient ):
5549 """This is a helper function to prompt for approval.
5650 Not using the prompt here ACTUALLY, as quickstart validation is required to be automated.
5751
@@ -65,9 +59,9 @@ def prompt_for_approval(daprClient: DaprClient):
6559 if state.runtime_status.name == "COMPLETED":
6660 return
6761 if approved.lower() == "y":
68- client .raise_workflow_event(instance_id=_id, event_name="manager_approval", data={'approval': True})
62+ wfClient .raise_workflow_event(instance_id=_id, event_name="manager_approval", data={'approval': True})
6963 else:
70- client .raise_workflow_event(instance_id=_id, event_name="manager_approval", data={'approval': False})
64+ wfClient .raise_workflow_event(instance_id=_id, event_name="manager_approval", data={'approval': False})
7165
7266 ## Additionally, you would need to import signal and define timeout_error:
7367 # import signal
@@ -76,32 +70,32 @@ def prompt_for_approval(daprClient: DaprClient):
7670
7771 # signal.signal(signal.SIGALRM, timeout_error)
7872 """
79- daprClient .raise_workflow_event (instance_id = _id , workflow_component = workflow_component ,
80- event_name = "manager_approval" , event_data = {'approval' : True })
73+ wfClient .raise_workflow_event (instance_id = _id , event_name = "manager_approval" , data = {'approval' : True })
8174
8275 approval_seeked = False
8376 start_time = datetime .now ()
8477 while True :
8578 time_delta = datetime .now () - start_time
86- state = daprClient .get_workflow (instance_id = _id , workflow_component = workflow_component )
79+ state = wfClient .get_workflow_state (instance_id = _id )
80+
8781 if not state :
8882 print ("Workflow not found!" ) # not expected
89- elif state . runtime_status == "Completed" or \
90- state . runtime_status == "Failed" or \
91- state .runtime_status == "Terminated" :
83+ break
84+
85+ if state .runtime_status in { WorkflowStatus . COMPLETED , WorkflowStatus . FAILED , WorkflowStatus . TERMINATED } :
9286 print (f'Workflow completed! Result: { state .runtime_status } ' , flush = True )
9387 break
88+
89+
9490 if time_delta .total_seconds () >= 10 :
95- state = daprClient .get_workflow (instance_id = _id , workflow_component = workflow_component )
96- if total_cost > 50000 and (
97- state .runtime_status != "Completed" or
98- state .runtime_status != "Failed" or
99- state .runtime_status != "Terminated"
100- ) and not approval_seeked :
91+ state = wfClient .get_workflow_state (instance_id = _id )
92+ if total_cost > 50000 and state not in {WorkflowStatus .COMPLETED , WorkflowStatus .FAILED , WorkflowStatus .TERMINATED } and not approval_seeked :
10193 approval_seeked = True
102- threading .Thread (target = prompt_for_approval (daprClient ), daemon = True ).start ()
94+ threading .Thread (target = prompt_for_approval (wfClient ), daemon = True ).start ()
95+
96+ wfr .shutdown ()
97+
10398
104- print ("Purchase of item is " , state .runtime_status , flush = True )
10599
106100 def restock_inventory (self , daprClient : DaprClient , baseInventory ):
107101 for key , item in baseInventory .items ():
0 commit comments