Enhance SwarmUI integration by adding swarm connection status to the API and updating client-side components to reflect this status. Improved localization strings for various SwarmUI states and refactored ApplicantsDialog and PersonCard components to utilize the new connection information, ensuring a more responsive user experience.
This commit is contained in:
@@ -28,6 +28,33 @@ internal sealed class SwarmUiClient
|
||||
|
||||
public bool IsConfigured => !string.IsNullOrWhiteSpace(_options.BaseUrl);
|
||||
|
||||
/// <summary>Opens a session to verify SwarmUI responds; does not generate an image.</summary>
|
||||
public async Task<bool> ProbeAsync(TimeSpan timeout, CancellationToken cancellationToken)
|
||||
{
|
||||
if (!IsConfigured)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
using var timeoutSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
||||
timeoutSource.CancelAfter(timeout);
|
||||
|
||||
try
|
||||
{
|
||||
await RefreshSessionAsync(timeoutSource.Token);
|
||||
return true;
|
||||
}
|
||||
catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogDebug(ex, "SwarmUI probe failed.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<byte[]> GenerateAsync(
|
||||
string prompt,
|
||||
string negativePrompt,
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
namespace HSchool.Server.Game;
|
||||
|
||||
/// <summary>Caches a lightweight SwarmUI reachability probe so /api/status stays cheap.</summary>
|
||||
internal sealed class SwarmUiHealthService(SwarmUiClient client)
|
||||
{
|
||||
private static readonly TimeSpan CacheTtl = TimeSpan.FromSeconds(15);
|
||||
private static readonly TimeSpan ProbeTimeout = TimeSpan.FromSeconds(5);
|
||||
|
||||
private readonly SemaphoreSlim _gate = new(1, 1);
|
||||
private bool? _lastConnected;
|
||||
private DateTime _lastCheckUtc = DateTime.MinValue;
|
||||
|
||||
/// <summary><c>null</c> when SwarmUI is not configured.</summary>
|
||||
public async Task<bool?> GetConnectedAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (!client.IsConfigured)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (DateTime.UtcNow - _lastCheckUtc < CacheTtl && _lastConnected.HasValue)
|
||||
{
|
||||
return _lastConnected;
|
||||
}
|
||||
|
||||
await _gate.WaitAsync(cancellationToken);
|
||||
try
|
||||
{
|
||||
if (DateTime.UtcNow - _lastCheckUtc < CacheTtl && _lastConnected.HasValue)
|
||||
{
|
||||
return _lastConnected;
|
||||
}
|
||||
|
||||
_lastConnected = await client.ProbeAsync(ProbeTimeout, cancellationToken);
|
||||
_lastCheckUtc = DateTime.UtcNow;
|
||||
return _lastConnected;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_gate.Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user