File tree Expand file tree Collapse file tree 3 files changed +17
-19
lines changed
Expand file tree Collapse file tree 3 files changed +17
-19
lines changed Original file line number Diff line number Diff line change 22
33from temporalio import activity
44
5- from polling .test_service import ComposeGreetingInput , TestService
5+ from polling .test_service import ComposeGreetingInput , get_service_result
66
77
88@activity .defn
99async def compose_greeting (input : ComposeGreetingInput ) -> str :
10- test_service = TestService ()
1110 while True :
1211 try :
1312 try :
14- result = await test_service . get_service_result (input )
13+ result = await get_service_result (input )
1514 activity .logger .info (f"Exiting activity ${ result } " )
1615 return result
1716 except Exception :
Original file line number Diff line number Diff line change 11from temporalio import activity
22
3- from polling .test_service import ComposeGreetingInput , TestService
3+ from polling .test_service import ComposeGreetingInput , get_service_result
44
55
66@activity .defn
77async def compose_greeting (input : ComposeGreetingInput ) -> str :
8- test_service = TestService ()
98 # If this raises an exception because it's not done yet, the activity will
109 # continually be scheduled for retry
11- return await test_service . get_service_result (input )
10+ return await get_service_result (input )
Original file line number Diff line number Diff line change 1+ from collections import Counter
12from dataclasses import dataclass
23
4+ from temporalio import activity
5+
6+ attempts = Counter ()
7+ ERROR_ATTEMPTS = 5
8+
39
410@dataclass
511class ComposeGreetingInput :
612 greeting : str
713 name : str
814
915
10- try_attempts = 0
11-
12-
13- class TestService :
14- def __init__ (self ):
15- self .error_attempts = 5
16+ async def get_service_result (input ):
17+ attempts [activity .info ().workflow_id ] += 1
18+ attempt = attempts [activity .info ().workflow_id ]
1619
17- async def get_service_result (self , input ):
18- global try_attempts
19- print (f"Attempt { try_attempts } of { self .error_attempts } to invoke service" )
20- try_attempts += 1
21- if try_attempts % self .error_attempts == 0 :
22- return f"{ input .greeting } , { input .name } !"
23- raise Exception ("service is down" )
20+ print (f"Attempt { attempt } of { ERROR_ATTEMPTS } to invoke service" )
21+ if attempt == ERROR_ATTEMPTS :
22+ return f"{ input .greeting } , { input .name } !"
23+ raise Exception ("service is down" )
You can’t perform that action at this time.
0 commit comments