|
| 1 | +""" |
| 2 | +Notes: |
| 3 | +
|
| 4 | +Sync operations: |
| 5 | +--------------- |
| 6 | +Implementations are free to make arbitrary network calls, or perform CPU-bound |
| 7 | +computations such as this one. Total execution duration must not exceed 10s. To |
| 8 | +perform Temporal client calls such as signaling/querying/listing workflows, use |
| 9 | +self.client. |
| 10 | +
|
| 11 | +
|
| 12 | +Workflow operations: |
| 13 | +--------------------- |
| 14 | +The task queue defaults to the task queue being used by the Nexus worker. |
| 15 | +""" |
| 16 | + |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +import nexusrpc.handler |
| 20 | +import temporalio.common |
| 21 | +import temporalio.nexus.handler |
| 22 | + |
| 23 | +from hello_nexus.handler.dbclient import MyDBClient |
| 24 | +from hello_nexus.handler.workflows import HelloWorkflow |
| 25 | +from hello_nexus.service import interface |
| 26 | +from hello_nexus.service.interface import ( |
| 27 | + EchoInput, |
| 28 | + EchoOutput, |
| 29 | + HelloInput, |
| 30 | + HelloOutput, |
| 31 | +) |
| 32 | + |
| 33 | + |
| 34 | +# Inheriting from the protocol here is optional. Users who do it will get the |
| 35 | +# operation definition itself type-checked in situ against the interface (*). |
| 36 | +# Call-sites using instances of the operation are always type-checked. |
| 37 | +# |
| 38 | +# (*) However, in VSCode/Pyright this is done only when type-checking is set to |
| 39 | +# 'strict'. |
| 40 | +class EchoOperation(nexusrpc.handler.Operation[EchoInput, EchoOutput]): |
| 41 | + def __init__(self, service: MyNexusService): |
| 42 | + self.service = service |
| 43 | + |
| 44 | + async def start( |
| 45 | + self, input: EchoInput, options: nexusrpc.handler.StartOperationOptions |
| 46 | + ) -> EchoOutput: |
| 47 | + return EchoOutput(message=f"Echo {input.message}!") |
| 48 | + |
| 49 | + async def cancel( |
| 50 | + self, token: str, options: nexusrpc.handler.CancelOperationOptions |
| 51 | + ) -> None: |
| 52 | + raise NotImplementedError |
| 53 | + |
| 54 | + async def fetch_info( |
| 55 | + self, token: str, options: nexusrpc.handler.FetchOperationInfoOptions |
| 56 | + ) -> nexusrpc.handler.OperationInfo: |
| 57 | + raise NotImplementedError |
| 58 | + |
| 59 | + async def fetch_result( |
| 60 | + self, token: str, options: nexusrpc.handler.FetchOperationResultOptions |
| 61 | + ) -> EchoOutput: |
| 62 | + raise NotImplementedError |
| 63 | + |
| 64 | + |
| 65 | +# Inheriting from the protocol here is optional. Users who do it will get the |
| 66 | +# operation definition itself type-checked in situ against the interface (*). |
| 67 | +# Call-sites using instances of the operation are always type-checked. |
| 68 | +# |
| 69 | +# (*) However, in VSCode/Pyright this is done only when type-checking is set to |
| 70 | +# 'strict'. |
| 71 | +class HelloOperation: # (nexusrpc.handler.Operation[HelloInput, HelloOutput]): |
| 72 | + def __init__(self, service: "MyNexusService"): |
| 73 | + self.service = service |
| 74 | + |
| 75 | + async def start( |
| 76 | + self, input: HelloInput, options: nexusrpc.handler.StartOperationOptions |
| 77 | + ) -> temporalio.nexus.handler.AsyncWorkflowOperationResult[HelloOutput]: |
| 78 | + self.service.db_client.execute("<some query>") |
| 79 | + workflow_id = "default-workflow-id" |
| 80 | + return await temporalio.nexus.handler.start_workflow( |
| 81 | + HelloWorkflow.run, |
| 82 | + input, |
| 83 | + id=workflow_id, |
| 84 | + options=options, |
| 85 | + ) |
| 86 | + |
| 87 | + async def cancel( |
| 88 | + self, token: str, options: nexusrpc.handler.CancelOperationOptions |
| 89 | + ) -> None: |
| 90 | + return await temporalio.nexus.handler.cancel_workflow(token, options) |
| 91 | + |
| 92 | + async def fetch_info( |
| 93 | + self, token: str, options: nexusrpc.handler.FetchOperationInfoOptions |
| 94 | + ) -> nexusrpc.handler.OperationInfo: |
| 95 | + return await temporalio.nexus.handler.fetch_workflow_info(token, options) |
| 96 | + |
| 97 | + async def fetch_result( |
| 98 | + self, token: str, options: nexusrpc.handler.FetchOperationResultOptions |
| 99 | + ) -> HelloOutput: |
| 100 | + return await temporalio.nexus.handler.fetch_workflow_result(token, options) |
| 101 | + |
| 102 | + |
| 103 | +class EchoOperation3(nexusrpc.handler.AbstractOperation[EchoInput, EchoOutput]): |
| 104 | + async def start( |
| 105 | + self, input: EchoInput, options: nexusrpc.handler.StartOperationOptions |
| 106 | + ) -> EchoOutput: |
| 107 | + return EchoOutput(message=f"Echo {input.message}! [from base class variant]") |
| 108 | + |
| 109 | + |
| 110 | +@nexusrpc.handler.service(interface=interface.MyNexusService) |
| 111 | +class MyNexusService: |
| 112 | + def __init__(self, db_client: MyDBClient): |
| 113 | + # An example of something that might be held by the service instance. |
| 114 | + self.db_client = db_client |
| 115 | + |
| 116 | + # -------------------------------------------------------------------------- |
| 117 | + # Operations defined by explicitly implementing the Operation interface. |
| 118 | + # |
| 119 | + |
| 120 | + @nexusrpc.handler.operation |
| 121 | + def echo(self) -> nexusrpc.handler.Operation[EchoInput, EchoOutput]: |
| 122 | + return EchoOperation(self) |
| 123 | + |
| 124 | + @nexusrpc.handler.operation |
| 125 | + def hello(self) -> nexusrpc.handler.Operation[HelloInput, HelloOutput]: |
| 126 | + return HelloOperation(self) |
| 127 | + |
| 128 | + @nexusrpc.handler.operation |
| 129 | + def echo3(self) -> nexusrpc.handler.Operation[EchoInput, EchoOutput]: |
| 130 | + return EchoOperation3() |
| 131 | + |
| 132 | + # -------------------------------------------------------------------------- |
| 133 | + # Operations defined by providing the start method only, using the |
| 134 | + # "shorthand" decorators. |
| 135 | + # |
| 136 | + # Note that a start method defined this way has access to the service |
| 137 | + # instance, but not to the operation instance (users who need the latter |
| 138 | + # should implement the Operation interface directly). |
| 139 | + |
| 140 | + @nexusrpc.handler.sync_operation |
| 141 | + async def echo2( |
| 142 | + self, input: EchoInput, _: nexusrpc.handler.StartOperationOptions |
| 143 | + ) -> EchoOutput: |
| 144 | + return EchoOutput(message=f"Echo {input.message} [via shorthand]!") |
| 145 | + |
| 146 | + # -------------------------------------------------------------------------- |
| 147 | + # Operations defined by providing the start method only, using the |
| 148 | + # "shorthand" decorators. |
| 149 | + # |
| 150 | + # Note that a start method defined this way has access to the service |
| 151 | + # instance, but not to the operation instance (users who need the latter |
| 152 | + # should implement the Operation interface directly). |
| 153 | + |
| 154 | + @temporalio.nexus.handler.workflow_operation |
| 155 | + async def hello2( |
| 156 | + self, input: HelloInput, options: nexusrpc.handler.StartOperationOptions |
| 157 | + ) -> temporalio.nexus.handler.AsyncWorkflowOperationResult[HelloOutput]: |
| 158 | + self.db_client.execute("<some query>") |
| 159 | + workflow_id = "default-workflow-id" |
| 160 | + input.name += " [via shorthand]" |
| 161 | + return await temporalio.nexus.handler.start_workflow( |
| 162 | + HelloWorkflow.run, |
| 163 | + input, |
| 164 | + id=workflow_id, |
| 165 | + options=options, |
| 166 | + ) |
| 167 | + |
| 168 | + |
| 169 | +if __name__ == "__main__": |
| 170 | + # Check run-time type annotations resulting from the decorators. |
| 171 | + service = MyNexusService(MyDBClient()) |
| 172 | + print("echo:", temporalio.common._type_hints_from_func(service.echo2().start)) |
| 173 | + print( |
| 174 | + "hello:", temporalio.common._type_hints_from_func(service.hello2().fetch_result) |
| 175 | + ) |
0 commit comments