From a6252adff6c70eb4a0c6a31c544cc98a4f1ff11f Mon Sep 17 00:00:00 2001 From: Devis Lucato Date: Sat, 23 Nov 2024 11:28:37 -0800 Subject: [PATCH] Fix URLs and ping errors --- aspire-orchestrator/Aspire.AppHost/Program.cs | 4 ---- .../WorkbenchConnector/WorkbenchConnector.cs | 15 ++++++++++++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/aspire-orchestrator/Aspire.AppHost/Program.cs b/aspire-orchestrator/Aspire.AppHost/Program.cs index 9d8b24ff..055eb84d 100644 --- a/aspire-orchestrator/Aspire.AppHost/Program.cs +++ b/aspire-orchestrator/Aspire.AppHost/Program.cs @@ -29,10 +29,6 @@ .WithReference(workbenchServiceEndpoint) .WaitFor(workbenchService); -// Console.WriteLine($"{dotnetAgent.GetEndpoint("http")}"); -// Console.WriteLine(dotnetAgent.GetEndpoint("http").Url); -// Environment.Exit(0); - // The agent needs to reference its own endpoint, in order to send it to the workbench service when during the registration dotnetAgent .WithEnvironment("Workbench__ConnectorEndpoint", $"{dotnetAgent.GetEndpoint("http")}/myagents") diff --git a/libraries/dotnet/WorkbenchConnector/WorkbenchConnector.cs b/libraries/dotnet/WorkbenchConnector/WorkbenchConnector.cs index 06bbe221..eb220be3 100644 --- a/libraries/dotnet/WorkbenchConnector/WorkbenchConnector.cs +++ b/libraries/dotnet/WorkbenchConnector/WorkbenchConnector.cs @@ -64,7 +64,7 @@ public virtual async Task ConnectAsync(CancellationToken cancellationToken = def this.Log.LogInformation("Connecting {1} {2} {3} to {4}...", this.WorkbenchConfig.ConnectorName, this.WorkbenchConfig.ConnectorId, this.WorkbenchConfig.ConnectorEndpoint, this.WorkbenchConfig.WorkbenchEndpoint); #pragma warning disable CS4014 // ping runs in the background without blocking - this._pingTimer ??= new Timer(_ => this.PingSemanticWorkbenchBackendAsync(cancellationToken), null, 0, 10000); + this._pingTimer ??= new Timer(_ => this.PingSemanticWorkbenchBackendAsync(cancellationToken), null, 0, PingFrequencyMS); #pragma warning restore CS4014 List agents = await this.Storage.GetAllAgentsAsync(cancellationToken).ConfigureAwait(false); @@ -373,9 +373,12 @@ public virtual async Task DeleteFileAsync( public virtual async Task PingSemanticWorkbenchBackendAsync(CancellationToken cancellationToken) { - this.Log.LogTrace("Pinging workbench backend"); + // Disable timer during the request + this._pingTimer?.Change(Timeout.Infinite, Timeout.Infinite); + string path = Constants.AgentServiceRegistration.Path .Replace(Constants.AgentServiceRegistration.Placeholder, this.WorkbenchConfig.ConnectorId, StringComparison.OrdinalIgnoreCase); + this.Log.LogTrace("Pinging workbench backend at {Path}", path); var data = new { @@ -386,10 +389,15 @@ public virtual async Task PingSemanticWorkbenchBackendAsync(CancellationToken ca }; await this.SendAsync(HttpMethod.Put, path, data, null, "PingSWBackend", cancellationToken).ConfigureAwait(false); + + // Activate timer + this._pingTimer?.Change(TimeSpan.FromMilliseconds(PingFrequencyMS), TimeSpan.FromMilliseconds(PingFrequencyMS)); } #region internals =========================================================================== + private const int PingFrequencyMS = 10000; + public void Dispose() { this.Dispose(true); @@ -414,11 +422,12 @@ protected virtual async Task SendAsync( string description, CancellationToken cancellationToken) { + url = url.TrimStart('/'); try { this.Log.LogTrace("Preparing request: {2}", description); HttpRequestMessage request = this.PrepareRequest(method, url, data, agentId); - this.Log.LogTrace("Sending request {0} {1} [{2}]", method, url.HtmlEncode(), description); + this.Log.LogTrace("Sending request {Method} {BaseAddress}{Path} [{Description}]", method, this.HttpClient.BaseAddress, url.HtmlEncode(), description); HttpResponseMessage result = await this.HttpClient .SendAsync(request, cancellationToken) .ConfigureAwait(false);