public async Task RunAsync(Deliveries.Domain.Delivery deli) { //console_details var consoleList = new List <string>(); if (IsConsole(deli.ConsignmentNo)) { consoleList.Add(deli.ConsignmentNo); } var deliEventAdapter = new Adapters.Oal.dbo_delivery_event_newAdapter(); var deliWwpEventAdapter = new Adapters.Oal.dbo_wwp_event_new_logAdapter(); var deliEventMap = new Integrations.Transforms.RtsDeliveryToOalDboDeliveryEventNew(); var deliIpsImportMap = new Integrations.Transforms.RtsDeliveryToIpsImport(); var parentRow = await deliEventMap.TransformAsync(deli); var parentIpsRow = await deliIpsImportMap.TransformAsync(deli); parentRow.id = GenerateId(34); m_deliEventRows.Add(parentRow); var deliWwpEventLogMap = new Integrations.Transforms.RtsDeliveryOalWwpEventNewLog(); var parentWwpRow = await deliWwpEventLogMap.TransformAsync(deli); m_deliWwpEventLogRows.Add(parentWwpRow); var consoleItem = await GetItemConsigmentsFromConsoleDetailsAsync(deli.ConsignmentNo); if (null != consoleItem) { var children = consoleItem.Split(new[] { '\t' }, StringSplitOptions.RemoveEmptyEntries); foreach (var item in children) { if (consoleList.Contains(item)) { continue; } ProcessChild(parentRow, item); ProcessChildWwp(parentWwpRow, item); if (IsIpsImportItem(item)) { await ProcessChildIpsImport(parentIpsRow, item); } //2 level var console = IsConsole(item); if (console) { consoleList.Add(item); var childConsole = await GetItemConsigmentsFromConsoleDetailsAsync(item); if (null != childConsole) { var childConsoleItems = childConsole.Split(new[] { '\t' }, StringSplitOptions.RemoveEmptyEntries); foreach (var cc in childConsoleItems) { if (consoleList.Contains(cc)) { continue; } ProcessChild(parentRow, cc); ProcessChildWwp(parentWwpRow, cc); if (IsIpsImportItem(cc)) { await ProcessChildIpsImport(parentIpsRow, cc); } //3 level var anotherConsole = IsConsole(cc); if (anotherConsole) { consoleList.Add(cc); var anotherChildConsole = await GetItemConsigmentsFromConsoleDetailsAsync(cc); if (null != anotherChildConsole) { var anotherChildConsoleItems = anotherChildConsole.Split(new[] { '\t' }, StringSplitOptions.RemoveEmptyEntries); foreach (var ccc in anotherChildConsoleItems) { if (consoleList.Contains(ccc)) { continue; } ProcessChild(parentRow, ccc); ProcessChildWwp(parentWwpRow, ccc); if (IsIpsImportItem(ccc)) { await ProcessChildIpsImport(parentIpsRow, ccc); } } } else { AddPendingItems(parentRow.id, parentWwpRow.id, cc); } } } } else { AddPendingItems(parentRow.id, parentWwpRow.id, item); } } } } else { AddPendingItems(parentRow.id, parentWwpRow.id, deli.ConsignmentNo); } foreach (var item in m_deliEventRows) { var pr = Policy.Handle <SqlException>() .WaitAndRetryAsync(5, x => TimeSpan.FromMilliseconds(500 * Math.Pow(2, x))) .ExecuteAndCaptureAsync(() => TrackEvents(deliEventAdapter.InsertAsync, item)); var result = await pr; if (result.FinalException != null) { throw result.FinalException; // send to dead letter queue } System.Diagnostics.Debug.Assert(result.Result > 0, "Should be at least 1 row"); } foreach (var item in m_deliWwpEventLogRows) { var pr = Policy.Handle <SqlException>() .WaitAndRetryAsync(5, x => TimeSpan.FromMilliseconds(500 * Math.Pow(2, x))) .ExecuteAndCaptureAsync(() => TrackEvents(deliWwpEventAdapter.InsertAsync, item)); var result = await pr; if (result.FinalException != null) { throw result.FinalException; // send to dead letter queue } System.Diagnostics.Debug.Assert(result.Result > 0, "Should be at least 1 row"); } var ipsAdapter = new Adapters.Oal.dbo_ips_importAdapter(); foreach (var item in m_deliIpsImportEventRows) { var pr = Policy.Handle <SqlException>() .WaitAndRetryAsync(5, x => TimeSpan.FromMilliseconds(500 * Math.Pow(2, x))) .ExecuteAndCaptureAsync(() => TrackEvents(ipsAdapter.InsertAsync, item)); var result = await pr; if (result.FinalException != null) { throw result.FinalException; // send to dead letter queue } System.Diagnostics.Debug.Assert(result.Result > 0, "Should be at least 1 row"); } var pendingAdapter = new Adapters.Oal.dbo_event_pending_consoleAdapter(); foreach (var item in m_deliEventPendingConsoleRows) { var pr = Policy.Handle <SqlException>() .WaitAndRetryAsync(5, x => TimeSpan.FromMilliseconds(500 * Math.Pow(2, x))) .ExecuteAndCaptureAsync(() => TrackEvents(pendingAdapter.InsertAsync, item)); var result = await pr; if (result.FinalException != null) { throw result.FinalException; // send to dead letter queue } System.Diagnostics.Debug.Assert(result.Result > 0, "Should be at least 1 row"); } }
private async Task ProcessDeliveryPendingItem(string deliEventId, string[] itemList) { var deliAdapter = new Adapters.Oal.dbo_delivery_event_newAdapter(); var ipsAdapter = new Adapters.Oal.dbo_ips_importAdapter(); var deliPendingPolly = await Policy.Handle <SqlException>(e => e.IsTimeout()) .WaitAndRetryAsync(RetryCount, WaitInterval) .ExecuteAndCaptureAsync(async() => await deliAdapter.LoadOneAsync(deliEventId)); if (null != deliPendingPolly.FinalException) { throw new Exception("Process Deli Pending Polly Error", deliPendingPolly.FinalException); } var deli = deliPendingPolly.Result; var deliItems = new List <Adapters.Oal.dbo_delivery_event_new>(); var ipsItems = new List <Adapters.Oal.dbo_ips_import>(); foreach (var item in itemList) { var console = IsConsole(item); var child = deli.Clone(); child.id = GenerateId(34); child.consignment_no = item; child.data_flag = "1"; child.item_type_code = console ? "02" : "01"; deliItems.Add(child); if (IsIpsImportItem(item)) { var ips = await CreateDeliIpsImport(deli, item); ipsItems.Add(ips); } } foreach (var item in deliItems) { var pr = Policy.Handle <SqlException>(e => e.IsDeadlockOrTimeout()) .WaitAndRetryAsync(RetryCount, WaitInterval) .ExecuteAndCaptureAsync(() => deliAdapter.InsertAsync(item)); var result = await pr; if (result.FinalException != null) { throw result.FinalException; // send to dead letter queue } System.Diagnostics.Debug.Assert(result.Result > 0, "Should be at least 1 row"); } foreach (var item in ipsItems) { var pr = Policy.Handle <SqlException>(e => e.IsDeadlockOrTimeout()) .WaitAndRetryAsync(RetryCount, WaitInterval) .ExecuteAndCaptureAsync(() => ipsAdapter.InsertAsync(item)); var result = await pr; if (result.FinalException != null) { throw result.FinalException; // send to dead letter queue } System.Diagnostics.Debug.Assert(result.Result > 0, "Should be at least 1 row"); } }