Skip to content

Commit ac7fcc5

Browse files
authored
Use the activity info provided during interceptor construction (#903)
* Use the activity info provided during interceptor construction instead of the activity context * Linting
1 parent 29599e5 commit ac7fcc5

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

temporalio/worker/_activity.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -784,8 +784,7 @@ def info(self) -> temporalio.activity.Info:
784784
return self._info
785785

786786
def heartbeat(self, *details: Any) -> None:
787-
info = temporalio.activity.info()
788-
self._worker._heartbeat(info.task_token, *details)
787+
self._worker._heartbeat(self._info.task_token, *details)
789788

790789

791790
# This has to be defined at the top-level to be picklable for process executors

tests/worker/test_activity.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,3 +1416,30 @@ async def raise_error():
14161416

14171417
finally:
14181418
activity.logger.base_logger.removeHandler(CustomLogHandler())
1419+
1420+
1421+
async def test_activity_heartbeat_context(client: Client, worker: ExternalWorker):
1422+
@activity.defn
1423+
async def heartbeat():
1424+
if activity.info().attempt == 1:
1425+
context: activity._Context = activity._Context.current()
1426+
1427+
def heartbeat_task():
1428+
async def h():
1429+
if context.heartbeat is not None:
1430+
context.heartbeat("Some detail")
1431+
1432+
asyncio.run(h())
1433+
1434+
thread = threading.Thread(target=heartbeat_task)
1435+
thread.start()
1436+
thread.join()
1437+
raise RuntimeError("oh no!")
1438+
else:
1439+
assert len(activity.info().heartbeat_details) == 1
1440+
return "details: " + activity.info().heartbeat_details[0]
1441+
1442+
result = await _execute_workflow_with_activity(
1443+
client, worker, heartbeat, retry_max_attempts=2
1444+
)
1445+
assert result.result == "details: Some detail"

0 commit comments

Comments
 (0)