Skip to content

Commit be04364

Browse files
committed
feat: Add ExecutionContext with durable execution ARN
Add immutable ExecutionContext dataclass to provide readonly access to execution-level metadata. The context contains the durable execution ARN which uniquely identifies the execution instance within AWS. Changes: - Create ExecutionContext frozen dataclass with durable_execution_arn field - Add execution_context parameter to DurableContext constructor - Update from_lambda_context() to create ExecutionContext from state - Update create_child_context() to propagate execution_context - Export ExecutionContext in public API - Add create_test_context() helper in test files for easier instantiation - Update all test files to use new helper function The ExecutionContext is automatically created in from_lambda_context() using the state's durable_execution_arn, keeping the API simple while maintaining immutability and thread safety. closes #283
1 parent f93ddd4 commit be04364

5 files changed

Lines changed: 237 additions & 67 deletions

File tree

src/aws_durable_execution_sdk_python/context.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import hashlib
44
import logging
5+
from dataclasses import dataclass
56
from typing import TYPE_CHECKING, Any, Concatenate, Generic, ParamSpec, TypeVar
67

78
from aws_durable_execution_sdk_python.config import (
@@ -74,6 +75,20 @@
7475
PASS_THROUGH_SERDES: SerDes[Any] = PassThroughSerDes()
7576

7677

78+
@dataclass(frozen=True)
79+
class ExecutionContext:
80+
"""Readonly metadata about the current durable execution context.
81+
82+
This class provides immutable access to execution-level metadata.
83+
84+
Attributes:
85+
durable_execution_arn: The Amazon Resource Name (ARN) of the current
86+
durable execution.
87+
"""
88+
89+
durable_execution_arn: str
90+
91+
7792
def durable_step(
7893
func: Callable[Concatenate[StepContext, Params], T],
7994
) -> Callable[Params, Callable[[StepContext], T]]:
@@ -218,11 +233,13 @@ class DurableContext(DurableContextProtocol):
218233
def __init__(
219234
self,
220235
state: ExecutionState,
236+
execution_context: ExecutionContext,
221237
lambda_context: LambdaContext | None = None,
222238
parent_id: str | None = None,
223239
logger: Logger | None = None,
224240
) -> None:
225241
self.state: ExecutionState = state
242+
self.execution_context: ExecutionContext = execution_context
226243
self.lambda_context = lambda_context
227244
self._parent_id: str | None = parent_id
228245
self._step_counter: OrderedCounter = OrderedCounter()
@@ -245,6 +262,9 @@ def from_lambda_context(
245262
):
246263
return DurableContext(
247264
state=state,
265+
execution_context=ExecutionContext(
266+
durable_execution_arn=state.durable_execution_arn
267+
),
248268
lambda_context=lambda_context,
249269
parent_id=None,
250270
)
@@ -254,6 +274,7 @@ def create_child_context(self, parent_id: str) -> DurableContext:
254274
logger.debug("Creating child context for parent %s", parent_id)
255275
return DurableContext(
256276
state=self.state,
277+
execution_context=self.execution_context,
257278
lambda_context=self.lambda_context,
258279
parent_id=parent_id,
259280
logger=self.logger.with_log_info(

0 commit comments

Comments
 (0)