|
| 1 | +import asyncio |
| 2 | +from typing import Optional |
| 3 | + |
| 4 | +from cadence.api.v1.common_pb2 import Payload |
| 5 | +from cadence.api.v1.service_worker_pb2 import PollForDecisionTaskRequest, PollForDecisionTaskResponse, \ |
| 6 | + RespondDecisionTaskFailedRequest |
| 7 | +from cadence.api.v1.tasklist_pb2 import TaskList, TaskListKind |
| 8 | +from cadence.api.v1.workflow_pb2 import DecisionTaskFailedCause |
| 9 | +from cadence.client import Client |
| 10 | +from cadence.worker._poller import Poller |
| 11 | +from cadence.worker._types import WorkerOptions, _LONG_POLL_TIMEOUT |
| 12 | + |
| 13 | + |
| 14 | +class DecisionWorker: |
| 15 | + def __init__(self, client: Client, task_list: str, options: WorkerOptions): |
| 16 | + self._client = client |
| 17 | + self._task_list = task_list |
| 18 | + self._identity = options["identity"] |
| 19 | + permits = asyncio.Semaphore(options["max_concurrent_decision_task_execution_size"]) |
| 20 | + self._poller = Poller[PollForDecisionTaskResponse](options["decision_task_pollers"], permits, self._poll, self._execute) |
| 21 | + # TODO: Sticky poller, actually running workflows, etc. |
| 22 | + |
| 23 | + async def run(self): |
| 24 | + await self._poller.run() |
| 25 | + |
| 26 | + async def _poll(self) -> Optional[PollForDecisionTaskResponse]: |
| 27 | + task: PollForDecisionTaskResponse = await self._client.worker_stub.PollForDecisionTask(PollForDecisionTaskRequest( |
| 28 | + domain=self._client.domain, |
| 29 | + task_list=TaskList(name=self._task_list,kind=TaskListKind.TASK_LIST_KIND_NORMAL), |
| 30 | + identity=self._identity, |
| 31 | + ), timeout=_LONG_POLL_TIMEOUT) |
| 32 | + |
| 33 | + if task.task_token: |
| 34 | + return task |
| 35 | + else: |
| 36 | + return None |
| 37 | + |
| 38 | + |
| 39 | + async def _execute(self, task: PollForDecisionTaskResponse): |
| 40 | + await self._client.worker_stub.RespondDecisionTaskFailed(RespondDecisionTaskFailedRequest( |
| 41 | + task_token=task.task_token, |
| 42 | + cause=DecisionTaskFailedCause.DECISION_TASK_FAILED_CAUSE_UNHANDLED_DECISION, |
| 43 | + identity=self._identity, |
| 44 | + details=Payload(data=b'not implemented') |
| 45 | + )) |
| 46 | + |
0 commit comments