private async Task ProcessSipPendingItem(string sipEventId, string[] itemList)
        {
            var sipAdapter      = new Entt_SipAdapter();
            var sipPendingPolly = await Policy.Handle <SqlException>(e => e.IsTimeout())
                                  .WaitAndRetryAsync(RetryCount, x => TimeSpan.FromMilliseconds(500 * Math.Pow(2, x)))
                                  .ExecuteAndCaptureAsync(async() => await sipAdapter.LoadOneAsync(sipEventId));

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

            var sip  = sipPendingPolly.Result;
            var sips = new List <Entt_Sip>();

            foreach (var item in itemList)
            {
                var console = IsConsole(item);
                var child   = sip.Clone();
                child.Id            = GenerateId(34);
                child.ConsignmentNo = item;
                child.DataFlag      = "1";
                child.ItemTypeCode  = console ? "02" : "01";
                sips.Add(child);
            }
            foreach (var item in sips)
            {
                var pr = Policy.Handle <SqlException>(e => e.IsDeadlockOrTimeout())
                         .WaitAndRetryAsync(RetryCount, x => TimeSpan.FromMilliseconds(500 * Math.Pow(2, x)))
                         .ExecuteAndCaptureAsync(() => sipAdapter.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");
            }
        }
Exemplo n.º 2
0
        public async Task RunAsync(RecordSip sip)
        {
            //console_details
            var consoleList = new List <string>();

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

            var sipEventMap   = new ReportSipToEnttSip();
            var sipConsoleRow = await sipEventMap.TransformAsync(sip);

            sipConsoleRow.Id = GenerateId(34);
            m_sipEventRows.Add(sipConsoleRow);

            var consoleItem = await GetItemConsigmentsFromConsoleDetailsAsync(sip.ConsignmentNo);

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

            var sipEventAdapter = new Entt_SipAdapter();

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