|
1 | 1 | """ |
2 | | -Notes: |
| 2 | +This file demonstrates how to define operation handlers by implementing an OperationHandler |
| 3 | +class directly. |
| 4 | +
|
| 5 | +See hello_nexus/basic/handler/service_handler.py for the alternative "shorthand" style |
| 6 | +where you implement the `start` method only. |
3 | 7 |
|
4 | 8 | Sync operations: |
5 | 9 | --------------- |
6 | 10 | 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. |
| 11 | +computations such as this one. Total execution duration must not exceed 10s. |
10 | 12 |
|
11 | 13 |
|
12 | 14 | Workflow operations: |
@@ -62,11 +64,16 @@ def my_workflow_run_operation( |
62 | 64 | return MyWorkflowRunOperation() |
63 | 65 |
|
64 | 66 |
|
| 67 | +# This is a Nexus operation that responds synchronously to all requests. |
65 | 68 | class MySyncOperation(OperationHandler[MyInput, MyOutput]): |
66 | 69 | # You can add an __init__ method taking any required arguments, since you are in |
67 | 70 | # control of instantiating the OperationHandler inside the operation handler method |
68 | 71 | # above decorated with @operation_handler. |
69 | 72 |
|
| 73 | + # Unlike the workflow run operation below, the `start` method for a sync operation |
| 74 | + # returns the final operation result. Sync operations are free to make arbitrary |
| 75 | + # network calls, or perform CPU-bound computations. Total execution duration must not |
| 76 | + # exceed 10s. async def start( |
70 | 77 | async def start( |
71 | 78 | self, ctx: StartOperationContext, input: MyInput |
72 | 79 | ) -> StartOperationResultSync[MyOutput]: |
@@ -103,7 +110,21 @@ async def cancel( |
103 | 110 | ) |
104 | 111 |
|
105 | 112 |
|
| 113 | +# This is a Nexus operation that is backed by a Temporal workflow. That means that it |
| 114 | +# responds asynchronously to all requests: it starts a workflow and responds with a token |
| 115 | +# that the handler can associate with the worklow is started. |
106 | 116 | class MyWorkflowRunOperation(OperationHandler[MyInput, MyOutput]): |
| 117 | + # You can add an __init__ method taking any required arguments, since you are in |
| 118 | + # control of instantiating the OperationHandler inside the operation handler method |
| 119 | + # above decorated with @operation_handler. |
| 120 | + |
| 121 | + # The start method starts a workflow, and returns a WorkflowRunOperationResult that it |
| 122 | + # creates from the workflow handle. This return value contains the Nexus operation |
| 123 | + # token that the handler can use to obtain a handle and interact with the workflow on |
| 124 | + # future requests (for example if a cancel request is subsequently sent by the |
| 125 | + # caller). The Temporal server takes care of delivering the workflow result to the |
| 126 | + # calling workflow. The task queue defaults to the task queue being used by the Nexus |
| 127 | + # worker. |
107 | 128 | async def start( |
108 | 129 | self, ctx: StartOperationContext, input: MyInput |
109 | 130 | ) -> WorkflowRunOperationResult: |
|
0 commit comments