Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
YunchuWang committed Jan 14, 2025
1 parent 3a40d09 commit c697fbc
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 45 deletions.
53 changes: 32 additions & 21 deletions src/Client/AzureManaged/DurableTaskSchedulerClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,12 @@ public static void UseDurableTaskScheduler(
TokenCredential credential,
Action<DurableTaskSchedulerClientOptions>? configure = null)
{
builder.Services.AddOptions<DurableTaskSchedulerClientOptions>(builder.Name)
.Configure(options =>
{
options.EndpointAddress = endpointAddress;
options.TaskHubName = taskHubName;
options.Credential = credential;
})
.Configure(configure ?? (_ => { }))
.ValidateDataAnnotations()
.ValidateOnStart();

builder.Services.TryAddEnumerable(
ServiceDescriptor.Singleton<IConfigureOptions<GrpcDurableTaskClientOptions>, ConfigureGrpcChannel>());
builder.UseGrpc(_ => { });
ConfigureSchedulerOptions(builder, options =>
{
options.EndpointAddress = endpointAddress;
options.TaskHubName = taskHubName;
options.Credential = credential;
}, configure);

Check warning on line 37 in src/Client/AzureManaged/DurableTaskSchedulerClientExtensions.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

The parameters should all be placed on the same line or each parameter should be placed on its own line (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1117.md)
}

/// <summary>
Expand All @@ -57,15 +49,34 @@ public static void UseDurableTaskScheduler(
Action<DurableTaskSchedulerClientOptions>? configure = null)
{
var connectionOptions = DurableTaskSchedulerClientOptions.FromConnectionString(connectionString);
ConfigureSchedulerOptions(builder, options =>
{
options.EndpointAddress = connectionOptions.EndpointAddress;
options.TaskHubName = connectionOptions.TaskHubName;
options.Credential = connectionOptions.Credential;
}, configure);

Check warning on line 57 in src/Client/AzureManaged/DurableTaskSchedulerClientExtensions.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

The parameters should all be placed on the same line or each parameter should be placed on its own line (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1117.md)
}

/// <summary>
/// Configures Durable Task client to use the Azure Durable Task Scheduler service using configuration options.
/// </summary>
/// <param name="builder">The Durable Task client builder to configure.</param>
/// <param name="configure">Callback to configure DurableTaskSchedulerClientOptions.</param>
public static void UseDurableTaskScheduler(
this IDurableTaskClientBuilder builder,
Action<DurableTaskSchedulerClientOptions>? configure = null)
{
ConfigureSchedulerOptions(builder, _ => { }, configure);
}

