|
8 | 8 | from azure.core.credentials import TokenCredential |
9 | 9 |
|
10 | 10 | from durabletask.azuremanaged.internal.durabletask_grpc_interceptor import ( |
| 11 | + DTSAsyncDefaultClientInterceptorImpl, |
11 | 12 | DTSDefaultClientInterceptorImpl, |
12 | 13 | ) |
13 | | -from durabletask.client import TaskHubGrpcClient |
| 14 | +from durabletask.client import AsyncTaskHubGrpcClient, TaskHubGrpcClient |
14 | 15 |
|
15 | 16 |
|
16 | 17 | # Client class used for Durable Task Scheduler (DTS) |
@@ -39,3 +40,65 @@ def __init__(self, *, |
39 | 40 | log_formatter=log_formatter, |
40 | 41 | interceptors=interceptors, |
41 | 42 | default_version=default_version) |
| 43 | + |
| 44 | + |
| 45 | +# Async client class used for Durable Task Scheduler (DTS) |
| 46 | +class AsyncDurableTaskSchedulerClient(AsyncTaskHubGrpcClient): |
| 47 | + """An async client implementation for Azure Durable Task Scheduler (DTS). |
| 48 | +
|
| 49 | + This class extends AsyncTaskHubGrpcClient to provide integration with Azure's |
| 50 | + Durable Task Scheduler service using async gRPC. It handles authentication via |
| 51 | + Azure credentials and configures the necessary gRPC interceptors for DTS |
| 52 | + communication. |
| 53 | +
|
| 54 | + Args: |
| 55 | + host_address (str): The gRPC endpoint address of the DTS service. |
| 56 | + taskhub (str): The name of the task hub. Cannot be empty. |
| 57 | + token_credential (Optional[TokenCredential]): Azure credential for authentication. |
| 58 | + If None, anonymous authentication will be used. |
| 59 | + secure_channel (bool, optional): Whether to use a secure gRPC channel (TLS). |
| 60 | + Defaults to True. |
| 61 | + default_version (Optional[str], optional): Default version string for orchestrations. |
| 62 | + log_handler (Optional[logging.Handler], optional): Custom logging handler for client logs. |
| 63 | + log_formatter (Optional[logging.Formatter], optional): Custom log formatter for client logs. |
| 64 | +
|
| 65 | + Raises: |
| 66 | + ValueError: If taskhub is empty or None. |
| 67 | +
|
| 68 | + Example: |
| 69 | + >>> from azure.identity.aio import DefaultAzureCredential |
| 70 | + >>> from durabletask.azuremanaged import AsyncDurableTaskSchedulerClient |
| 71 | + >>> |
| 72 | + >>> credential = DefaultAzureCredential() |
| 73 | + >>> async with AsyncDurableTaskSchedulerClient( |
| 74 | + ... host_address="my-dts-service.azure.com:443", |
| 75 | + ... taskhub="my-task-hub", |
| 76 | + ... token_credential=credential |
| 77 | + ... ) as client: |
| 78 | + ... instance_id = await client.schedule_new_orchestration("my_orchestrator") |
| 79 | + """ |
| 80 | + |
| 81 | + def __init__(self, *, |
| 82 | + host_address: str, |
| 83 | + taskhub: str, |
| 84 | + token_credential: Optional[TokenCredential], |
| 85 | + secure_channel: bool = True, |
| 86 | + default_version: Optional[str] = None, |
| 87 | + log_handler: Optional[logging.Handler] = None, |
| 88 | + log_formatter: Optional[logging.Formatter] = None): |
| 89 | + |
| 90 | + if not taskhub: |
| 91 | + raise ValueError("Taskhub value cannot be empty. Please provide a value for your taskhub") |
| 92 | + |
| 93 | + interceptors = [DTSAsyncDefaultClientInterceptorImpl(token_credential, taskhub)] |
| 94 | + |
| 95 | + # We pass in None for the metadata so we don't construct an additional interceptor in the parent class |
| 96 | + # Since the parent class doesn't use anything metadata for anything else, we can set it as None |
| 97 | + super().__init__( |
| 98 | + host_address=host_address, |
| 99 | + secure_channel=secure_channel, |
| 100 | + metadata=None, |
| 101 | + log_handler=log_handler, |
| 102 | + log_formatter=log_formatter, |
| 103 | + interceptors=interceptors, |
| 104 | + default_version=default_version) |
0 commit comments