11from datetime import timedelta
2- import logging
32import json
43
54from dapr .ext .workflow import DaprWorkflowContext , WorkflowActivityContext , WorkflowRuntime , when_any
1312
1413wfr = WorkflowRuntime ()
1514
16- logging .basicConfig (level = logging .INFO )
17-
1815
1916@wfr .workflow (name = "order_processing_workflow" )
2017def order_processing_workflow (ctx : DaprWorkflowContext , order_payload_str : str ):
@@ -78,36 +75,28 @@ def order_processing_workflow(ctx: DaprWorkflowContext, order_payload_str: str):
7875@wfr .activity (name = "notify_activity" )
7976def notify_activity (ctx : WorkflowActivityContext , input : Notification ):
8077 """Defines Notify Activity. This is used by the workflow to send out a notification"""
81- # Create a logger
82- logger = logging .getLogger ('NotifyActivity' )
83- logger .info (input .message )
78+ print (input .message , flush = True )
8479
8580
8681@wfr .activity (name = "process_payment_activity" )
8782def process_payment_activity (ctx : WorkflowActivityContext , input : PaymentRequest ):
8883 """Defines Process Payment Activity.This is used by the workflow to process a payment"""
89- logger = logging .getLogger ('ProcessPaymentActivity' )
90- logger .info ('Processing payment: ' + f'{ input .request_id } ' + ' for '
91- + f'{ input .quantity } ' + ' ' + f'{ input .item_being_purchased } ' + ' at ' + f'{ input .amount } '
92- + ' USD' )
93- logger .info (f'Payment for request ID { input .request_id } processed successfully' )
84+ print (f'Processing payment: { input .request_id } for { input .quantity } { input .item_being_purchased } at { input .amount } USD' , flush = True )
85+ print (f'Payment for request ID { input .request_id } processed successfully' , flush = True )
9486
9587
9688@wfr .activity (name = "verify_inventory_activity" )
9789def verify_inventory_activity (ctx : WorkflowActivityContext ,
9890 input : InventoryRequest ) -> InventoryResult :
9991 """Defines Verify Inventory Activity. This is used by the workflow to verify if inventory
10092 is available for the order"""
101- logger = logging .getLogger ('VerifyInventoryActivity' )
102-
103- logger .info ('Verifying inventory for order ' + f'{ input .request_id } ' + ' of '
104- + f'{ input .quantity } ' + ' ' + f'{ input .item_name } ' )
93+ print (f'Verifying inventory for order { input .request_id } of { input .quantity } { input .item_name } ' , flush = True )
10594 with DaprClient (f'{ settings .DAPR_RUNTIME_HOST } :{ settings .DAPR_GRPC_PORT } ' ) as client :
10695 result = client .get_state (store_name , input .item_name )
10796 if result .data is None :
10897 return InventoryResult (False , None )
10998 res_json = json .loads (str (result .data .decode ('utf-8' )))
110- logger . info (f'There are { res_json ["quantity" ]} { res_json ["name" ]} available for purchase' )
99+ print (f'There are { res_json ["quantity" ]} { res_json ["name" ]} available for purchase' , flush = True )
111100 inventory_item = InventoryItem (item_name = input .item_name ,
112101 per_item_cost = res_json ['per_item_cost' ],
113102 quantity = res_json ['quantity' ])
@@ -124,10 +113,7 @@ def update_inventory_activity(ctx: WorkflowActivityContext,
124113 """Defines Update Inventory Activity. This is used by the workflow to check if inventory
125114 is sufficient to fulfill the order and updates inventory by reducing order quantity from
126115 inventory."""
127- logger = logging .getLogger ('UpdateInventoryActivity' )
128-
129- logger .info ('Checking inventory for order ' + f'{ input .request_id } ' + ' for '
130- + f'{ input .quantity } ' + ' ' + f'{ input .item_being_purchased } ' )
116+ print (f'Checking inventory for order { input .request_id } for { input .quantity } { input .item_being_purchased } ' , flush = True )
131117 with DaprClient (f'{ settings .DAPR_RUNTIME_HOST } :{ settings .DAPR_GRPC_PORT } ' ) as client :
132118 result = client .get_state (store_name , input .item_being_purchased )
133119 res_json = json .loads (str (result .data .decode ('utf-8' )))
@@ -138,7 +124,7 @@ def update_inventory_activity(ctx: WorkflowActivityContext,
138124 + ' could not be processed. Insufficient inventory.' )
139125 new_val = f'{{"name": "{ input .item_being_purchased } ", "quantity": { str (new_quantity )} , "per_item_cost": { str (per_item_cost )} }}'
140126 client .save_state (store_name , input .item_being_purchased , new_val )
141- logger . info (f'There are now { new_quantity } { input .item_being_purchased } left in stock' )
127+ print (f'There are now { new_quantity } { input .item_being_purchased } left in stock' , flush = True )
142128
143129
144130
@@ -148,7 +134,4 @@ def request_approval_activity(ctx: WorkflowActivityContext,
148134 """Defines Request Approval Activity. This is used by the workflow to request approval
149135 for payment of an order. This activity is used only if the order total cost is greater than
150136 a particular threshold"""
151- logger = logging .getLogger ('RequestApprovalActivity' )
152-
153- logger .info ('Requesting approval for payment of ' + f'{ input ["total_cost" ]} ' + ' USD for '
154- + f'{ input ["quantity" ]} ' + ' ' + f'{ input ["item_name" ]} ' )
137+ print (f'Requesting approval for payment of { input ["total_cost" ]} USD for { input ["quantity" ]} { input ["item_name" ]} ' , flush = True )
0 commit comments