22
22
from temporalio .service import RPCError , RPCStatusCode
23
23
from temporalio .worker import UnsandboxedWorkflowRunner , Worker
24
24
25
- NEXUS_ENDPOINT_NAME = "my-nexus-endpoint"
26
-
27
25
28
26
# -----------------------------------------------------------------------------
29
27
# Service interface
@@ -124,17 +122,28 @@ class MyCallerWorkflow:
124
122
synchronously or asynchronously.
125
123
"""
126
124
127
- def __init__ (self ) -> None :
125
+ @workflow .init
126
+ def __init__ (
127
+ self ,
128
+ response_type : ResponseType ,
129
+ should_cancel : bool ,
130
+ task_queue : str ,
131
+ ) -> None :
128
132
self .nexus_service = workflow .NexusClient (
129
133
service = MyService ,
130
- endpoint = NEXUS_ENDPOINT_NAME ,
134
+ endpoint = make_nexus_endpoint_name ( task_queue ) ,
131
135
schedule_to_close_timeout = timedelta (seconds = 10 ),
132
136
)
133
137
self ._nexus_operation_started = False
134
138
self ._proceed = False
135
139
136
140
@workflow .run
137
- async def run (self , response_type : ResponseType , should_cancel : bool ) -> str :
141
+ async def run (
142
+ self ,
143
+ response_type : ResponseType ,
144
+ should_cancel : bool ,
145
+ task_queue : str ,
146
+ ) -> str :
138
147
op_handle = await self .nexus_service .start_operation (
139
148
MyService .my_operation ,
140
149
MyInput (
@@ -193,10 +202,10 @@ async def test_sync_response(client: Client, should_attempt_cancel: bool):
193
202
task_queue = task_queue ,
194
203
workflow_runner = UnsandboxedWorkflowRunner (),
195
204
):
196
- await create_nexus_endpoint (NEXUS_ENDPOINT_NAME , task_queue , client )
205
+ await create_nexus_endpoint (task_queue , client )
197
206
wf_handle = await client .start_workflow (
198
207
MyCallerWorkflow .run ,
199
- args = [SyncResponse (), should_attempt_cancel ],
208
+ args = [SyncResponse (), should_attempt_cancel , task_queue ],
200
209
id = str (uuid .uuid4 ()),
201
210
task_queue = task_queue ,
202
211
)
@@ -217,12 +226,12 @@ async def test_async_response(client: Client):
217
226
):
218
227
operation_workflow_id = str (uuid .uuid4 ())
219
228
operation_workflow_handle = client .get_workflow_handle (operation_workflow_id )
220
- await create_nexus_endpoint (NEXUS_ENDPOINT_NAME , task_queue , client )
229
+ await create_nexus_endpoint (task_queue , client )
221
230
222
231
# Start the caller workflow
223
232
wf_handle = await client .start_workflow (
224
233
MyCallerWorkflow .run ,
225
- args = [AsyncResponse (operation_workflow_id ), False ],
234
+ args = [AsyncResponse (operation_workflow_id ), False , task_queue ],
226
235
id = str (uuid .uuid4 ()),
227
236
task_queue = task_queue ,
228
237
)
@@ -256,12 +265,12 @@ async def test_cancellation_of_async_response(client: Client):
256
265
):
257
266
operation_workflow_id = str (uuid .uuid4 ())
258
267
operation_workflow_handle = client .get_workflow_handle (operation_workflow_id )
259
- await create_nexus_endpoint (NEXUS_ENDPOINT_NAME , task_queue , client )
268
+ await create_nexus_endpoint (task_queue , client )
260
269
261
270
# Start the caller workflow
262
271
wf_handle = await client .start_workflow (
263
272
MyCallerWorkflow .run ,
264
- args = [AsyncResponse (operation_workflow_id ), True ],
273
+ args = [AsyncResponse (operation_workflow_id ), True , task_queue ],
265
274
id = str (uuid .uuid4 ()),
266
275
task_queue = task_queue ,
267
276
)
@@ -286,7 +295,14 @@ async def test_cancellation_of_async_response(client: Client):
286
295
assert wf_details .status == WorkflowExecutionStatus .CANCELED
287
296
288
297
289
- async def create_nexus_endpoint (name : str , task_queue : str , client : Client ) -> None :
298
+ def make_nexus_endpoint_name (task_queue : str ) -> str :
299
+ return f"nexus-endpoint-{ task_queue } "
300
+
301
+
302
+ async def create_nexus_endpoint (task_queue : str , client : Client ) -> None :
303
+ # In order to be able to create endpoints for different task queues without risk of
304
+ # name collision.
305
+ name = make_nexus_endpoint_name (task_queue )
290
306
try :
291
307
await client .operator_service .create_nexus_endpoint (
292
308
temporalio .api .operatorservice .v1 .CreateNexusEndpointRequest (
0 commit comments