|
1 | 1 | import asyncio |
2 | 2 | import os |
| 3 | +import logging |
3 | 4 |
|
4 | 5 | import nest_asyncio |
5 | 6 | from dotenv import load_dotenv |
|
8 | 9 | from tc_temporal_backend.client import TemporalClient |
9 | 10 | from tc_temporal_backend.schema.hivemind import HivemindQueryPayload |
10 | 11 | from temporalio.common import RetryPolicy |
| 12 | +from temporalio.client import WorkflowFailureError |
11 | 13 |
|
12 | 14 | nest_asyncio.apply() |
13 | 15 |
|
@@ -42,13 +44,30 @@ async def query(self, query: str) -> str | None: |
42 | 44 | payload.workflow_id = self.workflow_id |
43 | 45 |
|
44 | 46 | hivemind_queue = self.load_hivemind_queue() |
45 | | - result = await client.execute_workflow( |
46 | | - "HivemindWorkflow", |
47 | | - payload, |
48 | | - id=f"hivemind-query-{self.community_id}-{self.workflow_id}", |
49 | | - task_queue=hivemind_queue, |
50 | | - retry_policy=RetryPolicy(maximum_attempts=3), |
51 | | - ) |
| 47 | + try: |
| 48 | + result = await client.execute_workflow( |
| 49 | + "HivemindWorkflow", |
| 50 | + payload, |
| 51 | + id=f"hivemind-query-{self.community_id}-{self.workflow_id}", |
| 52 | + task_queue=hivemind_queue, |
| 53 | + retry_policy=RetryPolicy(maximum_attempts=3), |
| 54 | + ) |
| 55 | + except WorkflowFailureError as e: |
| 56 | + logging.error(f"WorkflowFailureError: {e} for workflow {self.workflow_id}", exc_info=True) |
| 57 | + return None |
| 58 | + except Exception as e: |
| 59 | + logging.error(f"Exception: {e} for workflow {self.workflow_id}", exc_info=True) |
| 60 | + return None |
| 61 | + |
| 62 | + # Normalize Temporal failure-shaped responses that may be returned as data |
| 63 | + if isinstance(result, dict) and ( |
| 64 | + "workflowExecutionFailedEventAttributes" in result or "failure" in result |
| 65 | + ): |
| 66 | + logging.error(f"WorkflowFailureError: {result} for workflow {self.workflow_id}", exc_info=True) |
| 67 | + return None |
| 68 | + if isinstance(result, str) and "workflowExecutionFailedEventAttributes" in result: |
| 69 | + logging.error(f"WorkflowFailureError: {result} for workflow {self.workflow_id}", exc_info=True) |
| 70 | + return None |
52 | 71 |
|
53 | 72 | return result |
54 | 73 |
|
|
0 commit comments