Ejemplo n.º 1
0
        public void Handle(MessageInfo info, IBulkCampaignCreatedOrUpdated command)
        {
            if (!info.MessageType.BatchSize.HasValue)
            {
                _eventHandler.Handle(command);
            }
            else
            {
                _logger.Info($"Event: CampaignCreatedOrUpdatedBatchingHandler create/update process is started; MessageId: {info.Id}");
                var totalBatchSize = (int)Math.Ceiling((decimal)command.Data.Count() / (decimal)info.MessageType.BatchSize);
                info.TotalBatchCount = totalBatchSize;
                while ((info.ProcessedBatchCount ?? 0) < (info.TotalBatchCount ?? 0))
                {
                    var batch = command.Data.GetBatches(info.MessageType.BatchSize ?? 0, info.ProcessedBatchCount ?? 0);
                    _eventHandler.HandleWithoutLibraryScenario(new BulkCampaignCreatedOrUpdatedBatch(batch));
                    info.ProcessedBatchCount = (info.ProcessedBatchCount ?? 0) + 1;
                    _messageInfoRepository.Update(info);
                }
                _logger.Info($"Event: CampaignCreatedOrUpdatedBatchingHandler create/update process is finished; MessageId: {info.Id}");

                _logger.Info($"Event: CampaignCreatedOrUpdatedBatchingHandler handle library scenario process is started; MessageId: {info.Id}");
                _eventHandler.HandleLibraryScenario();
                _logger.Info($"Event: CampaignCreatedOrUpdatedBatchingHandler handle library scenario process is finished; MessageId: {info.Id}");
            }
        }
Ejemplo n.º 2
0
        public void Handle(MessageInfo info, IBulkBreaksDeleted command)
        {
            if (info.MessageType.BatchSize > 0 &&
                _eventHandler is IBatchingEnumerateHandler <IBulkBreaksDeleted, IBreakDeleted> batchingEnumerateHandler)
            {
                int batchNo = 1;
                command.Data.OrderBy(x => x.DateRangeStart).EnumerateBatches(info.MessageType.BatchSize.Value, enumerator =>
                {
                    if (batchNo <= (info.ProcessedBatchCount ?? 0))
                    {
                        while (enumerator.MoveNext())
                        {
                        }
                    }
                    else
                    {
                        batchingEnumerateHandler.Handle(enumerator);

                        info.ProcessedBatchCount = batchNo;
                        _messageInfoRepository.Update(info);
                    }

                    batchNo++;
                });
            }
            else
            {
                _eventHandler.Handle(command);
            }
        }
        public void Handle(MessageInfo info, IBulkProgrammeCreated command)
        {
            if (info.MessageType.BatchSize > 0 &&
                _eventHandler is IBatchingEnumerateHandler <IBulkProgrammeCreated, IProgrammeCreated>
                batchingEnumerateHandler)
            {
                var batchNo = 1;
                command.Data.OrderBy(c => c.StartDateTime).EnumerateBatches(info.MessageType.BatchSize.Value,
                                                                            enumerator =>
                {
                    if (batchNo <= (info.ProcessedBatchCount ?? 0))
                    {
                        while (enumerator.MoveNext())
                        {
                        }
                    }
                    else
                    {
                        batchingEnumerateHandler.Handle(enumerator);

                        info.ProcessedBatchCount = batchNo;
                        _messageInfoRepository.Update(info);
                    }

                    batchNo++;
                });
            }
            else
            {
                _eventHandler.Handle(command);
            }
        }
        public void Handle(MessageInfo info, IBulkProgrammeDeleted command)
        {
            if (info.MessageType.BatchSize > 0 &&
                _eventHandler is IBatchingEnumerateHandler <IBulkProgrammeDeleted, IProgrammesDeleted>
                batchingEnumerateHandler)
            {
                var batchNo = 1;
                command.Data.GroupBy(p => p.SalesArea).EnumerateBatches(info.MessageType.BatchSize.Value,
                                                                        enumerator =>
                {
                    if (batchNo <= (info.ProcessedBatchCount ?? 0))
                    {
                        while (enumerator.MoveNext())
                        {
                        }
                    }
                    else
                    {
                        using (var batchEnumerator = GetBatchEnumerable(enumerator).GetEnumerator())
                        {
                            batchingEnumerateHandler.Handle(batchEnumerator);
                        }

                        info.ProcessedBatchCount = batchNo;
                        _messageInfoRepository.Update(info);
                    }

                    batchNo++;
                });
            }
            else
            {
                _eventHandler.Handle(command);
            }

            IEnumerable <IProgrammesDeleted> GetBatchEnumerable(
                IEnumerator <IGrouping <string, IProgrammesDeleted> > enumerator)
            {
                while (enumerator.MoveNext())
                {
                    foreach (var item in enumerator.Current.OrderBy(x => x.FromDate))
                    {
                        yield return(item);
                    }
                }
            }
        }
        private bool ExecuteTransaction(GroupTransactionInfo transaction)
        {
            _logger?.Info($"Transaction execution is started TransactionId: {transaction.Id} ");

            transaction.State        = MessageState.InProgress;
            transaction.ExecutedDate = DateTime.UtcNow;

            _groupTransactionRepository.Update(transaction);

            _logger?.Info($"Transaction State is updated to {transaction.State.ToString()} TransactionId: {transaction.Id} ");

            transaction.State = MessageState.Completed;

            var messages = _messageInfoRepository.GetByTransactionId(transaction.Id);

            foreach (var eventMessage in messages.OrderByDescending(x => x.Priority))
            {
                if (eventMessage.State == MessageState.Completed)
                {
                    continue;
                }

                if (eventMessage.RetryCount == GlobalEventRetryCount)
                {
                    transaction.State = MessageState.Failed;

                    break;
                }
                _logger?.Info($"Event: {eventMessage.Name} processing started; MessageId: {eventMessage.Id} ");
                eventMessage.State        = MessageState.InProgress;
                eventMessage.ExecutedDate = DateTime.UtcNow;

                _messageInfoRepository.Update(eventMessage);

                ProcessMessageWithRetry(eventMessage);

                _eventInfoRepository.Update(eventMessage);

                if (eventMessage.State == MessageState.Failed)
                {
                    transaction.State = eventMessage.RetryCount >= GlobalEventRetryCount
                        ? MessageState.Failed
                        : MessageState.ReadyForRetry;

                    break;
                }

                _logger?.Info($"Event: {eventMessage.Name} processed successfully; MessageId: {eventMessage.Id} ");
            }

            if (transaction.State == MessageState.Completed)
            {
                transaction.CompletedDate = DateTime.UtcNow;
            }

            _groupTransactionRepository.Update(transaction);

            _logger?.Info($"Transaction execution is finished TransactionId: {transaction.Id}; Transaction State: {transaction.State.ToString()}");

            //We can't allow executing of the next transaction if the current is not completed successfuly
            return(transaction.State == MessageState.Completed);
        }