|
| 1 | +from datetime import timedelta |
| 2 | + |
| 3 | +import xray |
| 4 | +from temporalio import workflow |
| 5 | +from temporalio.workflow import NexusClient |
| 6 | + |
| 7 | +from nexus.service.interface import ( |
| 8 | + EchoInput, |
| 9 | + EchoOutput, |
| 10 | + HelloInput, |
| 11 | + HelloOutput, |
| 12 | + MyNexusService, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +class CallerWorkflowBase: |
| 17 | + def __init__(self): |
| 18 | + self.nexus_client = NexusClient( |
| 19 | + MyNexusService, # or string name "my-nexus-service", |
| 20 | + "my-nexus-endpoint-name", |
| 21 | + schedule_to_close_timeout=timedelta(seconds=30), |
| 22 | + ) |
| 23 | + |
| 24 | + |
| 25 | +@workflow.defn |
| 26 | +class EchoCallerWorkflow(CallerWorkflowBase): |
| 27 | + @xray.start_as_current_workflow_method_span() |
| 28 | + @workflow.run |
| 29 | + async def run(self, message: str) -> EchoOutput: |
| 30 | + op_output = await self.nexus_client.execute_operation( |
| 31 | + MyNexusService.echo, |
| 32 | + EchoInput(message), |
| 33 | + ) |
| 34 | + return op_output |
| 35 | + |
| 36 | + |
| 37 | +@workflow.defn |
| 38 | +class Echo2CallerWorkflow(CallerWorkflowBase): |
| 39 | + @xray.start_as_current_workflow_method_span() |
| 40 | + @workflow.run |
| 41 | + async def run(self, message: str) -> EchoOutput: |
| 42 | + op_output = await self.nexus_client.execute_operation( |
| 43 | + MyNexusService.echo2, |
| 44 | + EchoInput(message), |
| 45 | + ) |
| 46 | + return op_output |
| 47 | + |
| 48 | + |
| 49 | +@workflow.defn |
| 50 | +class Echo3CallerWorkflow(CallerWorkflowBase): |
| 51 | + @xray.start_as_current_workflow_method_span() |
| 52 | + @workflow.run |
| 53 | + async def run(self, message: str) -> EchoOutput: |
| 54 | + op_output = await self.nexus_client.execute_operation( |
| 55 | + MyNexusService.echo3, |
| 56 | + EchoInput(message), |
| 57 | + ) |
| 58 | + return op_output |
| 59 | + |
| 60 | + |
| 61 | +@workflow.defn |
| 62 | +class HelloCallerWorkflow(CallerWorkflowBase): |
| 63 | + @xray.start_as_current_workflow_method_span() |
| 64 | + @workflow.run |
| 65 | + async def run(self, name: str) -> HelloOutput: |
| 66 | + # TODO: Java returns a handle immediately. The handle has a blocking method to |
| 67 | + # wait until the operation has started (i.e. initial Nexus RPC response is |
| 68 | + # available, so operation ID is available in the case of an async operation). |
| 69 | + handle = await self.nexus_client.start_operation( |
| 70 | + MyNexusService.hello, |
| 71 | + HelloInput(name), |
| 72 | + ) |
| 73 | + op_output = await handle |
| 74 | + return op_output |
| 75 | + |
| 76 | + |
| 77 | +@workflow.defn |
| 78 | +class Hello2CallerWorkflow(CallerWorkflowBase): |
| 79 | + @xray.start_as_current_workflow_method_span() |
| 80 | + @workflow.run |
| 81 | + async def run(self, name: str) -> HelloOutput: |
| 82 | + handle = await self.nexus_client.start_operation( |
| 83 | + MyNexusService.hello2, |
| 84 | + HelloInput(name), |
| 85 | + ) |
| 86 | + op_output = await handle |
| 87 | + return op_output |
| 88 | + |
| 89 | + |
| 90 | +@workflow.defn |
| 91 | +class Hello3CallerWorkflow(CallerWorkflowBase): |
| 92 | + @xray.start_as_current_workflow_method_span() |
| 93 | + @workflow.run |
| 94 | + async def run(self, name: str) -> HelloOutput: |
| 95 | + handle = await self.nexus_client.start_operation( |
| 96 | + MyNexusService.hello3, |
| 97 | + HelloInput(name), |
| 98 | + ) |
| 99 | + op_output = await handle |
| 100 | + return op_output |
0 commit comments