Skip to content

fix missing sleep to properly poll inside activity #122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions polling/frequent/activities.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import asyncio
import time
from dataclasses import dataclass

from temporalio import activity
Expand All @@ -16,7 +18,19 @@ async def compose_greeting(input: ComposeGreetingInput) -> str:
test_service = TestService()
while True:
try:
result = test_service.get_service_result(input)
return result
except Exception:
try:
result = await test_service.get_service_result(input)
activity.logger.info(f"Exiting activity ${result}")
return result
except Exception as e:
# swallow exception since service is down
activity.logger.debug("Failed, trying again shortly", exc_info=True)

activity.heartbeat("Invoking activity")
await asyncio.sleep(1)
except asyncio.CancelledError:
# activity was either cancelled or workflow was completed or worker shut down
# if you need to clean up you can catch this.
# Here we are just reraising the exception
raise

2 changes: 1 addition & 1 deletion polling/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def __init__(self):
self.try_attempts = 0
self.error_attempts = 5

def get_service_result(self, input):
async def get_service_result(self, input):
print(
f"Attempt {self.try_attempts}"
f" of {self.error_attempts} to invoke service"
Expand Down
Loading