|
| 1 | +from datetime import datetime, timezone |
| 2 | +from typing import Any, Optional, Type, TypeVar, Union, overload |
| 3 | +from durabletask.entities.entity_instance_id import EntityInstanceId |
| 4 | + |
| 5 | +import durabletask.internal.orchestrator_service_pb2 as pb |
| 6 | + |
| 7 | +TState = TypeVar("TState") |
| 8 | + |
| 9 | + |
| 10 | +class EntityMetadata: |
| 11 | + """Class representing the metadata of a durable entity. |
| 12 | +
|
| 13 | + This class encapsulates the metadata information of a durable entity, allowing for |
| 14 | + easy access and manipulation of the entity's metadata within the Durable Task |
| 15 | + Framework. |
| 16 | +
|
| 17 | + Attributes: |
| 18 | + id (EntityInstanceId): The unique identifier of the entity instance. |
| 19 | + last_modified (datetime): The timestamp of the last modification to the entity. |
| 20 | + backlog_queue_size (int): The size of the backlog queue for the entity. |
| 21 | + locked_by (str): The identifier of the worker that currently holds the lock on the entity. |
| 22 | + includes_state (bool): Indicates whether the metadata includes the state of the entity. |
| 23 | + state (Optional[Any]): The current state of the entity, if included. |
| 24 | + """ |
| 25 | + |
| 26 | + def __init__(self, |
| 27 | + id: EntityInstanceId, |
| 28 | + last_modified: datetime, |
| 29 | + backlog_queue_size: int, |
| 30 | + locked_by: str, |
| 31 | + includes_state: bool, |
| 32 | + state: Optional[Any]): |
| 33 | + """Initializes a new instance of the EntityMetadata class. |
| 34 | +
|
| 35 | + Args: |
| 36 | + value: The initial state value of the entity. |
| 37 | + """ |
| 38 | + self.id = id |
| 39 | + self.last_modified = last_modified |
| 40 | + self.backlog_queue_size = backlog_queue_size |
| 41 | + self._locked_by = locked_by |
| 42 | + self.includes_state = includes_state |
| 43 | + self._state = state |
| 44 | + |
| 45 | + @staticmethod |
| 46 | + def from_entity_response(entity_response: pb.GetEntityResponse, includes_state: bool): |
| 47 | + entity_id = EntityInstanceId.parse(entity_response.entity.instanceId) |
| 48 | + if not entity_id: |
| 49 | + raise ValueError("Invalid entity instance ID in entity response.") |
| 50 | + entity_state = None |
| 51 | + if includes_state: |
| 52 | + entity_state = entity_response.entity.serializedState.value |
| 53 | + return EntityMetadata( |
| 54 | + id=entity_id, |
| 55 | + last_modified=entity_response.entity.lastModifiedTime.ToDatetime(timezone.utc), |
| 56 | + backlog_queue_size=entity_response.entity.backlogQueueSize, |
| 57 | + locked_by=entity_response.entity.lockedBy.value, |
| 58 | + includes_state=includes_state, |
| 59 | + state=entity_state |
| 60 | + ) |
| 61 | + |
| 62 | + @overload |
| 63 | + def get_state(self, intended_type: Type[TState]) -> Optional[TState]: |
| 64 | + ... |
| 65 | + |
| 66 | + @overload |
| 67 | + def get_state(self, intended_type: None = None) -> Any: |
| 68 | + ... |
| 69 | + |
| 70 | + def get_state(self, intended_type: Optional[Type[TState]] = None) -> Union[None, TState, Any]: |
| 71 | + """Get the current state of the entity, optionally converting it to a specified type.""" |
| 72 | + if intended_type is None or self._state is None: |
| 73 | + return self._state |
| 74 | + |
| 75 | + if isinstance(self._state, intended_type): |
| 76 | + return self._state |
| 77 | + |
| 78 | + try: |
| 79 | + return intended_type(self._state) # type: ignore[call-arg] |
| 80 | + except Exception as ex: |
| 81 | + raise TypeError( |
| 82 | + f"Could not convert state of type '{type(self._state).__name__}' to '{intended_type.__name__}'" |
| 83 | + ) from ex |
| 84 | + |
| 85 | + def get_locked_by(self) -> Optional[EntityInstanceId]: |
| 86 | + """Get the identifier of the worker that currently holds the lock on the entity. |
| 87 | +
|
| 88 | + Returns |
| 89 | + ------- |
| 90 | + str |
| 91 | + The identifier of the worker that currently holds the lock on the entity. |
| 92 | + """ |
| 93 | + if not self._locked_by: |
| 94 | + return None |
| 95 | + |
| 96 | + # Will throw ValueError if the format is invalid |
| 97 | + return EntityInstanceId.parse(self._locked_by) |
0 commit comments