Ejemplo n.º 1
0
        /// <summary>
        ///     Creates a new unique flow batch record against the flow.
        /// </summary>
        public FlowBatch GetNewFlowBatch(string flowCode)
        {
            var flow = Get(flowCode);

            lock (_syncLock)
            {
                if (flow == null)
                {
                    flow = new FlowId(flowCode); // just create new flow code
                }
                var newBatchId = flow.CurrentBatchId++;

                var batch = new FlowBatch()
                {
                    Flow    = flow,
                    BatchId = newBatchId,
                };

                // save/update original file - don't call save as it will through error
                _repo.Save(flow);

                // now return to caller
                return(batch);
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 ///     Creates a new instance
 /// </summary>
 public FlowSnapShot(
     FlowBatch flowBatch,
     FlowEntity sourceType, string sourceAddressId,
     FlowEntity targetType, string targetAddressId)
     : base(flowBatch, sourceType, sourceAddressId, targetType, targetAddressId)
 {
     this.Valid   = new List <TEntityOut>();
     this.Invalid = new List <TEntityOut>();
 }
Ejemplo n.º 3
0
        /// <summary>
        ///     Creates a new instance
        /// </summary>
        public FlowSnapshot(
            FlowBatch flowBatch,
            FlowEntity sourceType, string sourceAddressId,
            FlowEntity targetType, string targetAddressId = null) : this()
        {
            Guard.ArgumentNotNull(flowBatch, nameof(flowBatch));
            Guard.ArgumentNotNull(sourceType, nameof(sourceType));
            Guard.ArgumentNotNull(targetType, nameof(targetType));

            Batch           = flowBatch;
            SourceType      = sourceType;
            SourceAddressId = sourceAddressId;
            TargetAddressId = targetAddressId;
            TargetType      = targetType;
        }
Ejemplo n.º 4
0
        /// <summary>
        ///     Processes all  data from the incoming source and records. Will record to file and memory the entities that were and
        ///     were not processed and returns
        ///     a summary of the processes' execution status.
        /// </summary>
        /// <returns></returns>
        public FlowSnapshot Process <TEntityOut>(FlowBatch process, FlowSnapShot <TEntityOut> result = null)
        {
            Guard.ArgumentNotNull(process, nameof(process));

            // record processor time
            StartTime = DateTime.UtcNow;

            Current = 0;

            if (EntitiesReader == null)
            {
                throw new InvalidOperationException("EntitiesReader has not been assigned.");
            }

            if (result == null)
            {
                // create new
                result = new FlowSnapShot <TEntityOut>
                {
                    SourceType = new FlowEntity(typeof(TEntityIn)),
                    TargetType = new FlowEntity(typeof(TEntityOut))
                };
            }

            // creates new result batch
            result.Batch = process;

            Initialize?.Invoke();

            var onError = OnError ?? ((_, ___) => { });

            var processed  = new List <TEntityIn>();
            var errors     = new List <ErrorEvent>();
            var exceptions = new List <Exception>();

            foreach (var dto in EntitiesReader)
            {
                Current++;

                try
                {
                    var errorsEvents = ProcessItem(dto);

                    var errorEvents = errorsEvents as ErrorEvent[] ?? errorsEvents.ToArray();
                    if (!errorEvents.Any())
                    {
                        processed.Add(dto);
                    }
                    else
                    {
                        foreach (var error in errorEvents)
                        {
                            errors.Add(error);
                        }
                    }
                }
                catch (ApplicationException apex)
                {
                    if (Logger.IsErrorEnabled)
                    {
                        Logger.Error(apex, $"Can't process row {Current} due to error: {apex.Message}");
                    }

                    onError(dto, apex);

                    exceptions.Add(apex);

                    errors.Add(new ErrorEvent {
                        Type = "Exception", Message = apex.Message
                    });
                }
                catch (Exception ex)
                {
                    if (Logger.IsErrorEnabled)
                    {
                        Logger.Error(ex, $"Can't process entity at index {Current} due to an unexpected error.");
                    }

                    onError(dto, ex);

                    exceptions.Add(ex);

                    errors.Add(new ErrorEvent {
                        Type = "Exception", Message = ex.Message
                    });
                }
            }

            // update target
            try
            {
                Commit();
            }
            catch (Exception ex)
            {
                if (Logger.IsErrorEnabled)
                {
                    Logger.Error(ex, "An unexpected error occurred commiting the processed result.");
                }

                // roll back items into an error state
                foreach (var entityDto in processed)
                {
                    errors.Add(new ErrorEvent {
                        Type = "Exception", Message = $"Failed to commit changes: {ex.Message}"
                    });
                }

                //reset items
                processed.Clear();
            }

            // added/updated
            if (Logger.IsInfoEnabled)
            {
                Logger.Info($"Finised processing.");
            }

            result.ProcessedCount = processed.Count;
            result.Errors         = errors;
#if DEBUG
            result.Exceptions = exceptions;
#endif
            result.Warnings    = Warnings;
            result.ProcessTime = DateTime.UtcNow - StartTime;

            return(result);
        }
Ejemplo n.º 5
0
 /// <summary>
 ///     Creates a new instance.
 /// </summary>
 public FlowSnapshot(
     FlowBatch flowBatch) : this()
 {
     this.Batch = flowBatch;
 }
Ejemplo n.º 6
0
 /// <summary>
 ///     Creates a new instance.
 /// </summary>
 public FlowSnapShot(
     FlowBatch flowBatch) : base(flowBatch)
 {
     this.Batch      = flowBatch;
     this.TargetType = new FlowEntity(typeof(TEntityOut));
 }