Skip to content

Commit

Permalink
Fixes #133
Browse files Browse the repository at this point in the history
  • Loading branch information
Enkidu93 committed Nov 10, 2023
1 parent e1106b4 commit 2f3f052
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
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

0 comments on commit 2f3f052

Please sign in to comment.