Skip to content

Commit e8bc3f6

Browse files
authored
Rename to invocation_context_managers (#150)
This commit renames context_managers to invocation_context_managers to better reflect that the lifetime of the context manager is bound to an invocation (attempt)
1 parent cd1e8e3 commit e8bc3f6

5 files changed

Lines changed: 43 additions & 21 deletions

File tree

python/restate/context_managers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ def value(self) -> T:
5757
"""Return the value yielded by the wrapped context manager."""
5858
val = self._value_var.get()
5959
if val is None:
60-
raise LookupError("Context manager value accessed outside of context manager scope (has not been entered yet)")
60+
raise LookupError(
61+
"Context manager value accessed outside of context manager scope (has not been entered yet)"
62+
)
6163
return val
6264

6365
def __call__(self, *args: P.args, **kwargs: P.kwargs) -> AsyncContextManager[None]:

python/restate/object.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ def __init__(
8787
enable_lazy_state: typing.Optional[bool] = None,
8888
ingress_private: typing.Optional[bool] = None,
8989
invocation_retry_policy: typing.Optional[InvocationRetryPolicy] = None,
90-
context_managers: typing.Optional[typing.List[typing.Callable[[], typing.AsyncContextManager[None]]]] = None,
90+
invocation_context_managers: typing.Optional[
91+
typing.List[typing.Callable[[], typing.AsyncContextManager[None]]]
92+
] = None,
9193
):
9294
self.service_tag = ServiceTag("object", name, description, metadata)
9395
self.handlers = {}
@@ -98,7 +100,7 @@ def __init__(
98100
self.enable_lazy_state = enable_lazy_state
99101
self.ingress_private = ingress_private
100102
self.invocation_retry_policy = invocation_retry_policy
101-
self.context_managers = context_managers
103+
self.context_managers = invocation_context_managers
102104

103105
@property
104106
def name(self):
@@ -124,7 +126,9 @@ def handler(
124126
enable_lazy_state: typing.Optional[bool] = None,
125127
ingress_private: typing.Optional[bool] = None,
126128
invocation_retry_policy: typing.Optional[InvocationRetryPolicy] = None,
127-
context_managers: typing.Optional[typing.List[typing.Callable[[], typing.AsyncContextManager[None]]]] = None,
129+
invocation_context_managers: typing.Optional[
130+
typing.List[typing.Callable[[], typing.AsyncContextManager[None]]]
131+
] = None,
128132
) -> typing.Callable[[T], T]:
129133
"""
130134
Decorator for defining a handler function.
@@ -188,8 +192,8 @@ def wrapped(*args, **kwargs):
188192

189193
signature = inspect.signature(fn, eval_str=True)
190194
combined_context_managers = (
191-
(self.context_managers or []) + (context_managers or [])
192-
if self.context_managers or context_managers
195+
(self.context_managers or []) + (invocation_context_managers or [])
196+
if self.context_managers or invocation_context_managers
193197
else None
194198
)
195199
handler = make_handler(

python/restate/service.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class Service:
6868
NOTE: You can set this field only if you register this service against restate-server >= 1.4,
6969
otherwise the service discovery will fail.
7070
invocation_retry_policy (InvocationRetryPolicy, optional): Retry policy applied for all invocations to this service.
71+
invocation_context_managers (List[Callable[[], AsyncContextManager[None]]], optional): List of context manager factories that will be applied to each invocation of this service.
7172
"""
7273

7374
def __init__(
@@ -81,7 +82,9 @@ def __init__(
8182
idempotency_retention: typing.Optional[timedelta] = None,
8283
ingress_private: typing.Optional[bool] = None,
8384
invocation_retry_policy: typing.Optional[InvocationRetryPolicy] = None,
84-
context_managers: typing.Optional[typing.List[typing.Callable[[], typing.AsyncContextManager[None]]]] = None,
85+
invocation_context_managers: typing.Optional[
86+
typing.List[typing.Callable[[], typing.AsyncContextManager[None]]]
87+
] = None,
8588
) -> None:
8689
self.service_tag = ServiceTag("service", name, description, metadata)
8790
self.handlers: typing.Dict[str, Handler] = {}
@@ -91,7 +94,7 @@ def __init__(
9194
self.idempotency_retention = idempotency_retention
9295
self.ingress_private = ingress_private
9396
self.invocation_retry_policy = invocation_retry_policy
94-
self.context_managers = context_managers
97+
self.context_managers = invocation_context_managers
9598

9699
@property
97100
def name(self):
@@ -114,7 +117,9 @@ def handler(
114117
idempotency_retention: typing.Optional[timedelta] = None,
115118
ingress_private: typing.Optional[bool] = None,
116119
invocation_retry_policy: typing.Optional[InvocationRetryPolicy] = None,
117-
context_managers: typing.Optional[typing.List[typing.Callable[[], typing.AsyncContextManager[None]]]] = None,
120+
invocation_context_managers: typing.Optional[
121+
typing.List[typing.Callable[[], typing.AsyncContextManager[None]]]
122+
] = None,
118123
) -> typing.Callable[[T], T]:
119124
"""
120125
Decorator for defining a handler function.
@@ -152,6 +157,7 @@ def handler(
152157
NOTE: You can set this field only if you register this service against restate-server >= 1.4,
153158
otherwise the service discovery will fail.
154159
invocation_retry_policy (InvocationRetryPolicy, optional): Retry policy applied for all invocations to this handler.
160+
invocation_context_managers: List of context manager factories that will be applied to each invocation of this handler.
155161
156162
Returns:
157163
Callable: The decorated function.
@@ -176,8 +182,8 @@ def wrapped(*args, **kwargs):
176182

177183
# combine context managers or leave None if both are None
178184
combined_context_managers = (
179-
(self.context_managers or []) + (context_managers or [])
180-
if self.context_managers or context_managers
185+
(self.context_managers or []) + (invocation_context_managers or [])
186+
if self.context_managers or invocation_context_managers
181187
else None
182188
)
183189

python/restate/workflow.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class Workflow:
7676
NOTE: You can set this field only if you register this service against restate-server >= 1.4,
7777
otherwise the service discovery will fail.
7878
invocation_retry_policy (InvocationRetryPolicy, optional): Retry policy applied for all invocations to this workflow.
79+
invocation_context_managers (List[Callable[[], AsyncContextManager[None]]], optional): List of context manager factories that will be applied to each invocation of this workflow.
7980
"""
8081

8182
handlers: typing.Dict[str, Handler[typing.Any, typing.Any]]
@@ -92,7 +93,9 @@ def __init__(
9293
enable_lazy_state: typing.Optional[bool] = None,
9394
ingress_private: typing.Optional[bool] = None,
9495
invocation_retry_policy: typing.Optional[InvocationRetryPolicy] = None,
95-
context_managers: typing.Optional[typing.List[typing.Callable[[], typing.AsyncContextManager[None]]]] = None,
96+
invocation_context_managers: typing.Optional[
97+
typing.List[typing.Callable[[], typing.AsyncContextManager[None]]]
98+
] = None,
9699
):
97100
self.service_tag = ServiceTag("workflow", name, description, metadata)
98101
self.handlers = {}
@@ -103,7 +106,7 @@ def __init__(
103106
self.enable_lazy_state = enable_lazy_state
104107
self.ingress_private = ingress_private
105108
self.invocation_retry_policy = invocation_retry_policy
106-
self.context_managers = context_managers
109+
self.context_managers = invocation_context_managers
107110

108111
@property
109112
def name(self):
@@ -127,7 +130,9 @@ def main(
127130
enable_lazy_state: typing.Optional[bool] = None,
128131
ingress_private: typing.Optional[bool] = None,
129132
invocation_retry_policy: typing.Optional[InvocationRetryPolicy] = None,
130-
context_managers: typing.Optional[typing.List[typing.Callable[[], typing.AsyncContextManager[None]]]] = None,
133+
invocation_context_managers: typing.Optional[
134+
typing.List[typing.Callable[[], typing.AsyncContextManager[None]]]
135+
] = None,
131136
) -> typing.Callable[[T], T]:
132137
"""
133138
Mark this handler as a workflow entry point.
@@ -185,7 +190,7 @@ def main(
185190
enable_lazy_state=enable_lazy_state,
186191
ingress_private=ingress_private,
187192
invocation_retry_policy=invocation_retry_policy,
188-
context_managers=context_managers,
193+
invocation_context_managers=invocation_context_managers,
189194
)
190195

191196
def handler(
@@ -203,7 +208,9 @@ def handler(
203208
enable_lazy_state: typing.Optional[bool] = None,
204209
ingress_private: typing.Optional[bool] = None,
205210
invocation_retry_policy: typing.Optional[InvocationRetryPolicy] = None,
206-
context_managers: typing.Optional[typing.List[typing.Callable[[], typing.AsyncContextManager[None]]]] = None,
211+
invocation_context_managers: typing.Optional[
212+
typing.List[typing.Callable[[], typing.AsyncContextManager[None]]]
213+
] = None,
207214
) -> typing.Callable[[T], T]:
208215
"""
209216
Decorator for defining a handler function.
@@ -244,6 +251,7 @@ def handler(
244251
NOTE: You can set this field only if you register this service against restate-server >= 1.4,
245252
otherwise the service discovery will fail.
246253
invocation_retry_policy (InvocationRetryPolicy, optional): Retry policy applied for all invocations to this handler.
254+
invocation_context_managers (List[Callable[[], AsyncContextManager[None]]], optional): List of context manager factories that will be applied to each invocation of this handler.
247255
"""
248256
return self._add_handler(
249257
name,
@@ -261,7 +269,7 @@ def handler(
261269
enable_lazy_state,
262270
ingress_private,
263271
invocation_retry_policy,
264-
context_managers,
272+
invocation_context_managers,
265273
)
266274

267275
# pylint: disable=R0914
@@ -282,7 +290,9 @@ def _add_handler(
282290
enable_lazy_state: typing.Optional[bool] = None,
283291
ingress_private: typing.Optional[bool] = None,
284292
invocation_retry_policy: typing.Optional["InvocationRetryPolicy"] = None,
285-
context_managers: typing.Optional[typing.List[typing.Callable[[], typing.AsyncContextManager[None]]]] = None,
293+
invocation_context_managers: typing.Optional[
294+
typing.List[typing.Callable[[], typing.AsyncContextManager[None]]]
295+
] = None,
286296
) -> typing.Callable[[T], T]:
287297
"""
288298
Decorator for defining a handler function.
@@ -350,8 +360,8 @@ def wrapped(*args, **kwargs):
350360
signature = inspect.signature(fn, eval_str=True)
351361
description = inspect.getdoc(fn)
352362
combined_context_managers = (
353-
(self.context_managers or []) + (context_managers or [])
354-
if self.context_managers or context_managers
363+
(self.context_managers or []) + (invocation_context_managers or [])
364+
if self.context_managers or invocation_context_managers
355365
else None
356366
)
357367
handler = make_handler(

tests/ext.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ async def my_resource_manager():
5555
yield "hello"
5656

5757

58-
@greeter.handler(context_managers=[my_resource_manager])
58+
@greeter.handler(invocation_context_managers=[my_resource_manager])
5959
async def greet_with_cm(ctx: Context, name: str) -> str:
6060
return my_resource_manager.value
6161

0 commit comments

Comments
 (0)