Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #133 #135

Merged
merged 2 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public static IMachineBuilder AddClearMLService(this IMachineBuilder builder, st
builder.Services
.AddHttpClient("ClearML-NoRetry")
.ConfigureHttpClient(httpClient => httpClient.BaseAddress = new Uri(connectionString));

builder.Services.AddSingleton<ClearMLHealthCheck>();
builder.Services.AddHealthChecks().AddCheck<ClearMLHealthCheck>("ClearML Health Check");

return builder;
Expand Down
10 changes: 9 additions & 1 deletion src/SIL.Machine.AspNetCore/Services/ClearMLHealthCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ public class ClearMLHealthCheck : IHealthCheck
private readonly HttpClient _httpClient;
private readonly IOptionsMonitor<ClearMLOptions> _options;
private readonly IClearMLAuthenticationService _clearMLAuthenticationService;
private int _numConsecutiveFailures;
private readonly AsyncLock _lock;

public ClearMLHealthCheck(
IClearMLAuthenticationService clearMLAuthenticationService,
Expand All @@ -13,6 +15,8 @@ IOptionsMonitor<ClearMLOptions> options
_httpClient = httpClientFactory.CreateClient("ClearML-NoRetry");
_options = options;
_clearMLAuthenticationService = clearMLAuthenticationService;
_numConsecutiveFailures = 0;
_lock = new AsyncLock();
}

public async Task<HealthCheckResult> CheckHealthAsync(
Expand All @@ -28,11 +32,15 @@ public async Task<HealthCheckResult> CheckHealthAsync(
return HealthCheckResult.Unhealthy(
$"No ClearML agents are available for configured queue \"{_options.CurrentValue.Queue}\""
);
_numConsecutiveFailures = 0;
return HealthCheckResult.Healthy("ClearML is available");
}
catch (Exception e)
{
return HealthCheckResult.Unhealthy(exception: e);
_numConsecutiveFailures++;
return _numConsecutiveFailures > 3
? HealthCheckResult.Unhealthy(exception: e)
: HealthCheckResult.Degraded(exception: e);
}
}

Expand Down