|
| 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 uuid |
| 20 | + |
| 21 | +import temporalio.common |
| 22 | +import temporalio.nexus.handler |
| 23 | +from nexusrpc.handler import ( |
| 24 | + CancelOperationContext, |
| 25 | + FetchOperationInfoContext, |
| 26 | + FetchOperationResultContext, |
| 27 | + OperationHandler, |
| 28 | + OperationInfo, |
| 29 | + StartOperationContext, |
| 30 | + StartOperationResultAsync, |
| 31 | + StartOperationResultSync, |
| 32 | + operation_handler, |
| 33 | + service_handler, |
| 34 | +) |
| 35 | + |
| 36 | +from hello_nexus.basic.handler.db_client import MyDBClient |
| 37 | +from hello_nexus.basic.handler.service_handler import ( |
| 38 | + MyInput, |
| 39 | + MyNexusService, |
| 40 | + MyOutput, |
| 41 | +) |
| 42 | +from hello_nexus.basic.handler.workflows import WorkflowStartedByNexusOperation |
| 43 | + |
| 44 | + |
| 45 | +@service_handler(service=MyNexusService) |
| 46 | +class MyNexusServiceHandlerUsingOperationHandlerClasses: |
| 47 | + # You can create an __init__ method accepting what is needed by your operation |
| 48 | + # handlers to handle requests. You typically instantiate your service handler class |
| 49 | + # when starting your worker. See hello_nexus/basic/handler/worker.py. |
| 50 | + def __init__(self, connected_db_client: MyDBClient): |
| 51 | + # `connected_db_client` is intended as an example of something that might be |
| 52 | + # required by your operation handlers when handling requests, but is only |
| 53 | + # available at worker-start time. |
| 54 | + self.connected_db_client = connected_db_client |
| 55 | + |
| 56 | + @operation_handler |
| 57 | + def my_sync_operation(self) -> OperationHandler[MyInput, MyOutput]: |
| 58 | + # Pass any required arguments to the OperationHandler __init__ method here. |
| 59 | + return MySyncOperation() |
| 60 | + |
| 61 | + @operation_handler |
| 62 | + def my_workflow_run_operation( |
| 63 | + self, |
| 64 | + ) -> OperationHandler[MyInput, MyOutput]: |
| 65 | + # Pass any required arguments to the OperationHandler __init__ method here. |
| 66 | + return MyWorkflowRunOperation() |
| 67 | + |
| 68 | + |
| 69 | +class MySyncOperation(OperationHandler[MyInput, MyOutput]): |
| 70 | + # You can add an __init__ method taking any required arguments, since you are in |
| 71 | + # control of instantiating the OperationHandler inside the operation handler method |
| 72 | + # above decorated with @operation_handler. |
| 73 | + |
| 74 | + async def start( |
| 75 | + self, ctx: StartOperationContext, input: MyInput |
| 76 | + ) -> StartOperationResultSync[MyOutput]: |
| 77 | + output = MyOutput(message=f"Hello {input.name}!") |
| 78 | + return StartOperationResultSync(output) |
| 79 | + |
| 80 | + async def fetch_info( |
| 81 | + self, |
| 82 | + ctx: FetchOperationInfoContext, |
| 83 | + token: str, |
| 84 | + ) -> OperationInfo: |
| 85 | + raise NotImplementedError( |
| 86 | + "fetch_info is not supported when a Nexus operation is called by a Temporal workflow" |
| 87 | + ) |
| 88 | + |
| 89 | + async def fetch_result( |
| 90 | + self, |
| 91 | + ctx: FetchOperationResultContext, |
| 92 | + token: str, |
| 93 | + ) -> MyOutput: |
| 94 | + raise NotImplementedError( |
| 95 | + "fetch_result is not supported when a Nexus operation is called by a Temporal workflow, " |
| 96 | + "but this sample does not demonstrate result fetching" |
| 97 | + ) |
| 98 | + |
| 99 | + async def cancel( |
| 100 | + self, |
| 101 | + ctx: CancelOperationContext, |
| 102 | + token: str, |
| 103 | + ) -> None: |
| 104 | + raise NotImplementedError( |
| 105 | + "cancel is supported when a Nexus operation is called by a Temporal workflow, " |
| 106 | + "but this sample does not demonstrate cancellation" |
| 107 | + ) |
| 108 | + |
| 109 | + |
| 110 | +class MyWorkflowRunOperation(OperationHandler[MyInput, MyOutput]): |
| 111 | + async def start( |
| 112 | + self, ctx: StartOperationContext, input: MyInput |
| 113 | + ) -> StartOperationResultAsync: |
| 114 | + workflow_id = str(uuid.uuid4()) |
| 115 | + wf_handle = await temporalio.nexus.handler.start_workflow( |
| 116 | + ctx, |
| 117 | + WorkflowStartedByNexusOperation.run, |
| 118 | + input, |
| 119 | + id=workflow_id, |
| 120 | + ) |
| 121 | + # TODO(dan) |
| 122 | + token = temporalio.nexus.token.WorkflowOperationToken.from_workflow_handle( |
| 123 | + wf_handle |
| 124 | + ).encode() |
| 125 | + return StartOperationResultAsync(token) |
| 126 | + |
| 127 | + async def cancel(self, ctx: CancelOperationContext, token: str) -> None: |
| 128 | + return await temporalio.nexus.handler.cancel_workflow(ctx, token) |
| 129 | + |
| 130 | + async def fetch_info( |
| 131 | + self, ctx: FetchOperationInfoContext, token: str |
| 132 | + ) -> OperationInfo: |
| 133 | + raise NotImplementedError( |
| 134 | + "fetch_info is not supported when a Nexus operation is called by a Temporal workflow" |
| 135 | + ) |
| 136 | + |
| 137 | + async def fetch_result( |
| 138 | + self, ctx: FetchOperationResultContext, token: str |
| 139 | + ) -> MyOutput: |
| 140 | + raise NotImplementedError( |
| 141 | + "fetch_result is not supported when a Nexus operation is called by a Temporal workflow, " |
| 142 | + "but this sample does not demonstrate result fetching" |
| 143 | + ) |
0 commit comments