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) <noreply@anthropic.com>
This commit is contained in:
Søren Eggert Lundsteen Olsen 2026-10-02 13:02:35 +02:00
parent 2adeef4597
commit cade91aa4a

View file

@ -307,15 +307,27 @@ public sealed partial class InsightRepository
return unknown; return unknown;
} }
/// <summary>Queues a request for the agent (SQLInsight Web: INSERT only).</summary> /// <summary>
/// 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.
/// </summary>
public async Task<long> AddAgentRequestAsync(string kind, int? configuredInstanceId, string? parameters, string? by, DateTimeOffset now, public async Task<long> AddAgentRequestAsync(string kind, int? configuredInstanceId, string? parameters, string? by, DateTimeOffset now,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
await using var connection = await OpenAsync(cancellationToken).ConfigureAwait(false); await using var connection = await OpenAsync(cancellationToken).ConfigureAwait(false);
return await connection.ExecuteScalarAsync<long>(""" return await connection.ExecuteScalarAsync<long>("""
INSERT insight.AgentRequests (Kind, ConfiguredInstanceId, Parameters, Status, RequestedBy, RequestedAt) DECLARE @id bigint = (SELECT TOP (1) RequestId FROM insight.AgentRequests
OUTPUT INSERTED.RequestId WHERE Status = 'Pending' AND Kind = @kind
VALUES (@kind, @configuredInstanceId, @parameters, 'Pending', @by, @now); 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); """, new { kind, configuredInstanceId, parameters, by, now = Utc(now) }).ConfigureAwait(false);
} }