From 10e9406e24d46b264ecfda1064825b892590692b Mon Sep 17 00:00:00 2001 From: imranmomin Date: Fri, 6 Apr 2018 14:44:29 -0400 Subject: [PATCH] refrence to System.ValueType from named Tuples --- .../CountersAggregator.cs | 17 ++++++------ Hangfire.AzureDocumentDB/ExpirationManager.cs | 27 +++---------------- .../Hangfire.AzureDocumentDB.csproj | 1 + 3 files changed, 13 insertions(+), 32 deletions(-) diff --git a/Hangfire.AzureDocumentDB/CountersAggregator.cs b/Hangfire.AzureDocumentDB/CountersAggregator.cs index 97c093b..adb5a07 100644 --- a/Hangfire.AzureDocumentDB/CountersAggregator.cs +++ b/Hangfire.AzureDocumentDB/CountersAggregator.cs @@ -44,15 +44,14 @@ public void Execute(CancellationToken cancellationToken) .AsEnumerable() .ToList(); - Dictionary> counters = rawCounters.GroupBy(c => c.Key) - .ToDictionary(k => k.Key, v => new Tuple(v.Sum(c => c.Value), v.Max(c => c.ExpireOn))); + Dictionary 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 data; - if (counters.TryGetValue(key, out data)) + if (counters.TryGetValue(key, out var data)) { Counter aggregated = storage.Client.CreateDocumentQuery(storage.CollectionUri, queryOptions) .Where(c => c.Key == key && c.Type == CounterTypes.Aggregrate && c.DocumentType == DocumentTypes.Counter) @@ -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> task = storage.Client.UpsertDocumentWithRetriesAsync(storage.CollectionUri, aggregated); @@ -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(); } diff --git a/Hangfire.AzureDocumentDB/ExpirationManager.cs b/Hangfire.AzureDocumentDB/ExpirationManager.cs index 5bbc5c4..0de3451 100644 --- a/Hangfire.AzureDocumentDB/ExpirationManager.cs +++ b/Hangfire.AzureDocumentDB/ExpirationManager.cs @@ -16,7 +16,7 @@ internal class ExpirationManager : IServerComponent private static readonly ILog logger = LogProvider.For(); 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; @@ -30,15 +30,14 @@ 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> procedureTask = storage.Client.ExecuteStoredProcedureAsync(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); } @@ -46,22 +45,4 @@ public void Execute(CancellationToken cancellationToken) } } } - - 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)); - } - } } \ No newline at end of file diff --git a/Hangfire.AzureDocumentDB/Hangfire.AzureDocumentDB.csproj b/Hangfire.AzureDocumentDB/Hangfire.AzureDocumentDB.csproj index b1c6855..5adfbae 100644 --- a/Hangfire.AzureDocumentDB/Hangfire.AzureDocumentDB.csproj +++ b/Hangfire.AzureDocumentDB/Hangfire.AzureDocumentDB.csproj @@ -31,6 +31,7 @@ +