|
| 1 | +from datetime import timedelta |
| 2 | + |
| 3 | +import xray |
| 4 | +from temporalio import workflow |
| 5 | +from temporalio.exceptions import FailureError |
| 6 | +from temporalio.workflow import NexusClient |
| 7 | + |
| 8 | +from hello_nexus.handler.nexus_service import ( |
| 9 | + EchoInput, |
| 10 | + EchoOutput, |
| 11 | + HelloInput, |
| 12 | + HelloOutput, |
| 13 | + MyNexusService, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +class CallerWorkflowBase: |
| 18 | + def __init__(self): |
| 19 | + self.nexus_client = NexusClient( |
| 20 | + MyNexusService, # or string name "my-nexus-service", |
| 21 | + "my-nexus-endpoint-name-python", |
| 22 | + schedule_to_close_timeout=timedelta(seconds=30), |
| 23 | + ) |
| 24 | + |
| 25 | + |
| 26 | +@workflow.defn |
| 27 | +class EchoCallerWorkflow(CallerWorkflowBase): |
| 28 | + @xray.start_as_current_workflow_method_span() |
| 29 | + @workflow.run |
| 30 | + async def run(self, message: str) -> EchoOutput: |
| 31 | + op_output = await self.nexus_client.execute_operation( |
| 32 | + MyNexusService.echo, |
| 33 | + EchoInput(message), |
| 34 | + ) |
| 35 | + return op_output |
| 36 | + |
| 37 | + |
| 38 | +@workflow.defn |
| 39 | +class Echo2CallerWorkflow(CallerWorkflowBase): |
| 40 | + @xray.start_as_current_workflow_method_span() |
| 41 | + @workflow.run |
| 42 | + async def run(self, message: str) -> EchoOutput: |
| 43 | + op_output = await self.nexus_client.execute_operation( |
| 44 | + MyNexusService.echo2, |
| 45 | + EchoInput(message), |
| 46 | + ) |
| 47 | + return op_output |
| 48 | + |
| 49 | + |
| 50 | +@workflow.defn |
| 51 | +class Echo3CallerWorkflow(CallerWorkflowBase): |
| 52 | + @xray.start_as_current_workflow_method_span() |
| 53 | + @workflow.run |
| 54 | + async def run(self, message: str) -> EchoOutput: |
| 55 | + op_output = await self.nexus_client.execute_operation( |
| 56 | + MyNexusService.echo3, |
| 57 | + EchoInput(message), |
| 58 | + ) |
| 59 | + return op_output |
| 60 | + |
| 61 | + |
| 62 | +@workflow.defn |
| 63 | +class HelloCallerWorkflow(CallerWorkflowBase): |
| 64 | + @xray.start_as_current_workflow_method_span() |
| 65 | + @workflow.run |
| 66 | + async def run(self, name: str) -> HelloOutput: |
| 67 | + handle = await self.nexus_client.start_operation( |
| 68 | + MyNexusService.hello, |
| 69 | + HelloInput(name), |
| 70 | + ) |
| 71 | + assert handle.cancel() |
| 72 | + try: |
| 73 | + await handle |
| 74 | + except FailureError: |
| 75 | + handle = await self.nexus_client.start_operation( |
| 76 | + MyNexusService.hello, |
| 77 | + HelloInput(name), |
| 78 | + ) |
| 79 | + result = await handle |
| 80 | + return result |
| 81 | + raise AssertionError("Expected Nexus operation to be cancelled") |
| 82 | + |
| 83 | + |
| 84 | +@workflow.defn |
| 85 | +class Hello2CallerWorkflow(CallerWorkflowBase): |
| 86 | + @xray.start_as_current_workflow_method_span() |
| 87 | + @workflow.run |
| 88 | + async def run(self, name: str) -> HelloOutput: |
| 89 | + handle = await self.nexus_client.start_operation( |
| 90 | + MyNexusService.hello2, |
| 91 | + HelloInput(name), |
| 92 | + ) |
| 93 | + return await handle |
0 commit comments