From 111750069a4f9a10d8887c032e943ea4a135ed92 Mon Sep 17 00:00:00 2001 From: John Smith Date: Mon, 4 Nov 2024 09:04:33 +1030 Subject: [PATCH] chore(sdk-dotnet): Evaluate clusterId lazily (#37) --- sdk-dotnet/src/API/Models.cs | 14 +++++++--- sdk-dotnet/src/Inferable.cs | 27 ++++++++++--------- sdk-dotnet/src/Service.cs | 11 ++++---- .../tests/Inferable.Tests/InferableTest.cs | 1 - 4 files changed, 30 insertions(+), 23 deletions(-) diff --git a/sdk-dotnet/src/API/Models.cs b/sdk-dotnet/src/API/Models.cs index ede8b47c..009483a0 100644 --- a/sdk-dotnet/src/API/Models.cs +++ b/sdk-dotnet/src/API/Models.cs @@ -19,10 +19,16 @@ public override void Write(Utf8JsonWriter writer, JsonSchema value, JsonSerializ public struct CreateMachineInput { - [JsonPropertyName("service")] - public required string Service { get; set; } - [JsonPropertyName("functions")] - public required List Functions { get; set; } + [ + JsonPropertyName("service"), + JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull) + ] + public string Service { get; set; } + [ + JsonPropertyName("functions"), + JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull) + ] + public List Functions { get; set; } } public struct CreateMachineResult diff --git a/sdk-dotnet/src/Inferable.cs b/sdk-dotnet/src/Inferable.cs index cc9a4869..9949d0b1 100644 --- a/sdk-dotnet/src/Inferable.cs +++ b/sdk-dotnet/src/Inferable.cs @@ -37,7 +37,6 @@ public class InferableOptions public string? BaseUrl { get; set; } public string? ApiSecret { get; set; } public string? MachineId { get; set; } - public string? ClusterId { get; set; } } public struct PollRunOptions @@ -68,7 +67,7 @@ public class InferableClient private readonly ApiClient _client; private readonly ILogger _logger; - private readonly string? _clusterId; + private string? _clusterId; // Dictionary of service name to list of functions private Dictionary> _functionRegistry = new Dictionary>(); @@ -128,7 +127,6 @@ public InferableClient(InferableOptions? options = null, ILogger async public Task CreateRunAsync(CreateRunInput input) { - if (this._clusterId == null) { - throw new ArgumentException("Cluster ID must be provided to manage runs"); - } - - var result = await this._client.CreateRunAsync(this._clusterId, input); + var clusterId = await this.GetClusterId(); + var result = await this._client.CreateRunAsync(clusterId, input); return new RunReference { ID = result.ID, @@ -217,7 +212,7 @@ async public Task CreateRunAsync(CreateRunInput input) var start = DateTime.Now; var end = start + MaxWaitTime; while (DateTime.Now < end) { - var pollResult = await this._client.GetRun(this._clusterId, result.ID); + var pollResult = await this._client.GetRun(clusterId, result.ID); var transientStates = new List { "paused", "pending", "running" }; if (transientStates.Contains(pollResult.Status)) { @@ -295,7 +290,7 @@ internal async Task StartServiceAsync(string name) { var functions = this._functionRegistry[name]; - var service = new Service(name, this._client, this._logger, functions); + var service = new Service(name, await this.GetClusterId(), this._client, this._logger, functions); this._services.Add(service); await service.Start(); @@ -308,6 +303,16 @@ internal async Task StopServiceAsync(string name) { } await existing.Stop(); } + + internal async Task GetClusterId() { + if (this._clusterId == null) { + // Call register machine without any services to test API key and get clusterId + var registerResult = await _client.CreateMachine(new CreateMachineInput {}); + this._clusterId = registerResult.ClusterId; + } + + return this._clusterId; + } } public struct RegisteredService @@ -357,6 +362,4 @@ async public Task StopAsync() { await this._inferable.StopServiceAsync(this._name); } } - - } diff --git a/sdk-dotnet/src/Service.cs b/sdk-dotnet/src/Service.cs index 9c8cbcbd..fb6c89c3 100644 --- a/sdk-dotnet/src/Service.cs +++ b/sdk-dotnet/src/Service.cs @@ -13,7 +13,7 @@ internal class Service static int DEFAULT_RETRY_AFTER_SECONDS = 10; private string _name; - private string? _clusterId; + private string _clusterId; private bool _polling = false; private int _retryAfter = DEFAULT_RETRY_AFTER_SECONDS; @@ -23,10 +23,11 @@ internal class Service private List _functions = new List(); - internal Service(string name, ApiClient client, ILogger? logger, List functions) + internal Service(string name, string clusterId, ApiClient client, ILogger? logger, List functions) { this._name = name; this._functions = functions; + this._clusterId = clusterId; this._client = client; this._logger = logger ?? NullLogger.Instance; @@ -51,7 +52,7 @@ internal bool Polling async internal Task Start() { this._logger.LogDebug("Starting service '{name}'", this._name); - this._clusterId = await RegisterMachine(); + await RegisterMachine(); // Purposely not awaiting _ = this.runLoop(); @@ -131,7 +132,7 @@ async private Task pollIteration() _logger.LogDebug($"Polling service {this._name}"); } - async private Task RegisterMachine() + async private Task RegisterMachine() { this._logger.LogDebug("Registering machine"); var functions = new List(); @@ -152,8 +153,6 @@ async private Task RegisterMachine() Service = this._name, Functions = functions }); - - return registerResult.ClusterId; } } } diff --git a/sdk-dotnet/tests/Inferable.Tests/InferableTest.cs b/sdk-dotnet/tests/Inferable.Tests/InferableTest.cs index 81cdae5e..f4d64e49 100644 --- a/sdk-dotnet/tests/Inferable.Tests/InferableTest.cs +++ b/sdk-dotnet/tests/Inferable.Tests/InferableTest.cs @@ -35,7 +35,6 @@ static InferableClient CreateInferableClient() return new InferableClient(new InferableOptions { ApiSecret = System.Environment.GetEnvironmentVariable("INFERABLE_TEST_API_SECRET")!, BaseUrl = System.Environment.GetEnvironmentVariable("INFERABLE_TEST_API_ENDPOINT")!, - ClusterId = System.Environment.GetEnvironmentVariable("INFERABLE_TEST_CLUSTER_ID")!, }, logger); }