|
| 1 | +from contextlib import contextmanager |
1 | 2 | from datetime import timedelta |
2 | 3 | from math import ceil |
3 | | -from typing import Optional, Any, Unpack, Type, cast |
| 4 | +from typing import Iterator, Optional, Any, Unpack, Type, cast |
4 | 5 |
|
5 | 6 | from cadence._internal.workflow.statemachine.decision_manager import DecisionManager |
6 | 7 | from cadence._internal.workflow.decisions_helper import DecisionsHelper |
7 | 8 | from cadence.api.v1.common_pb2 import ActivityType |
8 | 9 | from cadence.api.v1.decision_pb2 import ScheduleActivityTaskDecisionAttributes |
9 | 10 | from cadence.api.v1.tasklist_pb2 import TaskList, TaskListKind |
10 | | -from cadence.client import Client |
11 | 11 | from cadence.data_converter import DataConverter |
12 | 12 | from cadence.workflow import WorkflowContext, WorkflowInfo, ResultType, ActivityOptions |
13 | 13 |
|
14 | 14 |
|
15 | 15 | class Context(WorkflowContext): |
16 | 16 | def __init__( |
17 | 17 | self, |
18 | | - client: Client, |
19 | 18 | info: WorkflowInfo, |
20 | | - decision_helper: DecisionsHelper, |
21 | 19 | decision_manager: DecisionManager, |
22 | 20 | ): |
23 | | - self._client = client |
24 | 21 | self._info = info |
25 | 22 | self._replay_mode = True |
26 | 23 | self._replay_current_time_milliseconds: Optional[int] = None |
27 | | - self._decision_helper = decision_helper |
| 24 | + self._decision_helper = DecisionsHelper() |
28 | 25 | self._decision_manager = decision_manager |
29 | 26 |
|
30 | 27 | def info(self) -> WorkflowInfo: |
31 | 28 | return self._info |
32 | 29 |
|
33 | | - def client(self) -> Client: |
34 | | - return self._client |
35 | | - |
36 | 30 | def data_converter(self) -> DataConverter: |
37 | | - return self._client.data_converter |
| 31 | + return self.info().data_converter |
38 | 32 |
|
39 | 33 | async def execute_activity( |
40 | 34 | self, |
@@ -80,7 +74,7 @@ async def execute_activity( |
80 | 74 | schedule_attributes = ScheduleActivityTaskDecisionAttributes( |
81 | 75 | activity_id=activity_id, |
82 | 76 | activity_type=ActivityType(name=activity), |
83 | | - domain=self._client.domain, |
| 77 | + domain=self.info().workflow_domain, |
84 | 78 | task_list=TaskList(kind=TaskListKind.TASK_LIST_KIND_NORMAL, name=task_list), |
85 | 79 | input=activity_input, |
86 | 80 | schedule_to_close_timeout=_round_to_nearest_second(schedule_to_close), |
@@ -116,6 +110,12 @@ def get_replay_current_time_milliseconds(self) -> Optional[int]: |
116 | 110 | """Get the current replay time in milliseconds.""" |
117 | 111 | return self._replay_current_time_milliseconds |
118 | 112 |
|
| 113 | + @contextmanager |
| 114 | + def _activate(self) -> Iterator["Context"]: |
| 115 | + token = WorkflowContext._var.set(self) |
| 116 | + yield self |
| 117 | + WorkflowContext._var.reset(token) |
| 118 | + |
119 | 119 |
|
120 | 120 | def _round_to_nearest_second(delta: timedelta) -> timedelta: |
121 | 121 | return timedelta(seconds=ceil(delta.total_seconds())) |
0 commit comments