-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Isolate counters associated with different workflows
- Loading branch information
1 parent
ddd7b83
commit 6fcc52c
Showing
3 changed files
with
17 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
from temporalio import activity | ||
|
||
from polling.test_service import ComposeGreetingInput, TestService | ||
from polling.test_service import ComposeGreetingInput, get_service_result | ||
|
||
|
||
@activity.defn | ||
async def compose_greeting(input: ComposeGreetingInput) -> str: | ||
test_service = TestService() | ||
# If this raises an exception because it's not done yet, the activity will | ||
# continually be scheduled for retry | ||
return await test_service.get_service_result(input) | ||
return await get_service_result(input) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,23 @@ | ||
from collections import Counter | ||
from dataclasses import dataclass | ||
|
||
from temporalio import activity | ||
|
||
attempts = Counter() | ||
ERROR_ATTEMPTS = 5 | ||
|
||
|
||
@dataclass | ||
class ComposeGreetingInput: | ||
greeting: str | ||
name: str | ||
|
||
|
||
try_attempts = 0 | ||
|
||
|
||
class TestService: | ||
def __init__(self): | ||
self.error_attempts = 5 | ||
async def get_service_result(input): | ||
attempts[activity.info().workflow_id] += 1 | ||
attempt = attempts[activity.info().workflow_id] | ||
|
||
async def get_service_result(self, input): | ||
global try_attempts | ||
print(f"Attempt {try_attempts} of {self.error_attempts} to invoke service") | ||
try_attempts += 1 | ||
if try_attempts % self.error_attempts == 0: | ||
return f"{input.greeting}, {input.name}!" | ||
raise Exception("service is down") | ||
print(f"Attempt {attempt} of {ERROR_ATTEMPTS} to invoke service") | ||
if attempt == ERROR_ATTEMPTS: | ||
return f"{input.greeting}, {input.name}!" | ||
raise Exception("service is down") |