public async Task Run( [QueueTrigger("people-batch", Connection = "AzureWebJobsStorage")] PeopleMessage queueItem, IBinder binder, ILogger log) { // Get the Batch ID from the incoming people message (from Storage Queue) var batchId = queueItem.BatchId; log.LogInformation($"INFO: PeopleQueueTrigger: Received people message with Batch ID: {batchId}"); // Iterate over all person records in the incoming people message queue item foreach (var person in queueItem.People) { try { // Apply busines rules to validate or enhance data _personRules.ApplyBusinessRules(person); // Add the person record to a data set to be batch processed _personRepository.UpsertPerson(person); } catch (Exception ex) { log.LogError($"ERROR: PeopleQueueTrigger: Unable to upsert person record with Batch ID = {person.PersonId}.", ex); // Rethrow error to ensure message gets moved to poison queue throw; } } try { // TODO: Remove the following three lines as // they are only meant to test the poison queue if (queueItem.BatchId.EndsWith("-3")) { throw new ApplicationException($"Bad data in Batch ID: {queueItem.BatchId}"); } // Save all person records to the SQL Server database await _personRepository.SaveChangesAsync(); log.LogInformation($"INFO: PeopleQueueTrigger: Successfully saved person records for Batch ID: {batchId}"); } catch (Exception ex) { log.LogError($"ERROR: PeopleQueueTrigger: Unable to save person records for Batch ID: {batchId}", ex); // Rethrow error to ensure message gets moved to poison queue throw; } }
private static int QueueBatch(string name, ICollector <PeopleMessage> queueCollector, int batch, PeopleMessage peopleMessage, ILogger log) { batch++; // Create Batch ID based on blob filename plus batch increment number var batchId = $"{name}-{batch}"; try { // Set people message Batch ID peopleMessage.BatchId = batchId; // Add people message to Storage Queue queueCollector.Add(peopleMessage); // Reset message properties peopleMessage.BatchId = string.Empty; peopleMessage.People.Clear(); } catch (Exception ex) { log.LogError($"ERROR: PeopleBlobTrigger: Unable to queue Batch ID: {batchId}", ex); throw; } return(batch); }
private void createMessageFor(RejectCancelDeleteInbetweenClass rcdbc, BuySellDoc buySellDoc) { string fromPersonId = ""; string toPersonId = ""; Person fromPerson = null; List <string> listOfToPeople = new List <string>(); string buySellDocId = buySellDoc.Id; Person ownerPerson = OwnerBiz.GetPersonForPlayer(buySellDoc.OwnerId); Person customerPerson = CustomerBiz.GetPersonForPlayer(buySellDoc.CustomerId); ownerPerson.IsNullThrowException(); customerPerson.IsNullThrowException(); switch (buySellDoc.BuySellDocumentTypeEnum) { case BuySellDocumentTypeENUM.Sale: fromPersonId = ownerPerson.Id; fromPerson = ownerPerson; toPersonId = customerPerson.Id; listOfToPeople.Add(toPersonId); break; case BuySellDocumentTypeENUM.Purchase: toPersonId = ownerPerson.Id; fromPersonId = customerPerson.Id; fromPerson = customerPerson; listOfToPeople.Add(toPersonId); break; case BuySellDocumentTypeENUM.Delivery: Person deliveryPerson = DeliverymanBiz.GetPersonForPlayer(buySellDoc.DeliverymanId); deliveryPerson.IsNullThrowException(); fromPersonId = deliveryPerson.Id; fromPerson = deliveryPerson; listOfToPeople.Add(ownerPerson.Id); listOfToPeople.Add(customerPerson.Id); break; case BuySellDocumentTypeENUM.Unknown: default: throw new Exception("Unknown document type"); } Message message = new Message( fromPersonId, fromPerson, listOfToPeople, rcdbc.Subject, rcdbc.Comment, MessageENUM.Free, null); if (buySellDoc.Messages.IsNull()) { buySellDoc.Messages = new List <Message>(); } buySellDoc.Messages.Add(message); message.BuySellDocId = buySellDoc.Id; message.ListOfToPeopleId.IsNullOrEmptyThrowException(); foreach (var pplId in message.ListOfToPeopleId) { Person person = PersonBiz.Find(pplId); person.IsNullThrowException(); PeopleMessage pplMsg = new PeopleMessage(); pplMsg.IsNullThrowException(); pplMsg.PersonId = pplId; pplMsg.Person = person; pplMsg.MessageId = message.Id; PeopleMessageBiz.Create(pplMsg); } MessageBiz.Create(message); }
public static void Run( [BlobTrigger("people-upload/{name}", Connection = "AzureWebJobsStorage")] Stream blob, string name, ILogger log, [Queue("people-batch", Connection = "AzureWebJobsStorage")] ICollector <PeopleMessage> queueCollector) { log.LogInformation($"INFO: PeopleBlobTrigger: Received uploaded blob\n Name:{name} \n Size: {blob.Length} Bytes"); var startTime = DateTime.UtcNow; log.LogInformation($"INFO: PeopleBlobTrigger: Started processing {name} at {DateTime.UtcNow.ToString("MM-dd-yyy HH:mm:ss")}"); using (var sr = new StreamReader(blob)) using (var csvReader = new CsvReader(sr)) { // Configure CsvReader options csvReader.Configuration.TrimOptions = TrimOptions.Trim; // Trim all whitespaces from fields csvReader.Configuration.Comment = '@'; // Set comment start character. Default is '#' csvReader.Configuration.AllowComments = true; // Allow comments in file csvReader.Configuration.IgnoreBlankLines = true; // Ignore blank lines in file csvReader.Configuration.Delimiter = ","; // Set the field delimiter character csvReader.Configuration.HasHeaderRecord = true; // Ensure a header row exists IEnumerable <Person> people = null; try { csvReader.Configuration.RegisterClassMap <PersonMap>(); people = csvReader.GetRecords <Person>(); } catch (Exception ex) { log.LogError("ERROR: PeopleBlobTrigger: Unable to read input blob", ex); return; } int count = 0; // Current counter increment int max = 100; // Maximum number of person records to process in a batch int batch = 0; // Current batch increment number var peopleMessage = new PeopleMessage(); foreach (var person in people) { count++; // Add person to PeopleMessage peopleMessage.People.Add(person); if (count >= max) { count = 0; // Add the current (fully populated) people message to a Storage Queue batch = QueueBatch(name, queueCollector, batch, peopleMessage, log); } } if (count > 0) { count = 0; // Add the final (partially populated) people message to a Storage Queue batch = QueueBatch(name, queueCollector, batch, peopleMessage, log); } } log.LogInformation($"INFO: PeopleBlobTrigger: Finished processing {name} at {DateTime.UtcNow.ToString("MM-dd-yyy HH:mm:ss")}"); }