Skip to content

Commit

Permalink
Merge branch 'main' into tgl-view
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNZL committed May 18, 2023
2 parents 2d0a793 + 7d2cc3f commit 0d09619
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions src/TogglTrack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ internal class TogglTrack
private TogglClient _client;
private (bool IsValid, string Token) _lastToken = (false, string.Empty);

private readonly (SemaphoreSlim Token, SemaphoreSlim Me, SemaphoreSlim RunningTimeEntries, SemaphoreSlim TimeEntries) _semaphores = (new SemaphoreSlim(1, 1), new SemaphoreSlim(1, 1), new SemaphoreSlim(1, 1), new SemaphoreSlim(1, 1));
private MemoryCache _cache = MemoryCache.Default;
private List<string> _summaryTimeEntriesCacheKeys = new List<string>();

Expand All @@ -46,8 +47,11 @@ internal TogglTrack(PluginInitContext context, Settings settings)
{
const string cacheKey = "Me";

if (!force && this._cache.Contains(cacheKey))
await this._semaphores.Me.WaitAsync();

if ((!force || this._semaphores.Me.CurrentCount == 0) && this._cache.Contains(cacheKey))
{
this._semaphores.Me.Release();
return (Me?)this._cache.Get(cacheKey);
}

Expand All @@ -61,11 +65,14 @@ internal TogglTrack(PluginInitContext context, Settings settings)
this._cache.Set(cacheKey, me, DateTimeOffset.Now.AddDays(3));
#pragma warning restore CS8604 // Possible null reference argument

this._semaphores.Me.Release();
return me;
}
catch (Exception exception)
{
this._context.API.LogException("TogglTrack", "Failed to fetch me", exception, "_GetMe");

this._semaphores.Me.Release();
return null;
}
}
Expand All @@ -74,26 +81,32 @@ internal TogglTrack(PluginInitContext context, Settings settings)
{
const string cacheKey = "RunningTimeEntry";

if (!force && this._cache.Contains(cacheKey))
await this._semaphores.RunningTimeEntries.WaitAsync();

if ((!force || this._semaphores.RunningTimeEntries.CurrentCount == 0) && this._cache.Contains(cacheKey))
{
this._semaphores.RunningTimeEntries.Release();
return (TimeEntry?)this._cache.Get(cacheKey);
}

try
{
this._context.API.LogInfo("TogglTrack", "Fetching running time entry", "_GetRunningTimeEntry");

var runningTimeEntry = await this._client.GetRunningTimeEntry();

#pragma warning disable CS8604 // Possible null reference argument
this._cache.Set(cacheKey, runningTimeEntry, DateTimeOffset.Now.AddSeconds(30));
#pragma warning restore CS8604 // Possible null reference argument

this._semaphores.RunningTimeEntries.Release();
return runningTimeEntry;
}
catch (Exception exception)
{
this._context.API.LogException("TogglTrack", "Failed to fetch running time entry", exception, "_GetRunningTimeEntry");

this._semaphores.RunningTimeEntries.Release();
return null;
}
}
Expand All @@ -102,8 +115,11 @@ internal TogglTrack(PluginInitContext context, Settings settings)
{
const string cacheKey = "TimeEntries";

await this._semaphores.TimeEntries.WaitAsync();

if (!force && this._cache.Contains(cacheKey))
{
this._semaphores.TimeEntries.Release();
return (List<TimeEntry>?)this._cache.Get(cacheKey);
}

Expand All @@ -114,14 +130,18 @@ internal TogglTrack(PluginInitContext context, Settings settings)
var timeEntries = await this._client.GetTimeEntries();

#pragma warning disable CS8604 // Possible null reference argument
// TODO: value cannot be null
this._cache.Set(cacheKey, timeEntries, DateTimeOffset.Now.AddSeconds(30));
#pragma warning restore CS8604 // Possible null reference argument

this._semaphores.TimeEntries.Release();
return timeEntries;
}
catch (Exception exception)
{
this._context.API.LogException("TogglTrack", "Failed to fetch time entries", exception, "_GetTimeEntries");

this._semaphores.TimeEntries.Release();
return null;
}
}
Expand Down Expand Up @@ -181,23 +201,28 @@ internal async ValueTask<bool> VerifyApiToken()
return false;
}

// TODO: this equal does not work
await this._semaphores.Token.WaitAsync();

if (this._settings.ApiToken.Equals(this._lastToken.Token))
{
this._semaphores.Token.Release();
return this._lastToken.IsValid;
}

this._lastToken.Token = this._settings.ApiToken;

if (string.IsNullOrWhiteSpace(this._settings.ApiToken))
{
this._semaphores.Token.Release();
return this._lastToken.IsValid = false;
}

this._client.UpdateToken(this._settings.ApiToken);


this._lastToken.IsValid = (await this._GetMe(true))?.api_token?.Equals(this._settings.ApiToken) ?? false;
this._lastToken.Token = this._settings.ApiToken;

this._semaphores.Token.Release();

if (this._lastToken.IsValid)
{
this.RefreshCache(true);
Expand Down

0 comments on commit 0d09619

Please sign in to comment.