Example #1
0
        public async Task Execute(MatrixDataHandler handler, int skip, int size, int paralleNumber, Func <MatrixDataRow, int, Task> rowCompleteAction, Func <MatrixDataRow, int, Exception, Task> rowExceptionAction)
        {
            if (!_matrixDataHandlerServiceFactories.TryGetValue(handler.Type, out IFactory <IMatrixDataHandlerService> serviceFactory))
            {
                var fragment = new TextFragment()
                {
                    Code = TextCodes.NotFoundMatrixDataHandlerServiceByType,
                    DefaultFormatting = "找不到类型为{0}的矩阵数据处理方服务,发生位置:{1}",
                    ReplaceParameters = new List <object>()
                    {
                        handler.Type, $"{this.GetType().FullName}.MatrixDataHandlerServiceFactories"
                    }
                };
                throw new UtilityException((int)Errors.NotFoundMatrixDataHandlerServiceByType, fragment);
            }

            var service = serviceFactory.Create();
            MatrixDataHandlerContext context = new MatrixDataHandlerContext();
            await service.PreExecute(context);

            await ParallelHelper.RunCircle(paralleNumber, async (int index) =>
            {
                int start   = index *size + skip;
                bool result = true;
                await handler.Provider.ExecuteAll(start, size, async(rowList) =>
                {
                    if (rowList.Count < size)
                    {
                        result = false;
                    }

                    if (rowList.Count > 0)
                    {
                        for (var i = 0; i <= rowList.Count - 1; i++)
                        {
                            start++;
                            bool executeResult = true;
                            try
                            {
                                await service.Execute(context, handler.Configuration, rowList[i]);
                            }
                            catch (Exception ex)
                            {
                                executeResult = false;
                                await rowExceptionAction(rowList[i], start, ex);
                            }

                            if (executeResult)
                            {
                                await rowCompleteAction(rowList[i], start);
                            }
                        }
                    }

                    return(false);
                });

                return(await Task.FromResult(result));
            });

            await service.PostExecute(context);
        }