4
4
5
5
from dapr .clients import DaprClient
6
6
from dapr .conf import settings
7
- from dapr .ext .workflow import WorkflowRuntime
7
+ from dapr .ext .workflow import DaprWorkflowClient , WorkflowStatus
8
8
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
11
10
from model import InventoryItem , OrderPayload
12
11
13
12
store_name = "statestore"
14
- workflow_component = "dapr"
15
13
workflow_name = "order_processing_workflow"
16
14
default_item_name = "cars"
17
15
18
16
class WorkflowConsoleApp :
19
17
def main (self ):
20
18
print ("*** Welcome to the Dapr Workflow console app sample!" , flush = True )
21
19
print ("*** Using this app, you can place orders that start workflows." , flush = True )
20
+
21
+ wfr .start ()
22
22
# Wait for the sidecar to become available
23
23
sleep (5 )
24
24
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
+ }
33
32
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 )
39
33
34
+ daprClient = DaprClient (address = f'{ settings .DAPR_RUNTIME_HOST } :{ settings .DAPR_GRPC_PORT } ' )
40
35
self .restock_inventory (daprClient , baseInventory )
41
36
42
37
print ("==========Begin the purchase of item:==========" , flush = True )
43
38
item_name = default_item_name
44
39
order_quantity = 10
45
-
46
40
total_cost = int (order_quantity ) * baseInventory [item_name ].per_item_cost
47
41
order = OrderPayload (item_name = item_name , quantity = int (order_quantity ), total_cost = total_cost )
42
+
48
43
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
53
47
54
- def prompt_for_approval (daprClient : DaprClient ):
48
+ def prompt_for_approval (wfClient : DaprWorkflowClient ):
55
49
"""This is a helper function to prompt for approval.
56
50
Not using the prompt here ACTUALLY, as quickstart validation is required to be automated.
57
51
@@ -65,9 +59,9 @@ def prompt_for_approval(daprClient: DaprClient):
65
59
if state.runtime_status.name == "COMPLETED":
66
60
return
67
61
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})
69
63
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})
71
65
72
66
## Additionally, you would need to import signal and define timeout_error:
73
67
# import signal
@@ -76,32 +70,32 @@ def prompt_for_approval(daprClient: DaprClient):
76
70
77
71
# signal.signal(signal.SIGALRM, timeout_error)
78
72
"""
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 })
81
74
82
75
approval_seeked = False
83
76
start_time = datetime .now ()
84
77
while True :
85
78
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
+
87
81
if not state :
88
82
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 } :
92
86
print (f'Workflow completed! Result: { state .runtime_status } ' , flush = True )
93
87
break
88
+
89
+
94
90
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 :
101
93
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
+
103
98
104
- print ("Purchase of item is " , state .runtime_status , flush = True )
105
99
106
100
def restock_inventory (self , daprClient : DaprClient , baseInventory ):
107
101
for key , item in baseInventory .items ():
0 commit comments