示例#1
0
        private async Task Polling(CancellationToken cancellationToken)
        {
            var authRequiredEvent = new ManualResetEvent(true);
            var pollingReadyEvent = new ManualResetEvent(false);

            var authPolling = Task.Run(
                () => AuthPolling(cancellationToken, authRequiredEvent, pollingReadyEvent),
                cancellationToken);

            var informationPollingHelper = new PollingHelper(
                new Logger("StationInformationPolling", _configuration.LogLevel),
                StationInformation,
                pollingReadyEvent,
                authRequiredEvent);

            var ordersToSetPollingHelper = new PollingHelper(
                new Logger("OrdersToSetPolling", _configuration.LogLevel),
                OrdersToSetPolling,
                pollingReadyEvent,
                authRequiredEvent);

            var ordersToCancelPollingHelper = new PollingHelper(
                new Logger("OrdersToCancelPolling", _configuration.LogLevel),
                OrdersToCancelPolling,
                pollingReadyEvent,
                authRequiredEvent);

            await Task
            .WhenAll(
                authPolling,
                informationPollingHelper.Start(_informationPollingInterval, cancellationToken),
                ordersToSetPollingHelper.Start(_ordersToSetPollingInterval, cancellationToken),
                ordersToCancelPollingHelper.Start(_ordersToCancelPollingInterval, cancellationToken))
            .ConfigureAwait(false);
        }
示例#2
0
        private static readonly TimeSpan PollingInterval = TimeSpan.FromSeconds(1);   // Poll for token every 1 second.

        /// <summary>
        /// Inspects outgoing Activities for <see cref="OAuthCard">OAuthCards</see>.
        /// </summary>
        /// <param name="adapter">The BotFrameworkAdapter used for polling the token service.</param>
        /// <param name="logger">The ILogger implementation this TokenResolver should use.</param>
        /// <param name="turnContext">The context object for the turn.</param>
        /// <param name="activity">The activity to send.</param>
        /// <param name="cancellationToken">Cancellation token.</param>
        /// <remarks>If an <see cref="OAuthCard"/> is found in an outgoing activity, the <see cref="TokenResolver"/> polls the Bot Framework Token Service in the background.
        /// When the user completes the login flow, the TokenResolver will retrieve the user's token from the service and create a <see cref="SignInConstants.TokenResponseEventName">TokenResponse</see> activity to "send" to the bot, mimicking non-streaming OAuth flows.
        /// <para />
        /// All bots using OAuth should query the service to ensure the user is successfully logged in before utilizing a user's token. The bot should never store the user's token.
        /// </remarks>
        public static void CheckForOAuthCards(BotAdapter adapter, ILogger logger, ITurnContext turnContext, Activity activity, CancellationToken cancellationToken)
        {
            if (activity?.Attachments == null)
            {
                return;
            }

            var pollingTimeout = TurnStateConstants.OAuthLoginTimeoutValue;

            // Override login timeout with value set from the OAuthPrompt or by the developer
            if (turnContext.TurnState.ContainsKey(TurnStateConstants.OAuthLoginTimeoutKey))
            {
                pollingTimeout = (TimeSpan)turnContext.TurnState.Get <object>(TurnStateConstants.OAuthLoginTimeoutKey);
            }

            var identity      = turnContext.TurnState.Get <IIdentity>(BotAdapter.BotIdentityKey) as ClaimsIdentity;
            var callback      = turnContext.TurnState.Get <BotCallbackHandler>();
            var pollingHelper = new PollingHelper()
            {
                Activity = turnContext.Activity,
                Adapter  = adapter,
                DefaultPollingInterval = PollingInterval,
                DefaultPollingTimeout  = pollingTimeout,
                CancellationToken      = cancellationToken,
                Identity = identity,
                Logger   = logger,
                Callback = callback
            };

            var pollTokenTasks = new List <Task>();

            foreach (var attachment in activity.Attachments.Where(a => a.ContentType == OAuthCard.ContentType))
            {
                if (attachment.Content is OAuthCard oauthCard)
                {
                    if (string.IsNullOrWhiteSpace(oauthCard.ConnectionName))
                    {
                        throw new InvalidOperationException("The OAuthPrompt's ConnectionName property is missing a value.");
                    }

                    pollTokenTasks.Add(PollForTokenAsync(pollingHelper, oauthCard.ConnectionName));
                }
            }

            if (pollTokenTasks.Any())
            {
                // Run the poll operations in the background.
                // On retrieving a token from the token service the TokenResolver creates an Activity to route the token to the bot to continue the conversation.
                // If these Tasks are awaited and the user doesn't complete the login flow, the bot may timeout in sending its response to the channel which can cause the streaming connection to disconnect.
#pragma warning disable VSTHRD110 // Observe result of async calls
                Task.WhenAll(pollTokenTasks.ToArray());
#pragma warning restore VSTHRD110 // Observe result of async calls
            }
        }
        public void writes_to_azure_tables()
        {
            var table = client.GetTableReference(tableName);
            var query = new TableQuery <TestCloudTableEntry>();

            table.CreateIfNotExists();

            var list = PollingHelper.WaitUntil(() => table.ExecuteQuery(query).ToArray(), l => l.Length >= 4, TimeSpan.FromSeconds(20));

            Assert.AreEqual <int>(4, list.Count());
            Assert.IsTrue(list.Any(x => x.EventId == 1));
            Assert.IsTrue(list.Any(x => x.EventId == 2));
            Assert.IsTrue(list.Any(x => x.EventId == 3));
            Assert.IsTrue(list.Any(x => x.EventId == 4));
        }
