public void Then1(string batchNumber, Table table) { var task = GenerateBulkCreditBus.GetSingleResponseAsync(5); task.Wait(); response = task.Result; Assert.IsNotNull(response, "No response received"); Assert.IsFalse(string.IsNullOrEmpty(response.transactions.First().bulkCreditVoucher.voucherBatch.scannedBatchNumber)); Assert.AreEqual(batchNumber, response.transactions.First().bulkCreditVoucher.voucherBatch.scannedBatchNumber); table.CompareToSet(response.transactions.Select(c => new { documentReferenceNumber = c.associatedDebitVouchers.First().documentReferenceNumber.Trim(), bsbNumber = c.associatedDebitVouchers.First().bsbNumber.Trim(), accountNumber = c.associatedDebitVouchers.First().accountNumber.Trim(), auxDom = c.associatedDebitVouchers.First().auxDom.Trim(), extraAuxDom = c.associatedDebitVouchers.First().extraAuxDom.Trim(), transactionCode = c.associatedDebitVouchers.First().transactionCode.Trim(), amount = c.associatedDebitVouchers.First().amount.Trim() })); }
/// <summary> /// Identify customer link number groups, create GenerateBulkCreditResponse elements for each distinct customer link number (i.e. the bulk credit and all other related vouchers) /// </summary> /// <param name="context"></param> public void Execute(IJobExecutionContext context) { Log.Information("Scanning database for completed generate bulk credit vouchers"); using (var lifetimeScope = component.BeginLifetimeScope()) { using (var dipsDbContext = dbContext ?? lifetimeScope.Resolve <IDipsDbContext>()) { // Get all the potential batches that have been completed var completedBatches = dipsDbContext.Queues .Where(q => !q.ResponseCompleted && q.S_LOCATION == DipsLocationType.GenerateBulkCreditVoucherDone.Value && q.S_LOCK.Trim() == "0") .ToList(); Log.Information("Found {0} completed generate bulk credit vouchers", completedBatches.Count); foreach (var completedBatch in completedBatches) { Log.Debug("Creating response for batch {@batch}", completedBatch.S_BATCH); // only commit the transaction if // a) we were the first application to mark this batch row as CorrectCodelineCompleted (DipsQueue uses optimistic concurrency) // b) we were able to place a response message on the bus using (var tx = dipsDbContext.BeginTransaction()) { try { // mark the line as completed completedBatch.ResponseCompleted = true; var routingKey = completedBatch.RoutingKey; dipsDbContext.SaveChanges(); var batchNumber = completedBatch.S_BATCH; var customerLinkBatches = dipsDbContext.NabChqPods.Where(n => n.S_BATCH == batchNumber).Select(n => new { n.customerLinkNumber }).Distinct().ToList(); var batchResponse = new GenerateBatchBulkCreditResponse(); List <GenerateBulkCreditResponse> bulkCredit = new List <GenerateBulkCreditResponse>(); var voucherBatch = dipsDbContext.NabChqPods .Where( v => v.S_BATCH == batchNumber) .ToList(); foreach (var customerLinkBatch in customerLinkBatches) { var vouchers = voucherBatch.Where(v => v.customerLinkNumber == customerLinkBatch.customerLinkNumber).ToList(); bulkCredit.AddRange(GenerateBulkCreditResponse(completedBatch, vouchers)); } batchResponse.transactions = bulkCredit.ToArray(); if (adapterConfiguration.DeleteDatabaseRows) { ResponseHelper.CleanupBatchData(batchNumber, dipsDbContext); } if (string.IsNullOrEmpty(routingKey)) { routingKey = string.Empty; } Task.WaitAll(responseExchange.PublishAsync(batchResponse, completedBatch.CorrelationId, routingKey.Trim())); tx.Commit(); Log.Debug( "Generate bulk credit voucher batch '{@batch}' has been completed and a response has been placed on the queue", completedBatch.S_BATCH); Log.Information("Batch '{@batch}' response sent: {@response}", completedBatch.S_BATCH, batchResponse.transactions); // note - Serilog does not seem able to output the batchResponse object } catch (OptimisticConcurrencyException) { //this is to handle the race condition where more than instance of this service is running at the same time and tries to update the row. //basically ignore the message by loggin a warning and rolling back. //if this row was not included by mistake (e.g. it should be included), it will just come in in the next batch run. Log.Warning( "Could not create a generate bulk credit voucher response for batch '{@batch}' because the DIPS database row was updated by another connection", completedBatch.S_BATCH); tx.Rollback(); } catch (Exception ex) { Log.Error( ex, "Could not complete and create a generate bulk credit voucher response for batch '{@batch}'", completedBatch.S_BATCH); tx.Rollback(); } } } } } Log.Information("Finished processing completed generate bulk credit voucher batches"); }