Skip to content

Commit

Permalink
fix missing sleep to properly poll inside activity (#122)
Browse files Browse the repository at this point in the history
* fix missing sleep to properly poll inside activity

* implement Chads guidance

* take2 and wrap whole thing in cancellederror

* moar better log

* await the things

* make test_service be async

* poe!
  • Loading branch information
mnichols authored Jun 11, 2024
1 parent 12d57bf commit ce12576
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
19 changes: 16 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,18 @@ 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

0 comments on commit ce12576

Please sign in to comment.