Skip to content

Commit 9538e1d

Browse files
committed
Cleanup
1 parent 5afcfc0 commit 9538e1d

6 files changed

Lines changed: 46 additions & 26 deletions

File tree

nexus_sync_operations/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
This sample shows how to create a Nexus service that is backed by a long-running workflow and
2-
exposes operations that use signals, queries, and updates against that workflow.
2+
exposes operations that execute updates and queries against that workflow. The long-running
3+
workflow, and the updates/queries are private implementation detail of the nexus service: the caller
4+
does not know how the operations are implemented.
35

46
### Sample directory structure
57

nexus_sync_operations/caller/workflows.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
"""
2+
This is a workflow that calls nexus operations. The caller does not have information about how these
3+
operations are implemented by the nexus service.
4+
"""
5+
16
from temporalio import workflow
27

38
from message_passing.introduction import Language
4-
from message_passing.introduction.workflows import (
5-
GetLanguagesInput,
6-
SetLanguageInput,
7-
)
9+
from message_passing.introduction.workflows import GetLanguagesInput, SetLanguageInput
810

911
with workflow.unsafe.imports_passed_through():
1012
from nexus_sync_operations.service import GreetingService

nexus_sync_operations/endpoint_description.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,3 @@
22
- operation: `get_languages`
33
- operation: `get_language`
44
- operation: `set_language`
5-
- operation: `set_language_using_activity`
6-
- operation: `approve`

nexus_sync_operations/handler/service_handler.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""
2-
This file demonstrates how to implement a Nexus service that is backed by a long-running
3-
workflow and exposes operations that perform signals, updates, and queries against that
4-
workflow.
2+
This file demonstrates how to implement a Nexus service that is backed by a long-running workflow
3+
and exposes operations that perform updates, and queries against that workflow.
54
"""
65

76
from __future__ import annotations
@@ -31,19 +30,16 @@ def __init__(
3130
async def create(cls, client: Client, task_queue: str) -> GreetingServiceHandler:
3231
# Obtain a workflow handle to the long-running workflow that backs this service, starting
3332
# the workflow if it is not already running.
34-
return cls(await cls._get_workflow_handle(client, task_queue))
35-
36-
@staticmethod
37-
async def _get_workflow_handle(
38-
client: Client, task_queue: str
39-
) -> WorkflowHandle[GreetingWorkflow, str]:
40-
return await client.start_workflow(
33+
wf_handle = await client.start_workflow(
4134
GreetingWorkflow.run,
4235
id="nexus-sync-operations-greeting-workflow",
4336
task_queue=task_queue,
4437
id_conflict_policy=WorkflowIDConflictPolicy.USE_EXISTING,
4538
)
39+
return cls(wf_handle)
4640

41+
# 👉 This is a handler for a nexus operation whose internal implementation involves executing a
42+
# query against a long-running workflow that is private to the nexus service.
4743
@nexusrpc.handler.sync_operation
4844
async def get_languages(
4945
self, ctx: nexusrpc.handler.StartOperationContext, input: GetLanguagesInput
@@ -52,12 +48,16 @@ async def get_languages(
5248
GreetingWorkflow.get_languages, input
5349
)
5450

51+
# 👉 This is a handler for a nexus operation whose internal implementation involves executing a
52+
# query against a long-running workflow that is private to the nexus service.
5553
@nexusrpc.handler.sync_operation
5654
async def get_language(
5755
self, ctx: nexusrpc.handler.StartOperationContext, input: None
5856
) -> Language:
5957
return await self.greeting_workflow_handle.query(GreetingWorkflow.get_language)
6058

59+
# 👉 This is a handler for a nexus operation whose internal implementation involves executing an
60+
# update against a long-running workflow that is private to the nexus service.
6161
@nexusrpc.handler.sync_operation
6262
async def set_language(
6363
self,

nexus_sync_operations/handler/worker.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from typing import Optional
44

55
from temporalio.client import Client
6+
from temporalio.common import WorkflowIDConflictPolicy
67
from temporalio.worker import Worker
78

89
from message_passing.introduction.activities import call_greeting_service
@@ -22,18 +23,29 @@ async def main(client: Optional[Client] = None):
2223
"localhost:7233",
2324
namespace=NAMESPACE,
2425
)
25-
greeting_service_handler = await GreetingServiceHandler.create(client, TASK_QUEUE)
2626

2727
async with Worker(
2828
client,
2929
task_queue=TASK_QUEUE,
3030
workflows=[GreetingWorkflow],
3131
activities=[call_greeting_service],
32-
nexus_service_handlers=[greeting_service_handler],
3332
):
34-
logging.info("Worker started, ctrl+c to exit")
35-
await interrupt_event.wait()
36-
logging.info("Shutting down")
33+
long_running_workflow_handle = await client.start_workflow(
34+
GreetingWorkflow.run,
35+
id="nexus-sync-operations-greeting-workflow",
36+
task_queue=TASK_QUEUE,
37+
id_conflict_policy=WorkflowIDConflictPolicy.USE_EXISTING,
38+
)
39+
async with Worker(
40+
client,
41+
task_queue=TASK_QUEUE,
42+
nexus_service_handlers=[
43+
GreetingServiceHandler(long_running_workflow_handle)
44+
],
45+
):
46+
logging.info("Worker started, ctrl+c to exit")
47+
await interrupt_event.wait()
48+
logging.info("Shutting down")
3749

3850

3951
if __name__ == "__main__":

nexus_sync_operations/service.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
"""
2+
This module defines a Nexus service that exposes three operations.
3+
4+
It is used by the nexus service handler to validate that the operation handlers implement the
5+
correct input and output types, and by the caller workflow to create a type-safe client. It does not
6+
contain the implementation of the operations; see nexus_sync_operations.handler.service_handler for
7+
that.
8+
"""
9+
110
import nexusrpc
211

312
from message_passing.introduction import Language
4-
from message_passing.introduction.workflows import (
5-
GetLanguagesInput,
6-
SetLanguageInput,
7-
)
13+
from message_passing.introduction.workflows import GetLanguagesInput, SetLanguageInput
814

915

1016
@nexusrpc.service

0 commit comments

Comments
 (0)