private async Task ProcessHopPendingItem(string hopEventId, string[] itemList)
        {
            var hopAdapter      = new Entt_HopAdapter();
            var hopPendingPolly = await Policy.Handle <SqlException>(e => e.IsTimeout())
                                  .WaitAndRetryAsync(RetryCount, x => TimeSpan.FromMilliseconds(500 * Math.Pow(2, x)))
                                  .ExecuteAndCaptureAsync(async() => await hopAdapter.LoadOneAsync(hopEventId));

            if (null != hopPendingPolly.FinalException)
            {
                throw new Exception("Process Hop Pending Polly Error", hopPendingPolly.FinalException);
            }

            var hop  = hopPendingPolly.Result;
            var hops = new List <Entt_Hop>();

            foreach (var item in itemList)
            {
                var console = IsConsole(item);
                var child   = hop.Clone();
                child.Id            = GenerateId(34);
                child.ConsignmentNo = item;
                child.DataFlag      = "1";
                child.ItemTypeCode  = console ? "02" : "01";
                hops.Add(child);
            }
            foreach (var item in hops)
            {
                var pr = Policy.Handle <SqlException>(e => e.IsDeadlockOrTimeout())
                         .WaitAndRetryAsync(RetryCount, x => TimeSpan.FromMilliseconds(500 * Math.Pow(2, x)))
                         .ExecuteAndCaptureAsync(() => hopAdapter.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");
            }
        }
Пример #2
0
        public async Task RunAsync(RecordHop hop)
        {
            //console_details
            var consoleList = new List <string>();

            if (IsConsole(hop.ConsignmentNo))
            {
                consoleList.Add(hop.ConsignmentNo);
            }

            var hopEventMap   = new RecordHopToEnttHop();
            var hopConsoleRow = await hopEventMap.TransformAsync(hop);

            hopConsoleRow.Id = GenerateId(34);
            m_hopEventRows.Add(hopConsoleRow);

            var consoleItem = await GetItemConsigmentsFromConsoleDetailsAsync(hop.ConsignmentNo);

            if (null != consoleItem)
            {
                var children = consoleItem.Split(new[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var item in children)
                {
                    if (consoleList.Contains(item))
                    {
                        continue;
                    }
                    ProcessChild(hopConsoleRow, 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(hopConsoleRow, 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(hopConsoleRow, ccc);
                                        }
                                    }
                                    else
                                    {
                                        AddPendingItems(hopConsoleRow.Id, cc);
                                    }
                                }
                            }
                        }
                        else
                        {
                            AddPendingItems(hopConsoleRow.Id, item);
                        }
                    }
                }
            }
            else
            {
                AddPendingItems(hopConsoleRow.Id, hop.ConsignmentNo);
            }

            var hopEventAdapter = new Entt_HopAdapter();

            foreach (var item in m_hopEventRows)
            {
                var pr = Policy.Handle <SqlException>()
                         .WaitAndRetryAsync(5, x => TimeSpan.FromMilliseconds(500 * Math.Pow(2, x)))
                         .ExecuteAndCaptureAsync(() => hopEventAdapter.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 Entt_EventPendingConsoleAdapter();

            foreach (var item in m_hopEventPendingConsoleRows)
            {
                var pr = Policy.Handle <SqlException>()
                         .WaitAndRetryAsync(5, x => TimeSpan.FromMilliseconds(500 * Math.Pow(2, x)))
                         .ExecuteAndCaptureAsync(() => 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");
            }
        }