From cade91aa4ab15590ae6806e62200309661c6de80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Eggert=20Lundsteen=20Olsen?= Date: Fri, 2 Oct 2026 13:02:35 +0200 Subject: [PATCH] Review 1.3: a request still waiting for the agent is reused, not queued again Pressing Test forbindelsen repeatedly, or a script calling the API in a loop, added a row per call to insight.AgentRequests, each run by the agent in turn. The same pending request (kind, instance, parameters) is now returned instead. Co-Authored-By: Claude Opus 5.5 (1M context) --- ...InsightRepository.InstanceConfiguration.cs | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/SQLInsight.Repository/InsightRepository.InstanceConfiguration.cs b/src/SQLInsight.Repository/InsightRepository.InstanceConfiguration.cs index 1394e18..b03a29d 100644 --- a/src/SQLInsight.Repository/InsightRepository.InstanceConfiguration.cs +++ b/src/SQLInsight.Repository/InsightRepository.InstanceConfiguration.cs @@ -307,15 +307,27 @@ public sealed partial class InsightRepository return unknown; } - /// Queues a request for the agent (SQLInsight Web: INSERT only). + /// + /// Queues a request for the agent (SQLInsight Web: INSERT only). The same request still waiting for the agent is + /// returned instead of a new one, so pressing the button again (or a script in a loop) does not grow the queue. + /// public async Task AddAgentRequestAsync(string kind, int? configuredInstanceId, string? parameters, string? by, DateTimeOffset now, CancellationToken cancellationToken = default) { await using var connection = await OpenAsync(cancellationToken).ConfigureAwait(false); return await connection.ExecuteScalarAsync(""" - INSERT insight.AgentRequests (Kind, ConfiguredInstanceId, Parameters, Status, RequestedBy, RequestedAt) - OUTPUT INSERTED.RequestId - VALUES (@kind, @configuredInstanceId, @parameters, 'Pending', @by, @now); + DECLARE @id bigint = (SELECT TOP (1) RequestId FROM insight.AgentRequests + WHERE Status = 'Pending' AND Kind = @kind + AND (ConfiguredInstanceId = @configuredInstanceId OR (@configuredInstanceId IS NULL AND ConfiguredInstanceId IS NULL)) + AND ISNULL(Parameters, N'') = ISNULL(@parameters, N'') + ORDER BY RequestId DESC); + IF @id IS NULL + BEGIN + INSERT insight.AgentRequests (Kind, ConfiguredInstanceId, Parameters, Status, RequestedBy, RequestedAt) + VALUES (@kind, @configuredInstanceId, @parameters, 'Pending', @by, @now); + SET @id = SCOPE_IDENTITY(); + END + SELECT @id; """, new { kind, configuredInstanceId, parameters, by, now = Utc(now) }).ConfigureAwait(false); }