Skip to content

Commit

Permalink
Clean up ClearML authentication error reporting.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnml1135 committed Jan 11, 2024
1 parent cc532fe commit 4058b1a
Showing 1 changed file with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class ClearMLAuthenticationService : RecurrentTask, IClearMLAuthenticatio
// 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.
private static readonly TimeSpan RefreshPeriod = TimeSpan.FromSeconds(3600);
private string _authToken = "";
private string? _authToken = "";

public ClearMLAuthenticationService(
IServiceProvider services,
Expand All @@ -29,14 +29,14 @@ public async Task<string> GetAuthTokenAsync(CancellationToken cancellationToken
{
using (await _lock.LockAsync(cancellationToken))
{
if (_authToken is "")
if (_authToken is null || _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;
return _authToken ?? throw new Exception("ClearML authentication token not found in response.");
}

protected override async Task DoWorkAsync(IServiceScope scope, CancellationToken cancellationToken)
Expand All @@ -49,6 +49,9 @@ protected override async Task DoWorkAsync(IServiceScope scope, CancellationToken
catch (Exception e)
{
_logger.LogError(e, "Error occurred while refreshing ClearML authentication token.");
if (_authToken is null || _authToken is "")
// The ClearML token never was set. We can't continue without it.
throw;
}
}

Expand All @@ -64,6 +67,8 @@ private async Task AuthorizeAsync(CancellationToken cancellationToken)
HttpResponseMessage response = await _httpClient.SendAsync(request, cancellationToken);
string result = await response.Content.ReadAsStringAsync(cancellationToken);
_authToken = (string)((JsonObject?)JsonNode.Parse(result))?["data"]?["token"]!;
if (_authToken is null || _authToken is "")
throw new Exception($"ClearML authentication failed - {response.StatusCode}: {response.ReasonPhrase}");
_logger.LogInformation("ClearML Authentication Token Refresh Successful.");
}
}

0 comments on commit 4058b1a

Please sign in to comment.