Ejemplo n.º 1
0
        public bool Process(StepContext stepContext, IStepRepository stepRepository)
        {
            bool        success = true;
            StepContext ctx     = StepContext.InitialRun(stepContext, _chunkSize);

            while (ctx.HasNext)
            {
                long newStepId = stepRepository.InsertStep(ctx.StepName, ctx.NextStepIndex);

                bool          exceptionThrown = false;
                bool          skip            = false;
                bool          error           = false;
                List <TInput> items           = Enumerable.Empty <TInput>().ToList();
                TOutput[]     processed       = Enumerable.Empty <TOutput>().ToArray();

                try
                {
                    items = _reader.Read(ctx.StepIndex, _chunkSize).ToList();

                    processed = items.Select(item => _processor.Process(item)).ToArray();

                    if (processed.Any())
                    {
                        success &= _writer.Write(processed);
                    }
                }
                catch (Exception ex)
                {
                    error = true;
                    skip  = _skipPolicy.IsSatisfiedBy(stepRepository, new SkipContext(ctx.StepName, ctx.NextStepIndex, ex));
                    if (!skip)
                    {
                        exceptionThrown = true;
                        throw;
                    }
                }
                finally
                {
                    if (!exceptionThrown)
                    {
                        ctx = StepContext.Increment(ctx, items.Count, processed.Length, skip);
                    }

                    stepRepository.UpdateStep(newStepId, processed.Length, error, skip);
                }
            }
            return(success);
        }