Skip to content

Commit 6fcc52c

Browse files
committed
Isolate counters associated with different workflows
1 parent ddd7b83 commit 6fcc52c

File tree

3 files changed

+17
-19
lines changed

3 files changed

+17
-19
lines changed

polling/frequent/activities.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22

33
from 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
99
async 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:

polling/infrequent/activities.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
from 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
77
async 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)

polling/test_service.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1+
from collections import Counter
12
from dataclasses import dataclass
23

4+
from temporalio import activity
5+
6+
attempts = Counter()
7+
ERROR_ATTEMPTS = 5
8+
39

410
@dataclass
511
class 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")

0 commit comments

Comments
 (0)