Skip to content

Commit ce12576

Browse files
authored
fix missing sleep to properly poll inside activity (temporalio#122)
* 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!
1 parent 12d57bf commit ce12576

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

polling/frequent/activities.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import asyncio
2+
import time
13
from dataclasses import dataclass
24

35
from temporalio import activity
@@ -16,7 +18,18 @@ async def compose_greeting(input: ComposeGreetingInput) -> str:
1618
test_service = TestService()
1719
while True:
1820
try:
19-
result = test_service.get_service_result(input)
20-
return result
21-
except Exception:
21+
try:
22+
result = await test_service.get_service_result(input)
23+
activity.logger.info(f"Exiting activity ${result}")
24+
return result
25+
except Exception as e:
26+
# swallow exception since service is down
27+
activity.logger.debug("Failed, trying again shortly", exc_info=True)
28+
2229
activity.heartbeat("Invoking activity")
30+
await asyncio.sleep(1)
31+
except asyncio.CancelledError:
32+
# activity was either cancelled or workflow was completed or worker shut down
33+
# if you need to clean up you can catch this.
34+
# Here we are just reraising the exception
35+
raise

polling/test_service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ def __init__(self):
33
self.try_attempts = 0
44
self.error_attempts = 5
55

6-
def get_service_result(self, input):
6+
async def get_service_result(self, input):
77
print(
88
f"Attempt {self.try_attempts}"
99
f" of {self.error_attempts} to invoke service"

0 commit comments

Comments
 (0)