diff --git a/Conductor/Client/Extensions/DependencyInjectionExtensions.cs b/Conductor/Client/Extensions/DependencyInjectionExtensions.cs index bd0ada2a..24a766e4 100644 --- a/Conductor/Client/Extensions/DependencyInjectionExtensions.cs +++ b/Conductor/Client/Extensions/DependencyInjectionExtensions.cs @@ -9,41 +9,36 @@ namespace Conductor.Client.Extensions { public static class DependencyInjectionExtensions { - public static IServiceCollection WithConductorWorker(this IServiceCollection services) where T : IWorkflowTask + public static IServiceCollection AddConductorWorkflowTask(this IServiceCollection services) where T : IWorkflowTask { services.AddTransient(typeof(IWorkflowTask), typeof(T)); services.AddTransient(typeof(T)); return services; } - public static IServiceCollection WithOrkesApiClient(this IServiceCollection services, OrkesApiClient orkesApiClient) + public static IServiceCollection AddConductorWorker(this IServiceCollection services, Configuration configuration = null, Action configureHttpClient = null) { services.AddHttpClient(); services.AddOptions(); + if (configuration == null) + { + configuration = new Configuration(); + } + services.AddSingleton(configuration); + OrkesApiClient orkesApiClient = new OrkesApiClient(configuration); services.AddSingleton(orkesApiClient); - services.AddTransient(); + services.AddSingleton(new ConductorWorkerRestClient(orkesApiClient)); services.AddSingleton(); services.AddTransient(); - services.AddSingleton(orkesApiClient); - return services; - } - - public static IServiceCollection WithHostedService(this IServiceCollection services) where T : BackgroundService - { - services.AddHostedService(); - return services; + return services.AddConductorClient(configureHttpClient); } - public static IServiceCollection WithConfiguration(this IServiceCollection services, Configuration configuration) + public static IServiceCollection AddConductorClient(this IServiceCollection services, Func serverUrl) { - if (configuration == null) + services.AddHttpClient((provider, client) => { - services.AddSingleton(); - } - else - { - services.AddSingleton(configuration); - } + client.BaseAddress = new Uri(serverUrl(provider)); + }); return services; } @@ -59,5 +54,12 @@ public static IServiceCollection AddConductorClient(this IServiceCollection serv } return services; } + + public static IServiceCollection WithHostedService(this IServiceCollection services) where T : BackgroundService + { + services.AddHostedService(); + return services; + } + } } diff --git a/Conductor/Client/OrkesApiClient.cs b/Conductor/Client/OrkesApiClient.cs index 7140f03b..a79572cf 100644 --- a/Conductor/Client/OrkesApiClient.cs +++ b/Conductor/Client/OrkesApiClient.cs @@ -10,22 +10,24 @@ public class OrkesApiClient { private Configuration _configuration; private MemoryCache _memoryCache; - - private OrkesAuthenticationSettings _authenticationSettings; private TokenResourceApi _tokenClient; + private OrkesAuthenticationSettings _authenticationSettings; private OrkesApiClient() { _memoryCache = new MemoryCache(new MemoryCacheOptions()); _configuration = null; - _authenticationSettings = null; _tokenClient = null; + _authenticationSettings = null; } - public OrkesApiClient(Configuration configuration = null, OrkesAuthenticationSettings authenticationSettings = null) : this() + public OrkesApiClient(Configuration configuration = null) : this() { - _authenticationSettings = authenticationSettings; _configuration = configuration; + if (_configuration != null && !string.IsNullOrEmpty(_configuration.keyId) && !string.IsNullOrEmpty(_configuration.keySecret)) + { + _authenticationSettings = new OrkesAuthenticationSettings(_configuration.keyId, _configuration.keySecret); + } RefreshAuthenticationHeader(); } diff --git a/Conductor/Client/Worker/ConductorWorkerRestClient.cs b/Conductor/Client/Worker/ConductorWorkerRestClient.cs index 85f54af2..2d7530c8 100644 --- a/Conductor/Client/Worker/ConductorWorkerRestClient.cs +++ b/Conductor/Client/Worker/ConductorWorkerRestClient.cs @@ -1,7 +1,7 @@ +using Conductor.Api; using Conductor.Client.Interfaces; using Conductor.Client.Models; using System.Threading.Tasks; -using Conductor.Api; namespace Conductor.Client { diff --git a/Conductor/Client/Worker/WorkflowTaskCoordinator.cs b/Conductor/Client/Worker/WorkflowTaskCoordinator.cs index acbcbcab..fdfd8e39 100644 --- a/Conductor/Client/Worker/WorkflowTaskCoordinator.cs +++ b/Conductor/Client/Worker/WorkflowTaskCoordinator.cs @@ -11,14 +11,13 @@ namespace Conductor.Client.Worker internal class WorkflowTaskCoordinator : IWorkflowTaskCoordinator { private int _concurrentWorkers; - private IServiceProvider _serviceProvider; private ILogger _logger; + private IWorkflowTaskExecutor _workflowTaskExecutor; private HashSet _workerDefinitions; private TaskResourceApi _client; public WorkflowTaskCoordinator(IServiceProvider serviceProvider, ILogger logger, OrkesApiClient orkesApiClient, int? concurrentWorkers = null) { - _serviceProvider = serviceProvider; _logger = logger; _workerDefinitions = new HashSet(); if (concurrentWorkers == null) @@ -27,6 +26,7 @@ public WorkflowTaskCoordinator(IServiceProvider serviceProvider, ILogger(); + _workflowTaskExecutor = serviceProvider.GetService(typeof(IWorkflowTaskExecutor)) as IWorkflowTaskExecutor; } public async Task Start() @@ -35,8 +35,7 @@ public async Task Start() var pollers = new List(); for (var i = 0; i < _concurrentWorkers; i++) { - var executor = _serviceProvider.GetService(typeof(IWorkflowTaskExecutor)) as IWorkflowTaskExecutor; - pollers.Add(executor.StartPoller(_workerDefinitions.ToList())); + pollers.Add(_workflowTaskExecutor.StartPoller(_workerDefinitions.ToList())); } await Task.WhenAll(pollers); } diff --git a/Conductor/Client/Worker/WorkflowTaskExecutor.cs b/Conductor/Client/Worker/WorkflowTaskExecutor.cs index 5b634158..e7c6ddd6 100644 --- a/Conductor/Client/Worker/WorkflowTaskExecutor.cs +++ b/Conductor/Client/Worker/WorkflowTaskExecutor.cs @@ -13,6 +13,7 @@ namespace Conductor.Client.Worker { internal class WorkflowTaskExecutor : IWorkflowTaskExecutor { + private IServiceProvider _serviceProvider; private List workers; private ILogger logger; private readonly Configuration configuration; @@ -24,12 +25,11 @@ internal class WorkflowTaskExecutor : IWorkflowTaskExecutor private readonly string workerId = Environment.MachineName + "_" + new Random(epoch).Next(); public WorkflowTaskExecutor( - IConductorWorkerRestClient taskClient, IServiceProvider serviceProvider, ILogger logger, IOptions configuration) { - this.taskClient = taskClient; + this.taskClient = serviceProvider.GetService(typeof(ConductorWorkerRestClient)) as ConductorWorkerRestClient; this.serviceProvider = serviceProvider; this.logger = logger; this.configuration = configuration.Value; diff --git a/Conductor/conductor-csharp.csproj b/Conductor/conductor-csharp.csproj index 39f4a5a5..7c57d162 100644 --- a/Conductor/conductor-csharp.csproj +++ b/Conductor/conductor-csharp.csproj @@ -15,5 +15,6 @@ + \ No newline at end of file diff --git a/Tests/Util/ApiUtil.cs b/Tests/Util/ApiUtil.cs index c2bf4bf8..303f7dda 100644 --- a/Tests/Util/ApiUtil.cs +++ b/Tests/Util/ApiUtil.cs @@ -1,6 +1,5 @@ using Conductor.Api; using Conductor.Client; -using Conductor.Client.Authentication; using Conductor.Executor; using System; using System.Diagnostics; @@ -34,30 +33,17 @@ public static WorkflowExecutor GetWorkflowExecutor() public static T GetClient() where T : IApiAccessor, new() { - OrkesApiClient apiClient = GetApiClient(); + OrkesApiClient apiClient = new OrkesApiClient(GetConfiguration()); return apiClient.GetClient(); } - public static OrkesApiClient GetApiClient() + public static Configuration GetConfiguration() { - return GetApiClient( - basePath: _basePath, - keyId: _keyId, - keySecret: _keySecret - ); - } - - private static OrkesApiClient GetApiClient(string basePath, string keyId, string keySecret) - { - return new OrkesApiClient( - configuration: new Configuration() - { - BasePath = basePath - }, - authenticationSettings: new OrkesAuthenticationSettings( - keyId, keySecret - ) - ); + Configuration configuration = new Configuration(); + configuration.keyId = _keyId; + configuration.keySecret = _keySecret; + configuration.BasePath = _basePath; + return configuration; } private static string GetEnvironmentVariable(string variable) diff --git a/Tests/Worker/WorkerTests.cs b/Tests/Worker/WorkerTests.cs index c47cdb8d..1c4ac166 100644 --- a/Tests/Worker/WorkerTests.cs +++ b/Tests/Worker/WorkerTests.cs @@ -1,8 +1,11 @@ using Conductor.Api; +using Conductor.Client; using Conductor.Client.Extensions; +using Conductor.Client.Interfaces; using Conductor.Definition; using Conductor.Definition.TaskType; using Conductor.Executor; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using System; @@ -83,8 +86,8 @@ private IHost GetWorkerHost() .ConfigureServices( (ctx, services) => { - services.WithOrkesApiClient(ApiUtil.GetApiClient()); - services.WithConductorWorker(); + services.AddConductorWorker(ApiUtil.GetConfiguration()); + services.AddConductorWorkflowTask(); services.WithHostedService(); } ).ConfigureLogging(