diff --git a/src/SIL.Machine.AspNetCore/Services/ClearMLAuthenticationService.cs b/src/SIL.Machine.AspNetCore/Services/ClearMLAuthenticationService.cs index adbe3755b..9dba80f56 100644 --- a/src/SIL.Machine.AspNetCore/Services/ClearMLAuthenticationService.cs +++ b/src/SIL.Machine.AspNetCore/Services/ClearMLAuthenticationService.cs @@ -5,6 +5,7 @@ public class ClearMLAuthenticationService : BackgroundService, IClearMLAuthentic private readonly HttpClient _httpClient; private readonly IOptionsMonitor _options; private readonly ILogger _logger; + private readonly AsyncLock _lock = new(); // technically, the token should be good for 30 days, but let's refresh each hour // to know well ahead of time if something is wrong. @@ -25,11 +26,14 @@ ILogger logger public async Task GetAuthToken(CancellationToken cancellationToken = default) { - if (_authToken is "") + using (await _lock.LockAsync()) { - //Should only happen once, so no different in cost than previous solution - _logger.LogInformation("Token was empty; refreshing"); - await AuthorizeAsync(cancellationToken); + if (_authToken is "") + { + //Should only happen once, so no different in cost than previous solution + _logger.LogInformation("Token was empty; refreshing"); + await AuthorizeAsync(cancellationToken); + } } return _authToken; } diff --git a/src/SIL.Machine.AspNetCore/Services/ClearMLHealthCheck.cs b/src/SIL.Machine.AspNetCore/Services/ClearMLHealthCheck.cs index 55f9d6ff3..ced15ea5c 100644 --- a/src/SIL.Machine.AspNetCore/Services/ClearMLHealthCheck.cs +++ b/src/SIL.Machine.AspNetCore/Services/ClearMLHealthCheck.cs @@ -26,7 +26,7 @@ public async Task CheckHealthAsync( } catch (Exception e) { - _logger.LogError(0, exception: e, null); + _logger.LogError(e, null); return HealthCheckResult.Unhealthy(exception: e); } }