|
28 | 28 |
|
29 | 29 |
|
30 | 30 | class ConcurrencyOptions: |
31 | | - """Configuration options for controlling concurrency of different work item types. |
| 31 | + """Configuration options for controlling concurrency of different work item types and the thread pool size. |
32 | 32 |
|
33 | | - This class mirrors the .NET DurableTask SDK's ConcurrencyOptions class, |
34 | | - providing fine-grained control over concurrent processing limits for |
35 | | - activities, orchestrations, and entities. |
| 33 | + This class provides fine-grained control over concurrent processing limits for |
| 34 | + activities, orchestrations and the thread pool size. |
36 | 35 | """ |
37 | 36 |
|
38 | 37 | def __init__( |
@@ -134,6 +133,83 @@ class ActivityNotRegisteredError(ValueError): |
134 | 133 |
|
135 | 134 |
|
136 | 135 | class TaskHubGrpcWorker: |
| 136 | + """A gRPC-based worker for processing durable task orchestrations and activities. |
| 137 | +
|
| 138 | + This worker connects to a Durable Task backend service via gRPC to receive and process |
| 139 | + work items including orchestration functions and activity functions. It provides |
| 140 | + concurrent execution capabilities with configurable limits and automatic retry handling. |
| 141 | +
|
| 142 | + The worker manages the complete lifecycle: |
| 143 | + - Registers orchestrator and activity functions |
| 144 | + - Connects to the gRPC backend service |
| 145 | + - Receives work items and executes them concurrently |
| 146 | + - Handles failures, retries, and state management |
| 147 | + - Provides logging and monitoring capabilities |
| 148 | +
|
| 149 | + Args: |
| 150 | + host_address (Optional[str], optional): The gRPC endpoint address of the backend service. |
| 151 | + Defaults to the value from environment variables or localhost. |
| 152 | + metadata (Optional[list[tuple[str, str]]], optional): gRPC metadata to include with |
| 153 | + requests. Used for authentication and routing. Defaults to None. |
| 154 | + log_handler (optional): Custom logging handler for worker logs. Defaults to None. |
| 155 | + log_formatter (Optional[logging.Formatter], optional): Custom log formatter. |
| 156 | + Defaults to None. |
| 157 | + secure_channel (bool, optional): Whether to use a secure gRPC channel (TLS). |
| 158 | + Defaults to False. |
| 159 | + interceptors (Optional[Sequence[shared.ClientInterceptor]], optional): Custom gRPC |
| 160 | + interceptors to apply to the channel. Defaults to None. |
| 161 | + concurrency_options (Optional[ConcurrencyOptions], optional): Configuration for |
| 162 | + controlling worker concurrency limits. If None, default settings are used. |
| 163 | +
|
| 164 | + Attributes: |
| 165 | + concurrency_options (ConcurrencyOptions): The current concurrency configuration. |
| 166 | +
|
| 167 | + Example: |
| 168 | + Basic worker setup: |
| 169 | +
|
| 170 | + >>> from durabletask import TaskHubGrpcWorker, ConcurrencyOptions |
| 171 | + >>> |
| 172 | + >>> # Create worker with custom concurrency settings |
| 173 | + >>> concurrency = ConcurrencyOptions( |
| 174 | + ... maximum_concurrent_activity_work_items=50, |
| 175 | + ... maximum_concurrent_orchestration_work_items=20 |
| 176 | + ... ) |
| 177 | + >>> worker = TaskHubGrpcWorker( |
| 178 | + ... host_address="localhost:4001", |
| 179 | + ... concurrency_options=concurrency |
| 180 | + ... ) |
| 181 | + >>> |
| 182 | + >>> # Register functions |
| 183 | + >>> @worker.add_orchestrator |
| 184 | + ... def my_orchestrator(context, input): |
| 185 | + ... result = yield context.call_activity("my_activity", input="hello") |
| 186 | + ... return result |
| 187 | + >>> |
| 188 | + >>> @worker.add_activity |
| 189 | + ... def my_activity(context, input): |
| 190 | + ... return f"Processed: {input}" |
| 191 | + >>> |
| 192 | + >>> # Start the worker |
| 193 | + >>> worker.start() |
| 194 | + >>> # ... worker runs in background thread |
| 195 | + >>> worker.stop() |
| 196 | +
|
| 197 | + Using as context manager: |
| 198 | +
|
| 199 | + >>> with TaskHubGrpcWorker() as worker: |
| 200 | + ... worker.add_orchestrator(my_orchestrator) |
| 201 | + ... worker.add_activity(my_activity) |
| 202 | + ... worker.start() |
| 203 | + ... # Worker automatically stops when exiting context |
| 204 | +
|
| 205 | + Raises: |
| 206 | + RuntimeError: If attempting to add orchestrators/activities while the worker is running, |
| 207 | + or if starting a worker that is already running. |
| 208 | + OrchestratorNotRegisteredError: If an orchestration work item references an |
| 209 | + unregistered orchestrator function. |
| 210 | + ActivityNotRegisteredError: If an activity work item references an unregistered |
| 211 | + activity function. |
| 212 | + """ |
137 | 213 | _response_stream: Optional[grpc.Future] = None |
138 | 214 | _interceptors: Optional[list[shared.ClientInterceptor]] = None |
139 | 215 |
|
|
0 commit comments