示例#4
0
        public void then_writing_more_events_should_flush_only_the_batch_size()
        {
            for (int i = 0; i < BufferingCount + 2; i++)
            {
                var entry = new EventRecord
                {
                    ProviderId       = Guid.NewGuid(),
                    ProviderName     = "TestName",
                    EventId          = 50,
                    Level            = (int)EventLevel.Verbose,
                    Opcode           = 5,
                    Task             = 6,
                    Timestamp        = DateTimeOffset.UtcNow,
                    Version          = 2,
                    InstanceName     = "Custom instance name",
                    FormattedMessage = "Test" + i,
                    Payload          = "{arg0:Test}"
                };

                this.sink.OnNext(entry);
                Thread.Sleep(10);
            }

            int count = PollingHelper.WaitUntil(() =>
            {
                using (var cmd = new SqlCommand("SELECT COUNT(*) FROM Traces", this.LocalDbConnection))
                {
                    return((int)cmd.ExecuteScalar());
                }
            },
                                                c => c > 0,
                                                TimeSpan.FromSeconds(30));

            this.sink.Dispose();

            Assert.AreEqual(BufferingCount, count);
        }
示例#5
0
        private static async Task PollForTokenAsync(PollingHelper pollingHelper, string connectionName)
        {
            try
            {
                var pollingParams = new PollingParams()
                {
                    ConnectionName  = connectionName,
                    PollingInterval = pollingHelper.DefaultPollingInterval,
                    PollingTimeout  = pollingHelper.DefaultPollingTimeout,
                };

                var stopwatch = Stopwatch.StartNew();

                while (stopwatch.Elapsed < pollingParams.PollingTimeout && !pollingParams.ShouldEndPolling)
                {
                    await pollingHelper.PollForTokenAsync(pollingParams).ConfigureAwait(false);

                    if (!pollingParams.ShouldEndPolling)
                    {
                        await Task.Delay(pollingParams.PollingInterval, pollingHelper.CancellationToken).ConfigureAwait(false);
                    }
                }

                if (!pollingParams.SentToken)
                {
                    pollingHelper.Logger.LogInformation("PollForTokenAsync completed without receiving a token", pollingHelper.Activity);
                }

                stopwatch.Stop();
            }
#pragma warning disable CA1031 // Do not catch general exception types (for now we just log the exception and continue)
            catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
            {
                pollingHelper.Logger.LogError(ex, "PollForTokenAsync threw an exception", connectionName);
            }
        }
