|
7 | 7 | import math |
8 | 8 | from abc import ABC, abstractmethod |
9 | 9 | from datetime import datetime, timedelta |
10 | | -from typing import Any, Callable, Generator, Generic, Optional, Type, TypeVar, Union, overload |
11 | | -import uuid |
| 10 | +from typing import Any, Callable, Generator, Generic, Optional, TypeVar, Union |
12 | 11 |
|
13 | | -from durabletask.entities import DurableEntity, EntityInstanceId, EntityLock |
14 | | -from durabletask.internal import shared |
15 | | -from durabletask.internal.entity_state_shim import StateShim |
| 12 | +from durabletask.entities import DurableEntity, EntityInstanceId, EntityLock, EntityContext |
16 | 13 | import durabletask.internal.helpers as pbh |
17 | 14 | import durabletask.internal.orchestrator_service_pb2 as pb |
18 | 15 |
|
19 | 16 | T = TypeVar('T') |
20 | 17 | TInput = TypeVar('TInput') |
21 | 18 | TOutput = TypeVar('TOutput') |
22 | | -TState = TypeVar("TState") |
23 | 19 |
|
24 | 20 |
|
25 | 21 | class OrchestrationContext(ABC): |
@@ -515,103 +511,6 @@ def task_id(self) -> int: |
515 | 511 | return self._task_id |
516 | 512 |
|
517 | 513 |
|
518 | | -class EntityContext: |
519 | | - def __init__(self, orchestration_id: str, operation: str, state: StateShim, entity_id: EntityInstanceId): |
520 | | - self._orchestration_id = orchestration_id |
521 | | - self._operation = operation |
522 | | - self._state = state |
523 | | - self._entity_id = entity_id |
524 | | - |
525 | | - @property |
526 | | - def orchestration_id(self) -> str: |
527 | | - """Get the ID of the orchestration instance that scheduled this entity. |
528 | | -
|
529 | | - Returns |
530 | | - ------- |
531 | | - str |
532 | | - The ID of the current orchestration instance. |
533 | | - """ |
534 | | - return self._orchestration_id |
535 | | - |
536 | | - @property |
537 | | - def operation(self) -> str: |
538 | | - """Get the operation associated with this entity invocation. |
539 | | -
|
540 | | - The operation is a string that identifies the specific action being |
541 | | - performed on the entity. It can be used to distinguish between |
542 | | - multiple operations that are part of the same entity invocation. |
543 | | -
|
544 | | - Returns |
545 | | - ------- |
546 | | - str |
547 | | - The operation associated with this entity invocation. |
548 | | - """ |
549 | | - return self._operation |
550 | | - |
551 | | - @overload |
552 | | - def get_state(self, intended_type: Type[TState], default: TState) -> TState: |
553 | | - ... |
554 | | - |
555 | | - @overload |
556 | | - def get_state(self, intended_type: Type[TState]) -> Optional[TState]: |
557 | | - ... |
558 | | - |
559 | | - @overload |
560 | | - def get_state(self, intended_type: None = None, default: Any = None) -> Any: |
561 | | - ... |
562 | | - |
563 | | - def get_state(self, intended_type: Optional[Type[TState]] = None, default: Optional[TState] = None) -> Optional[TState] | Any: |
564 | | - return self._state.get_state(intended_type, default) |
565 | | - |
566 | | - def set_state(self, new_state: Any): |
567 | | - self._state.set_state(new_state) |
568 | | - |
569 | | - def signal_entity(self, entity_instance_id: EntityInstanceId, operation: str, input: Optional[Any] = None) -> None: |
570 | | - encoded_input = shared.to_json(input) if input is not None else None |
571 | | - self._state.add_operation_action( |
572 | | - pb.OperationAction( |
573 | | - sendSignal=pb.SendSignalAction( |
574 | | - instanceId=str(entity_instance_id), |
575 | | - name=operation, |
576 | | - input=pbh.get_string_value(encoded_input), |
577 | | - scheduledTime=None, |
578 | | - requestTime=None, |
579 | | - parentTraceContext=None, |
580 | | - ) |
581 | | - ) |
582 | | - ) |
583 | | - |
584 | | - def schedule_new_orchestration(self, orchestration_name: str, input: Optional[Any] = None, instance_id: Optional[str] = None) -> str: |
585 | | - encoded_input = shared.to_json(input) if input is not None else None |
586 | | - if not instance_id: |
587 | | - instance_id = uuid.uuid4().hex |
588 | | - self._state.add_operation_action( |
589 | | - pb.OperationAction( |
590 | | - startNewOrchestration=pb.StartNewOrchestrationAction( |
591 | | - instanceId=instance_id, |
592 | | - name=orchestration_name, |
593 | | - input=pbh.get_string_value(encoded_input), |
594 | | - version=None, |
595 | | - scheduledTime=None, |
596 | | - requestTime=None, |
597 | | - parentTraceContext=None |
598 | | - ) |
599 | | - ) |
600 | | - ) |
601 | | - return instance_id |
602 | | - |
603 | | - @property |
604 | | - def entity_id(self) -> EntityInstanceId: |
605 | | - """Get the ID of the entity instance. |
606 | | -
|
607 | | - Returns |
608 | | - ------- |
609 | | - str |
610 | | - The ID of the current entity instance. |
611 | | - """ |
612 | | - return self._entity_id |
613 | | - |
614 | | - |
615 | 514 | # Orchestrators are generators that yield tasks and receive/return any type |
616 | 515 | Orchestrator = Callable[[OrchestrationContext, TInput], Union[Generator[Task, Any, Any], TOutput]] |
617 | 516 |
|
|
0 commit comments