diff --git a/packages/csharp/ArmoniK.Api.Tests/WorkerServerTest.cs b/packages/csharp/ArmoniK.Api.Tests/WorkerServerTest.cs index 9a20e1ff0..0b0128522 100644 --- a/packages/csharp/ArmoniK.Api.Tests/WorkerServerTest.cs +++ b/packages/csharp/ArmoniK.Api.Tests/WorkerServerTest.cs @@ -56,17 +56,17 @@ public virtual void TearDown() [Test] public Task BuildServer() { - var collection = new List>(); - - collection.Add(new KeyValuePair($"{nameof(ComputePlane)}:{nameof(ComputePlane.WorkerChannel)}:{nameof(ComputePlane.WorkerChannel.Address)}", - "/tmp/worker.sock")); - collection.Add(new KeyValuePair($"{nameof(ComputePlane)}:{nameof(ComputePlane.WorkerChannel)}:{nameof(ComputePlane.WorkerChannel.SocketType)}", - GrpcSocketType.UnixDomainSocket.ToString())); - - collection.Add(new KeyValuePair($"{nameof(ComputePlane)}:{nameof(ComputePlane.AgentChannel)}:{nameof(ComputePlane.AgentChannel.Address)}", - "/tmp/agent.sock")); - collection.Add(new KeyValuePair($"{nameof(ComputePlane)}:{nameof(ComputePlane.AgentChannel)}:{nameof(ComputePlane.AgentChannel.SocketType)}", - GrpcSocketType.UnixDomainSocket.ToString())); + var collection = new List> + { + new($"{nameof(ComputePlane)}:{nameof(ComputePlane.WorkerChannel)}:{nameof(ComputePlane.WorkerChannel.Address)}", + "/tmp/worker.sock"), + new($"{nameof(ComputePlane)}:{nameof(ComputePlane.WorkerChannel)}:{nameof(ComputePlane.WorkerChannel.SocketType)}", + GrpcSocketType.UnixDomainSocket.ToString()), + new($"{nameof(ComputePlane)}:{nameof(ComputePlane.AgentChannel)}:{nameof(ComputePlane.AgentChannel.Address)}", + "/tmp/agent.sock"), + new($"{nameof(ComputePlane)}:{nameof(ComputePlane.AgentChannel)}:{nameof(ComputePlane.AgentChannel.SocketType)}", + GrpcSocketType.UnixDomainSocket.ToString()), + }; var configuration = new ConfigurationBuilder().AddInMemoryCollection(collection) .Build(); @@ -80,6 +80,32 @@ public Task BuildServer() return Task.CompletedTask; } + [Test] + public Task BuildServerConfigurator() + { + var collection = new List> + { + new($"{nameof(ComputePlane)}:{nameof(ComputePlane.WorkerChannel)}:{nameof(ComputePlane.WorkerChannel.Address)}", + "/tmp/worker.sock"), + new($"{nameof(ComputePlane)}:{nameof(ComputePlane.WorkerChannel)}:{nameof(ComputePlane.WorkerChannel.SocketType)}", + GrpcSocketType.UnixDomainSocket.ToString()), + new($"{nameof(ComputePlane)}:{nameof(ComputePlane.AgentChannel)}:{nameof(ComputePlane.AgentChannel.Address)}", + "/tmp/agent.sock"), + new($"{nameof(ComputePlane)}:{nameof(ComputePlane.AgentChannel)}:{nameof(ComputePlane.AgentChannel.SocketType)}", + GrpcSocketType.UnixDomainSocket.ToString()), + }; + + var app = WorkerServer.Create((_, + configuration) => + { + foreach (var pair in collection) + { + configuration[pair.Key] = pair.Value; + } + }); + return Task.CompletedTask; + } + [Test] public Task BuildServerNoArgs() { diff --git a/packages/csharp/ArmoniK.Api.Worker/Utils/WorkerServer.cs b/packages/csharp/ArmoniK.Api.Worker/Utils/WorkerServer.cs index f1e06de99..aaeaa7495 100644 --- a/packages/csharp/ArmoniK.Api.Worker/Utils/WorkerServer.cs +++ b/packages/csharp/ArmoniK.Api.Worker/Utils/WorkerServer.cs @@ -60,6 +60,31 @@ public static class WorkerServer public static WebApplication Create(IConfiguration? configuration = null, Action? serviceConfigurator = null) where T : gRPC.V1.Worker.Worker.WorkerBase + => Create((collection, + configuration1) => + { + if (configuration != null) + { + foreach (var pair in configuration.AsEnumerable()) + { + configuration1[pair.Key] = pair.Value; + } + } + + serviceConfigurator?.Invoke(collection); + }); + + + /// + /// Create a web application for the given ArmoniK Worker gRPC Service + /// + /// gRPC Service to add to the web application + /// Lambda to configure server services + /// + /// The web application initialized + /// + public static WebApplication Create(Action? configurator) + where T : gRPC.V1.Worker.Worker.WorkerBase { try { @@ -71,10 +96,8 @@ public static WebApplication Create(IConfiguration? configuration false) .AddEnvironmentVariables(); - if (configuration is not null) - { - builder.Configuration.AddConfiguration(configuration); - } + configurator?.Invoke(builder.Services, + builder.Configuration); Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(builder.Configuration) .WriteTo.Console(new CompactJsonFormatter()) @@ -114,7 +137,6 @@ public static WebApplication Create(IConfiguration? configuration .AddGrpcReflection() .AddGrpc(options => options.MaxReceiveMessageSize = null); - serviceConfigurator?.Invoke(builder.Services); var app = builder.Build();