Example #1
0
 public async Task <EnvironmentClaimGenerator> QueryByName(string name)
 {
     return(await _kvcacheVisitor.Get(
                async(k) =>
     {
         return await _environmentClaimGeneratorRepository.QueryByName(name);
     },
                name
                ));
 }
Example #2
0
        public async Task Do(string groupName)
        {
            var group = await _sQueueProcessGroupRepository.QueryByName(groupName);

            if (group == null)
            {
                var fragment = new TextFragment()
                {
                    Code = TextCodes.NotFoundSQueueProcessGroupByName,
                    DefaultFormatting = "没有找到名称为{0}的队列执行组",
                    ReplaceParameters = new List <object>()
                    {
                        groupName
                    }
                };

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

            //对组里的每个队列并行处理
            List <SQueue> queueList = new List <SQueue>();

            var environmentClaimGenerator = await _environmentClaimGeneratorRepository.QueryByName(EnvironmentClaimGeneratorName);

            var claimContextGenerator = await _claimContextGeneratorRepository.QueryByName(ClaimContextGeneratorName);

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

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

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

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

            await group.GetAllQueue(async(queue) =>
            {
                queueList.Add(queue);
                await Task.FromResult(0);
            });


            Parallel.ForEach(queueList, (queue) =>
            {
                var fun = Task.Run(async() =>
                {
                    while (true)
                    {
                        try
                        {
                            //从当前环境中获取声明
                            var claims = await environmentClaimGenerator.Generate().ConfigureAwait(false);
                            ///初始化上下文
                            claimContextGenerator.ContextInit(claims.Claims);
                            //获取队列中的所有消息
                            await _sMessageRepository.QueryAllByQueue(queue, 500, async(messages) =>
                            {
                                foreach (var message in messages)
                                {
                                    var messageResult = await message.Execute();

                                    //需要退出查询
                                    if (messageResult.Status == 0 || messageResult.Status == 3)
                                    {
                                        if (messageResult.Status == 0)
                                        {
                                            //需要删除
                                            await message.Delete();
                                        }
                                        return(false);
                                    }

                                    //如果结果出错,需要记录日志
                                    if (messageResult.Status == 2)
                                    {
                                        var logger = _loggerFactory.CreateLogger(ErrorCatalogName);
                                        logger.LogError($"AppExecuteQueueProcessGroup Message Execute Error,Type:{message.Type},Key:{message.Key},Data:{message.Data},error:{messageResult.Description}");
                                    }
                                }

                                return(true);
                            });
                        }
                        catch (Exception ex)
                        {
                            //发生错误,需要记录日志
                            var logger = _loggerFactory.CreateLogger(ErrorCatalogName);
                            logger.LogError($"AppExecuteQueueProcessGroup Error,error:{ex.Message},stack:{ex.StackTrace}");
                        }

                        System.Threading.Thread.Sleep(5);
                    }
                });

                fun.Wait();
            });
        }
Example #3
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));
        }