Skip to content
This repository has been archived by the owner on Feb 2, 2022. It is now read-only.

Commit

Permalink
refrence to System.ValueType from named Tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
imranmomin committed Apr 6, 2018
1 parent 722037b commit 10e9406
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 32 deletions.
17 changes: 8 additions & 9 deletions Hangfire.AzureDocumentDB/CountersAggregator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,14 @@ public void Execute(CancellationToken cancellationToken)
.AsEnumerable()
.ToList();

Dictionary<string, Tuple<int, DateTime?>> counters = rawCounters.GroupBy(c => c.Key)
.ToDictionary(k => k.Key, v => new Tuple<int, DateTime?>(v.Sum(c => c.Value), v.Max(c => c.ExpireOn)));
Dictionary<string, (int Sum, DateTime? ExpireOn)> counters = rawCounters.GroupBy(c => c.Key)
.ToDictionary(k => k.Key, v=> (Sum: v.Sum(c => c.Value), ExpireOn: v.Max(c => c.ExpireOn)));

Array.ForEach(counters.Keys.ToArray(), key =>
{
cancellationToken.ThrowIfCancellationRequested();

Tuple<int, DateTime?> data;
if (counters.TryGetValue(key, out data))
if (counters.TryGetValue(key, out var data))
{
Counter aggregated = storage.Client.CreateDocumentQuery<Counter>(storage.CollectionUri, queryOptions)
.Where(c => c.Key == key && c.Type == CounterTypes.Aggregrate && c.DocumentType == DocumentTypes.Counter)
Expand All @@ -65,14 +64,14 @@ public void Execute(CancellationToken cancellationToken)
{
Key = key,
Type = CounterTypes.Aggregrate,
Value = data.Item1,
ExpireOn = data.Item2
Value = data.Sum,
ExpireOn = data.ExpireOn
};
}
else
{
aggregated.Value += data.Item1;
aggregated.ExpireOn = data.Item2;
aggregated.Value += data.Sum;
aggregated.ExpireOn = data.ExpireOn;
}

Task<ResourceResponse<Document>> task = storage.Client.UpsertDocumentWithRetriesAsync(storage.CollectionUri, aggregated);
Expand All @@ -95,7 +94,7 @@ public void Execute(CancellationToken cancellationToken)
logger.Trace("Records from the 'Counter' table aggregated.");
cancellationToken.WaitHandle.WaitOne(checkInterval);
}

public override string ToString() => GetType().ToString();

}
Expand Down
27 changes: 4 additions & 23 deletions Hangfire.AzureDocumentDB/ExpirationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal class ExpirationManager : IServerComponent
private static readonly ILog logger = LogProvider.For<ExpirationManager>();
private const string DISTRIBUTED_LOCK_KEY = "expirationmanager";
private static readonly TimeSpan defaultLockTimeout = TimeSpan.FromMinutes(5);
private static readonly string[] documents = { "locks", "jobs", "lists", "sets", "hashes", "counters" };
private static readonly DocumentTypes[] documents = { DocumentTypes.Lock, DocumentTypes.Job, DocumentTypes.List, DocumentTypes.Set, DocumentTypes.Hash, DocumentTypes.Counter };
private readonly TimeSpan checkInterval;
private readonly DocumentDbStorage storage;
private readonly Uri spDeleteExpiredDocumentsUri;
Expand All @@ -30,38 +30,19 @@ public ExpirationManager(DocumentDbStorage storage)

public void Execute(CancellationToken cancellationToken)
{
foreach (string document in documents)
foreach (DocumentTypes type in documents)
{
logger.Debug($"Removing outdated records from the '{document}' document.");
DocumentTypes type = document.ToDocumentType();
logger.Debug($"Removing outdated records from the '{type}' document.");

using (new DocumentDbDistributedLock(DISTRIBUTED_LOCK_KEY, defaultLockTimeout, storage))
{
Task<StoredProcedureResponse<int>> procedureTask = storage.Client.ExecuteStoredProcedureAsync<int>(spDeleteExpiredDocumentsUri, type);
Task task = procedureTask.ContinueWith(t => logger.Trace($"Outdated records removed {t.Result.Response} records from the '{document}' document."), TaskContinuationOptions.OnlyOnRanToCompletion);
Task task = procedureTask.ContinueWith(t => logger.Trace($"Outdated records removed {t.Result.Response} records from the '{type}' document."), TaskContinuationOptions.OnlyOnRanToCompletion);
task.Wait(cancellationToken);
}

cancellationToken.WaitHandle.WaitOne(checkInterval);
}
}
}

internal static class ExpirationManagerExtenison
{
internal static DocumentTypes ToDocumentType(this string document)
{
switch (document)
{
case "locks": return DocumentTypes.Lock;
case "jobs": return DocumentTypes.Job;
case "lists": return DocumentTypes.List;
case "sets": return DocumentTypes.Set;
case "hashes": return DocumentTypes.Hash;
case "counters": return DocumentTypes.Counter;
}

throw new ArgumentException(@"invalid document type", nameof(document));
}
}
}
1 change: 1 addition & 0 deletions Hangfire.AzureDocumentDB/Hangfire.AzureDocumentDB.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<ItemGroup>
<PackageReference Include="Hangfire.Core" Version="1.6.17" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="System.ValueTuple" Version="4.4.0" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="StoredProcedure\deleteDocumentIfExists.js" />
Expand Down

0 comments on commit 10e9406

Please sign in to comment.