示例#1
0
        public void ThenTheMessageInfoWithGroupTransactionWillBeUpdatedAsFollowing(string groupTransactionKey, Table table)
        {
            var dbmessageInfos        = MessageInfoRepository.GetByTransactionId(_groupTransactionIds[groupTransactionKey]);
            var messageInfos          = table.CreateSet <MessageInfo>();
            var dbmessageInfosGrouped = dbmessageInfos.GroupBy(m => new { m.Name, m.State, m.Type });
            var messageInfosGrouped   = messageInfos.GroupBy(m => new { m.Name, m.State, m.Type });
            var joinedMessages        = dbmessageInfosGrouped.Join(messageInfosGrouped,
                                                                   db => db.Key,
                                                                   local => local.Key,
                                                                   (db, local) => db.Key);

            Assert.True(dbmessageInfos.Count() == messageInfos.Count() && dbmessageInfosGrouped.Count() == messageInfosGrouped.Count() && messageInfosGrouped.Count() == joinedMessages.Count());
        }
        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);
        }