示例#6
0
        public void then_writing_more_events_should_flush_only_the_batch_size()
        {
            for (int i = 0; i < BufferingCount + 2; i++)
            {
                var entry =
                    EventEntryTestHelper.Create(
                        providerId: Guid.NewGuid(),
                        providerName: "TestName",
                        eventId: 50,
                        level: EventLevel.Verbose,
                        opcode: (EventOpcode)5,
                        task: (EventTask)6,
                        timestamp: DateTimeOffset.UtcNow,
                        version: 2,
                        formattedMessage: "Test" + i,
                        payloadNames: new string[] { "arg0" },
                        payload: new object[] { "Test" });

                this.sink.OnNext(entry);
                Thread.Sleep(10);
            }

            int count = PollingHelper.WaitUntil(() =>
            {
                using (var cmd = new SqlCommand("SELECT COUNT(*) FROM Traces", this.localDbConnection))
                {
                    return((int)cmd.ExecuteScalar());
                }
            },
                                                c => c > 0,
                                                TimeSpan.FromSeconds(30));

            this.sink.Dispose();

            Assert.AreEqual(BufferingCount, count);
        }
示例#7
0
        async static Task Main(string[] args)
        {
            AsyncInteration <string> asyncInteration = new AsyncInteration <string>(
                async(index) =>
            {
                if (index <= 3)
                {
                    //模拟数据源
                    return(await Task.FromResult(new List <string>()
                    {
                        Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "A", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString()
                    }));
                }

                return(null);
            }
                );


            var pollingResult = await PollingHelper.Polling <string>(
                async() =>
            {
                return(await Task.FromResult(
                           new AsyncInteration <string>(
                               async(index) =>
                {
                    if (index <= 3)
                    {
                        //模拟数据源
                        return await Task.FromResult(new List <string>()
                        {
                            Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "A", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString()
                        });
                    }

                    return null;
                }
                               )
                           ));
            }, 3, 1000,
                async (data) =>
            {
                //模拟耗时操作
                await Task.Delay(100);

                Console.WriteLine(data);
            }
                ,
                async(ex) =>
            {
                Console.WriteLine(ex.ToString());
                await Task.CompletedTask;
            }
                );



            //10秒后,关闭轮询
            await Task.Delay(10000);

            await pollingResult.Stop();


            Console.ReadLine();

            /*await ParallelHelper.ForEach(asyncInteration, 3, async (data) =>
             * {
             *  //模拟耗时操作
             *  await Task.Delay(300);
             *
             * Console.WriteLine(data);
             * });*/



            //初始化
            //Init();


            //await LocalTimeout();

            //await LocalVersion();

            //await Combination();
        }
        public async Task <IDTOperationRecordPollingRollbackController> Execute(string storeGroupName, string memberName)
        {
            //获取存储组
            var storeGroup = await _storeGroupRepositoryCacheProxy.QueryByName(storeGroupName);

            if (storeGroup == null)
            {
                var fragment = new TextFragment()
                {
                    Code = TextCodes.NotFounStoreGroupMemberByName,
                    DefaultFormatting = "找不到名称为{0}的存储组",
                    ReplaceParameters = new List <object>()
                    {
                        storeGroupName
                    }
                };

                throw new UtilityException((int)Errors.NotFounStoreGroupByName, fragment);
            }
            //获取指定的组成员
            var groupMember = await storeGroup.GetMember(memberName);


            if (groupMember == null)
            {
                var fragment = new TextFragment()
                {
                    Code = TextCodes.NotFoundStoreGroupMemberInGroup,
                    DefaultFormatting = "在名称为{0}的存储组下找不到名称为{1}的成员",
                    ReplaceParameters = new List <object>()
                    {
                        storeGroupName, memberName
                    }
                };

                throw new UtilityException((int)Errors.NotFoundStoreGroupMemberInGroup, fragment);
            }

            var pollingResult = await PollingHelper.Polling <DTOperationRecord>(
                async() =>
            {
                await Task.CompletedTask;
                return
                (new AsyncInteration <DTOperationRecord>
                 (
                     async(index) =>
                {
                    if (index == 0)
                    {
                        while (true)
                        {
                            var completeList = await _dtOperationRecordRepository.QueryBySkip(groupMember.StoreInfo, (int)DTOperationRecordStatus.Complete, 0, 500);

                            await ParallelHelper.ForEach(completeList, 5,

                                                         async(record) =>
                            {
                                await record.Delete();
                            }
                                                         );


                            if (completeList.Count < 500)
                            {
                                break;
                            }
                        }
                    }

                    var datas = await _dtOperationRecordRepository.QueryBySkip(groupMember.StoreInfo, (int)DTOperationRecordStatus.UnComplete, index * 500, 500);
                    return datas;
                }
                 ));
            }, 5, 500,
                async(record) =>
            {
                try
                {
                    using (var diContainer = DIContainerContainer.CreateContainer())
                    {
                        var orginialDI = ContextContainer.GetValue <IDIContainer>("DI");
                        try
                        {
                            ContextContainer.SetValue <IDIContainer>("DI", diContainer);

                            if (await record.NeedCancel())
                            {
                                await record.Cancel();
                            }
                        }
                        finally
                        {
                            ContextContainer.SetValue <IDIContainer>("DI", orginialDI);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Exception rootEx = ex;
                    while (ex.InnerException != null)
                    {
                        ex = ex.InnerException;
                    }
                    if (ex != rootEx)
                    {
                        await record.UpdateErrorMessage($"root message:{rootEx.Message},inner message:{ex.Message},root Stack:{rootEx.StackTrace},inner Stack:{ex.StackTrace}");
                    }
                    else
                    {
                        await record.UpdateErrorMessage($"message:{rootEx.Message},Stack:{rootEx.StackTrace}");
                    }
                }
            },
                null,
                async(ex) =>
            {
                LoggerHelper.LogError(ErrorLoggerCategoryName, $"DTOperationRecordPollingRollbackService Execute Error,ErrorMessage:{ex.Message},StackTrace:{ex.StackTrace}");
            }
                );

            DTOperationRecordPollingRollbackControllerDefault controller = new DTOperationRecordPollingRollbackControllerDefault(pollingResult);

            return(controller);
        }
示例#9
0
        /// <summary>
        /// 执行所有关联的队列
        /// </summary>
        /// <param name="group"></param>
        /// <returns></returns>
        public async Task <ISQueueProcessGroupExecuteResult> Execute(SQueueProcessGroup group)
        {
            bool errorLogRecord = false;
            //声明一个轮询配置列表,队列的执行通过轮询处理帮助器管理,保证只有一个主控线程被占用
            List <PollingConfiguration> pollingConfigurations = new List <PollingConfiguration>();

            await GetAllQueue(group, async (queue) =>
            {
                pollingConfigurations.Add(new PollingConfiguration()
                {
                    Action = async() =>
                    {
                        try
                        {
                            if (AdministratorClaimGeneratorName != null && AdministratorClaimContextGeneratorName != null)
                            {
                                //生成上下文
                                var administratorClaimGenerator = await _environmentClaimGeneratorRepository.QueryByName(AdministratorClaimGeneratorName);
                                if (administratorClaimGenerator == null)
                                {
                                    var fragment = new TextFragment()
                                    {
                                        Code = TextCodes.NotFoundEnvironmentClaimGeneratorByName,
                                        DefaultFormatting = "没有找到名称为{0}的环境声明生成器",
                                        ReplaceParameters = new List <object>()
                                        {
                                            AdministratorClaimGeneratorName
                                        }
                                    };

                                    throw new UtilityException((int)Errors.NotFoundEnvironmentClaimGeneratorByName, fragment);
                                }

                                var claims = await administratorClaimGenerator.Generate();

                                var administratorClaimContextGenerator = await _claimContextGeneratorRepository.QueryByName(AdministratorClaimContextGeneratorName);
                                if (administratorClaimContextGenerator == null)
                                {
                                    var fragment = new TextFragment()
                                    {
                                        Code = TextCodes.NotFoundClaimContextGeneratorByName,
                                        DefaultFormatting = "没有找到名称为{0}的上下文生成器",
                                        ReplaceParameters = new List <object>()
                                        {
                                            AdministratorClaimContextGeneratorName
                                        }
                                    };

                                    throw new UtilityException((int)Errors.NotFoundClaimContextGeneratorByName, fragment);
                                }

                                administratorClaimContextGenerator.ContextInit(claims.Claims);
                            }

                            ConcurrentDictionary <Guid, Guid> errorMessageList = new ConcurrentDictionary <Guid, Guid>();

                            //获取队列中的消息
                            await _smessageRepository.QueryAllByQueue(queue, 500, async(messages) =>
                            {
                                bool needRestart = false;


                                foreach (var message in messages)
                                {
                                    StatusResult executeResult = new StatusResult()
                                    {
                                        Status = 2
                                    };

                                    try
                                    {
                                        using (var diContainer = DIContainerContainer.CreateContainer())
                                        {
                                            var orginialDI = ContextContainer.GetValue <IDIContainer>("DI");
                                            try
                                            {
                                                ContextContainer.SetValue <IDIContainer>("DI", diContainer);
                                                //对每个消息执行处理
                                                executeResult = await message.Execute();
                                            }
                                            finally
                                            {
                                                ContextContainer.SetValue <IDIContainer>("DI", orginialDI);
                                            }
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        while (ex.InnerException != null)
                                        {
                                            ex = ex.InnerException;
                                        }
                                        //if (errorMessageList.Count<=100000)
                                        //{
                                        //if (!errorMessageList.ContainsKey(message.ID))
                                        //{
                                        //   errorMessageList.TryAdd(message.ID, message.ID);
                                        LoggerHelper.LogError(ErrorLoggerCategoryName,
                                                              $"SQueueProcessGroup {group.Name} Execute Error,message Type {message.Type},message id {message.ID.ToString()},ErrorMessage:{await ex.GetCurrentLcidMessage()},StackTrace:{ex.StackTrace}");
                                        //}
                                        //}
                                    }

                                    if (executeResult.Status == 0)
                                    {
                                        //执行成功
                                        needRestart = true;
                                        await message.Delete();

                                        //errorMessageList.TryRemove(message.ID, out Guid deleteId);
                                    }
                                    else
                                    {
                                        if (executeResult.Status == 3)
                                        {
                                            needRestart = true;
                                            //执行失败
                                            //LoggerHelper.LogError(ErrorLoggerCategoryName, $"SQueueProcessGroup Message Execute Error,Type:{message.Type},Key:{message.Key},Data:{message.Data},ErrorMessage:{executeResult.Description}");
                                        }
                                    }
                                }

                                if (needRestart)
                                {
                                    return(await Task.FromResult(false));
                                }
                                else
                                {
                                    return(await Task.FromResult(true));
                                }
                            });

                            //System.Threading.Thread.Sleep(1000);
                        }
                        catch (Exception ex)
                        {
                            //if (!errorLogRecord)
                            //{
                            while (ex.InnerException != null)
                            {
                                ex = ex.InnerException;
                            }
                            LoggerHelper.LogError(ErrorLoggerCategoryName,
                                                  $"SQueueProcessGroup {group.Name} Execute Error,ErrorMessage:{await ex.GetCurrentLcidMessage()},StackTrace:{ex.StackTrace}");
                            //    errorLogRecord = true;
                            //}

                            await Task.Delay(1000 * 60 * 2);
                        }
                    },
                    Interval = queue.Interval
                }
                                          );

                await Task.FromResult(0);
            });

            var pollingResult = PollingHelper.Polling(pollingConfigurations,
                                                      async(ex) =>
            {
                LoggerHelper.LogError(ErrorLoggerCategoryName,
                                      $"PollingHelper Execute Error,ErrorMessage:{ex.Message},StackTrace:{ex.StackTrace}");
            }
                                                      );

            return(new SQueueProcessGroupExecuteResultDefalut(pollingResult));
        }