/// <summary> /// This method saves the description (eventString) and timestamp of the recentmost induced fault as /// a ChaosEntry in a Reliable Dictionary /// </summary> /// <param name="eventString"></param> /// <returns>A task to await on</returns> private async Task StoreEventAsync(string eventString) { ServiceEventSource.Current.ServiceMessage(this, "ChaosTest: {0}", eventString); IReliableDictionary <string, long> eventCount = await this.StateManager.GetOrAddAsync <IReliableDictionary <string, long> >(StringResource.EventCountKey); IReliableDictionary <string, DateTime> startTime = await this.StateManager.GetOrAddAsync <IReliableDictionary <string, DateTime> >(StringResource.StartTimeKey); IReliableDictionary <long, ChaosEntry> savedEvents = await this.StateManager.GetOrAddAsync <IReliableDictionary <long, ChaosEntry> >(StringResource.SavedEventsKey); using (ITransaction tx = this.StateManager.CreateTransaction()) { if (!await startTime.ContainsKeyAsync(tx, StringResource.StartTimeKey)) { await startTime.AddAsync(tx, StringResource.StartTimeKey, DateTime.UtcNow); } if (!await eventCount.ContainsKeyAsync(tx, StringResource.EventCountKey)) { await eventCount.AddAsync(tx, StringResource.EventCountKey, 0); } ConditionalValue <long> result = await eventCount.TryGetValueAsync(tx, StringResource.EventCountKey, LockMode.Update); if (result.HasValue) { long currentCount = result.Value; // If we have HistoryLength number of events, we make room for new events by removing oldest ones, // always keeping HistoryLength number of recentmost events on the show on the webpage. if (currentCount > Constants.HistoryLength - 1) { await savedEvents.TryRemoveAsync(tx, currentCount - Constants.HistoryLength + 1); } ChaosEntry chaosEntry = new ChaosEntry { Record = eventString, TimeStamp = DateTime.UtcNow.ToString(CultureInfo.InvariantCulture) }; await savedEvents.AddAsync(tx, ++currentCount, chaosEntry); await eventCount.SetAsync(tx, StringResource.EventCountKey, currentCount); await tx.CommitAsync(); } } }
/// <summary> /// This method saves the description (eventString) and timestamp of the recentmost induced fault as /// a ChaosEntry in a Reliable Dictionary /// </summary> /// <param name="eventString"></param> /// <returns>A task to await on</returns> private async Task StoreEventAsync(string eventString) { ServiceEventSource.Current.ServiceMessage(this, "ChaosTest: {0}", eventString); IReliableDictionary<string, long> eventCount = await this.StateManager.GetOrAddAsync<IReliableDictionary<string, long>>(StringResource.EventCountKey); IReliableDictionary<string, DateTime> startTime = await this.StateManager.GetOrAddAsync<IReliableDictionary<string, DateTime>>(StringResource.StartTimeKey); IReliableDictionary<long, ChaosEntry> savedEvents = await this.StateManager.GetOrAddAsync<IReliableDictionary<long, ChaosEntry>>(StringResource.SavedEventsKey); using (ITransaction tx = this.StateManager.CreateTransaction()) { if (!await startTime.ContainsKeyAsync(tx, StringResource.StartTimeKey)) { await startTime.AddAsync(tx, StringResource.StartTimeKey, DateTime.UtcNow); } if (!await eventCount.ContainsKeyAsync(tx, StringResource.EventCountKey)) { await eventCount.AddAsync(tx, StringResource.EventCountKey, 0); } ConditionalValue<long> result = await eventCount.TryGetValueAsync(tx, StringResource.EventCountKey, LockMode.Update); if (result.HasValue) { long currentCount = result.Value; // If we have HistoryLength number of events, we make room for new events by removing oldest ones, // always keeping HistoryLength number of recentmost events on the show on the webpage. if (currentCount > Constants.HistoryLength - 1) { await savedEvents.TryRemoveAsync(tx, currentCount - Constants.HistoryLength + 1); } ChaosEntry chaosEntry = new ChaosEntry { Record = eventString, TimeStamp = DateTime.UtcNow.ToString(CultureInfo.InvariantCulture) }; await savedEvents.AddAsync(tx, ++currentCount, chaosEntry); await eventCount.SetAsync(tx, StringResource.EventCountKey, currentCount); await tx.CommitAsync(); } } }