public async Task Save(EventDataWrapper eventDataWrapper) { try { var policies = _policyRegistry.CreateAsyncPolicies(); await Polly.Policy.WrapAsync(policies).ExecuteAsync(async() => { var storageAccount = CloudStorageAccount.Parse(_configuration[Constants.Storage.ConnectionString]); var tableClient = storageAccount.CreateCloudTableClient(); var table = tableClient.GetTableReference(Constants.Storage.PoisonTableName); await table.CreateIfNotExistsAsync(); var insertOperation = TableOperation.Insert(new PoisonMessageEntity(eventDataWrapper)); await table.ExecuteAsync(insertOperation); }); } catch (Exception ex) { _logger?.LogError(ex, "Unable to save poison message into table"); } }
public async Task ProcessAsync(IEnumerable <EventDataWrapper> messages) { var policies = _policyRegistry.CreateAsyncPolicies(); var repository = _repositoryFactory.Create(); var groupedMessages = messages.Where(t => t.Properties.ContainsKey(Constants.VehicleId)) .GroupBy(t => t.Properties[Constants.VehicleId]); foreach (var message in groupedMessages) { var id = message.Key.ToString(); _logger?.LogDebug($"Process event for vehicle {id}"); var vehicleSnapshot = repository.Get(id) ?? new VehicleSnapshot { Id = id }; message .Select(t => Deserialize(t)) .Where(t => t != null && t.Quality >= Constants.QualityGate) .ToList().ForEach(s => { if (vehicleSnapshot.Sensors.ContainsKey(s.Name)) { ReplaceSensorData(s, vehicleSnapshot); } else { vehicleSnapshot.Sensors.Add(s.Name, VehicleSensorData.Create(s)); } }); await Polly.Policy.WrapAsync(policies).ExecuteAsync(async() => { await repository.AddAsync(vehicleSnapshot); _logger?.LogInformation($"Save snapshot for vehicle {id}"); }); } ; }