|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +from typing import Any, Callable, Optional |
| 5 | + |
| 6 | +from durabletask.entities import EntityContext |
| 7 | + |
| 8 | +from .orchestration_context import accepts_two_positional_args |
| 9 | + |
| 10 | + |
| 11 | +class DurableEntityContext: |
| 12 | + """Azure Functions-style entity context (v1-compatible). |
| 13 | +
|
| 14 | + Wraps a durabletask :class:`~durabletask.entities.EntityContext` (and the |
| 15 | + operation input) and exposes the v1 ``DurableEntityContext`` API. It is |
| 16 | + delivered to one-argument entity functions (``def entity(context):``). |
| 17 | + durabletask-native two-argument entity functions |
| 18 | + (``def entity(ctx, input):``) and class-based entities are used directly. |
| 19 | + """ |
| 20 | + |
| 21 | + def __init__(self, ctx: EntityContext, operation_input: Any = None): |
| 22 | + self._ctx = ctx |
| 23 | + self._input = operation_input |
| 24 | + self._result: Any = None |
| 25 | + |
| 26 | + # -- identity ------------------------------------------------------------ |
| 27 | + @property |
| 28 | + def entity_name(self) -> str: |
| 29 | + """Get the entity name.""" |
| 30 | + return self._ctx.entity_id.entity |
| 31 | + |
| 32 | + @property |
| 33 | + def entity_key(self) -> str: |
| 34 | + """Get the entity key.""" |
| 35 | + return self._ctx.entity_id.key |
| 36 | + |
| 37 | + @property |
| 38 | + def operation_name(self) -> str: |
| 39 | + """Get the current operation name.""" |
| 40 | + return self._ctx.operation |
| 41 | + |
| 42 | + @property |
| 43 | + def is_newly_constructed(self) -> bool: |
| 44 | + """Whether the entity was newly constructed. |
| 45 | +
|
| 46 | + The v1 semantics of this flag were unspecified; it is always ``False``. |
| 47 | + """ |
| 48 | + return False |
| 49 | + |
| 50 | + # -- input / state / result --------------------------------------------- |
| 51 | + def get_input(self, expected_type: Optional[type] = None) -> Any: |
| 52 | + """Get the input for the current operation. |
| 53 | +
|
| 54 | + ``expected_type`` is accepted for v1 compatibility but the input is |
| 55 | + already deserialized by durabletask, so it is returned as-is. |
| 56 | + """ |
| 57 | + return self._input |
| 58 | + |
| 59 | + def get_state(self, |
| 60 | + initializer: Optional[Callable[[], Any]] = None, |
| 61 | + expected_type: Optional[type] = None) -> Any: |
| 62 | + """Get the current state of the entity. |
| 63 | +
|
| 64 | + Parameters |
| 65 | + ---------- |
| 66 | + initializer : Optional[Callable[[], Any]] |
| 67 | + A zero-argument callable providing the initial state when no state |
| 68 | + exists yet. |
| 69 | + expected_type : Optional[type] |
| 70 | + Optional type used to reconstruct the state. |
| 71 | + """ |
| 72 | + default = initializer() if callable(initializer) else None |
| 73 | + return self._ctx.get_state(expected_type, default) |
| 74 | + |
| 75 | + def set_state(self, state: Any) -> None: |
| 76 | + """Set the state of the entity.""" |
| 77 | + self._ctx.set_state(state) |
| 78 | + |
| 79 | + def set_result(self, result: Any) -> None: |
| 80 | + """Set the result (return value) of the current operation.""" |
| 81 | + self._result = result |
| 82 | + |
| 83 | + def resolve_result(self, fallback: Any) -> Any: |
| 84 | + """Return the value set via :meth:`set_result`, or ``fallback`` if unset.""" |
| 85 | + return self._result if self._result is not None else fallback |
| 86 | + |
| 87 | + def destruct_on_exit(self) -> None: |
| 88 | + """Delete this entity after the operation completes.""" |
| 89 | + self._ctx.set_state(None) |
| 90 | + |
| 91 | + |
| 92 | +def wrap_entity(fn: Callable[..., Any]) -> Callable[..., Any]: |
| 93 | + """Adapt a v1-style one-argument entity function to durabletask's ``(ctx, input)``. |
| 94 | +
|
| 95 | + Class-based entities and durabletask-native two-argument entity functions |
| 96 | + are returned unchanged. For a wrapped v1 entity, the operation result is |
| 97 | + taken from ``context.set_result(...)`` (falling back to the function's |
| 98 | + return value). |
| 99 | + """ |
| 100 | + if isinstance(fn, type): |
| 101 | + # Class-based entity: handled natively by durabletask. |
| 102 | + return fn |
| 103 | + if accepts_two_positional_args(fn): |
| 104 | + # durabletask-native (ctx, input) entity function. |
| 105 | + return fn |
| 106 | + |
| 107 | + def _wrapper(ctx: EntityContext, _input: Any = None) -> Any: |
| 108 | + adapter = DurableEntityContext(ctx, _input) |
| 109 | + returned = fn(adapter) |
| 110 | + return adapter.resolve_result(returned) |
| 111 | + |
| 112 | + _wrapper.__name__ = getattr(fn, "__name__", "entity") |
| 113 | + durable_entity_name = getattr(fn, "__durable_entity_name__", None) |
| 114 | + if durable_entity_name is not None: |
| 115 | + _wrapper.__durable_entity_name__ = durable_entity_name # type: ignore[attr-defined] |
| 116 | + return _wrapper |
0 commit comments