Skip to content

Commit

Permalink
feat: new overloads for worker server creation (#407)
Browse files Browse the repository at this point in the history
  • Loading branch information
lemaitre-aneo authored Sep 5, 2023
2 parents 10db83f + 692024d commit 226f0a7
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 16 deletions.
48 changes: 37 additions & 11 deletions packages/csharp/ArmoniK.Api.Tests/WorkerServerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ public virtual void TearDown()
[Test]
public Task BuildServer()
{
var collection = new List<KeyValuePair<string, string>>();

collection.Add(new KeyValuePair<string, string>($"{nameof(ComputePlane)}:{nameof(ComputePlane.WorkerChannel)}:{nameof(ComputePlane.WorkerChannel.Address)}",
"/tmp/worker.sock"));
collection.Add(new KeyValuePair<string, string>($"{nameof(ComputePlane)}:{nameof(ComputePlane.WorkerChannel)}:{nameof(ComputePlane.WorkerChannel.SocketType)}",
GrpcSocketType.UnixDomainSocket.ToString()));

collection.Add(new KeyValuePair<string, string>($"{nameof(ComputePlane)}:{nameof(ComputePlane.AgentChannel)}:{nameof(ComputePlane.AgentChannel.Address)}",
"/tmp/agent.sock"));
collection.Add(new KeyValuePair<string, string>($"{nameof(ComputePlane)}:{nameof(ComputePlane.AgentChannel)}:{nameof(ComputePlane.AgentChannel.SocketType)}",
GrpcSocketType.UnixDomainSocket.ToString()));
var collection = new List<KeyValuePair<string, string>>
{
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();
Expand All @@ -80,6 +80,32 @@ public Task BuildServer()
return Task.CompletedTask;
}

[Test]
public Task BuildServerConfigurator()
{
var collection = new List<KeyValuePair<string, string>>
{
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<TestService>((_,
configuration) =>
{
foreach (var pair in collection)
{
configuration[pair.Key] = pair.Value;
}
});
return Task.CompletedTask;
}

[Test]
public Task BuildServerNoArgs()
{
Expand Down
32 changes: 27 additions & 5 deletions packages/csharp/ArmoniK.Api.Worker/Utils/WorkerServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,31 @@ public static class WorkerServer
public static WebApplication Create<T>(IConfiguration? configuration = null,
Action<IServiceCollection>? serviceConfigurator = null)
where T : gRPC.V1.Worker.Worker.WorkerBase
=> Create<T>((collection,
configuration1) =>
{
if (configuration != null)
{
foreach (var pair in configuration.AsEnumerable())
{
configuration1[pair.Key] = pair.Value;
}
}

serviceConfigurator?.Invoke(collection);
});


/// <summary>
/// Create a web application for the given ArmoniK Worker gRPC Service
/// </summary>
/// <typeparam name="T">gRPC Service to add to the web application</typeparam>
/// <param name="configurator">Lambda to configure server services</param>
/// <returns>
/// The web application initialized
/// </returns>
public static WebApplication Create<T>(Action<IServiceCollection, IConfiguration>? configurator)
where T : gRPC.V1.Worker.Worker.WorkerBase
{
try
{
Expand All @@ -71,10 +96,8 @@ public static WebApplication Create<T>(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())
Expand Down Expand Up @@ -114,7 +137,6 @@ public static WebApplication Create<T>(IConfiguration? configuration
.AddGrpcReflection()
.AddGrpc(options => options.MaxReceiveMessageSize = null);

serviceConfigurator?.Invoke(builder.Services);

var app = builder.Build();

Expand Down

0 comments on commit 226f0a7

Please sign in to comment.