private static void ConfigureSchedulerOptions(
IDurableTaskClientBuilder builder,
Action<DurableTaskSchedulerClientOptions> initialConfig,
Action<DurableTaskSchedulerClientOptions>? additionalConfig)
{
builder.Services.AddOptions<DurableTaskSchedulerClientOptions>(builder.Name)
.Configure(options =>
{
options.EndpointAddress = connectionOptions.EndpointAddress;
options.TaskHubName = connectionOptions.TaskHubName;
options.Credential = connectionOptions.Credential;
})
.Configure(configure ?? (_ => { }))
.Configure(initialConfig)
.Configure(additionalConfig ?? (_ => { }))
.ValidateDataAnnotations()
.ValidateOnStart();

Expand Down
53 changes: 32 additions & 21 deletions src/Worker/AzureManaged/DurableTaskSchedulerWorkerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,12 @@ public static void UseDurableTaskScheduler(
TokenCredential credential,
Action<DurableTaskSchedulerWorkerOptions>? configure = null)
{
builder.Services.AddOptions<DurableTaskSchedulerWorkerOptions>(builder.Name)
.Configure(options =>
{
options.EndpointAddress = endpointAddress;
options.TaskHubName = taskHubName;
options.Credential = credential;
})
.Configure(configure ?? (_ => { }))
.ValidateDataAnnotations()
.ValidateOnStart();

builder.Services.TryAddEnumerable(
ServiceDescriptor.Singleton<IConfigureOptions<GrpcDurableTaskWorkerOptions>, ConfigureGrpcChannel>());
builder.UseGrpc(_ => { });
ConfigureSchedulerOptions(builder, options =>
{
options.EndpointAddress = endpointAddress;
options.TaskHubName = taskHubName;
options.Credential = credential;
}, configure);

Check warning on line 38 in src/Worker/AzureManaged/DurableTaskSchedulerWorkerExtensions.cs

View workflow job for this annotation

GitHub Actions / build

The parameters should all be placed on the same line or each parameter should be placed on its own line (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1117.md)
}

/// <summary>
Expand All @@ -58,15 +50,34 @@ public static void UseDurableTaskScheduler(
Action<DurableTaskSchedulerWorkerOptions>? configure = null)
{
var connectionOptions = DurableTaskSchedulerWorkerOptions.FromConnectionString(connectionString);
ConfigureSchedulerOptions(builder, options =>
{
options.EndpointAddress = connectionOptions.EndpointAddress;
options.TaskHubName = connectionOptions.TaskHubName;
options.Credential = connectionOptions.Credential;
}, configure);

Check warning on line 58 in src/Worker/AzureManaged/DurableTaskSchedulerWorkerExtensions.cs

View workflow job for this annotation

GitHub Actions / build

The parameters should all be placed on the same line or each parameter should be placed on its own line (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1117.md)
}

/// <summary>
/// Configures Durable Task worker to use the Azure Durable Task Scheduler service using configuration options.
/// </summary>
/// <param name="builder">The Durable Task worker builder to configure.</param>
/// <param name="configure">Callback to configure DurableTaskSchedulerWorkerOptions.</param>
public static void UseDurableTaskScheduler(
this IDurableTaskWorkerBuilder builder,
Action<DurableTaskSchedulerWorkerOptions>? configure = null)
{
ConfigureSchedulerOptions(builder, _ => { }, configure);
}

private static void ConfigureSchedulerOptions(
IDurableTaskWorkerBuilder builder,
Action<DurableTaskSchedulerWorkerOptions> initialConfig,
Action<DurableTaskSchedulerWorkerOptions>? additionalConfig)
{
builder.Services.AddOptions<DurableTaskSchedulerWorkerOptions>(builder.Name)
.Configure(options =>
{
options.EndpointAddress = connectionOptions.EndpointAddress;
options.TaskHubName = connectionOptions.TaskHubName;
options.Credential = connectionOptions.Credential;
})
.Configure(configure ?? (_ => { }))
.Configure(initialConfig)
.Configure(additionalConfig ?? (_ => { }))
.ValidateDataAnnotations()
.ValidateOnStart();

Expand Down
4 changes: 2 additions & 2 deletions src/Worker/AzureManaged/DurableTaskSchedulerWorkerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ internal static DurableTaskSchedulerWorkerOptions FromConnectionString(
/// <returns>A configured <see cref="GrpcChannel"/> instance that can be used to make gRPC calls.</returns>
public GrpcChannel CreateChannel()

Check warning on line 80 in src/Worker/AzureManaged/DurableTaskSchedulerWorkerOptions.cs

View workflow job for this annotation

GitHub Actions / build

{
Check.NotNullOrEmpty(this.EndpointAddress, nameof(this.EndpointAddress));
Check.NotNullOrEmpty(this.TaskHubName, nameof(this.TaskHubName));
Verify.NotNull(this.EndpointAddress, nameof(this.EndpointAddress));
Verify.NotNull(this.TaskHubName, nameof(this.TaskHubName));
string taskHubName = this.TaskHubName;
string endpoint = !this.EndpointAddress.Contains("://")
? $"https://{this.EndpointAddress}"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.ComponentModel.DataAnnotations;
using Azure.Core;
using Azure.Identity;
using FluentAssertions;
Expand All @@ -10,7 +11,6 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Moq;
using System.ComponentModel.DataAnnotations;
using Xunit;

namespace Microsoft.DurableTask.Client.AzureManaged.Tests;
Expand Down

0 comments on commit c697fbc

Please sign in to comment.