Ejemplo n.º 1
0
        /// <summary>
        /// Send message puts the message in the database to be processed
        /// by the CDC.
        /// </summary>
        /// <param name="message">Message to publish</param>
        void IMessageSender.Send(IMessage message)
        {
            // Relies on database column default value to set creation_time
            var messageEntity = new Message(message);

            using (EventuateTramDbContext dbContext = _dbContextProvider.CreateDbContext())
            {
                dbContext.Messages.Add(messageEntity);
                dbContext.SaveChanges();
            }
        }
        public bool IsDuplicate(string consumerId, string messageId)
        {
            string logContext = $"{nameof(IsDuplicate)} " +
                                $"for {nameof(consumerId)}='{consumerId}', {nameof(messageId)}='{messageId}'";

            try
            {
                _logger.LogDebug($"+{logContext}");
                using (EventuateTramDbContext dbContext = _dbContextProvider.CreateDbContext())
                {
                    // Relies on database column default value to set creation_time
                    dbContext.ReceivedMessages.Add(new ReceivedMessage
                    {
                        ConsumerId = consumerId, MessageId = messageId
                    });
                    dbContext.SaveChanges();
                }

                _logger.LogDebug($"-{logContext}");

                return(false);
            }
            catch (DbUpdateException e)
            {
                const int duplicateKeyError = 2627;

                if (e.InnerException is SqlException sqlException && sqlException.Number == duplicateKeyError)
                {
                    _logger.LogInformation($"{logContext}: Detected duplicate.");
                    return(true);
                }

                _logger.LogError(e, $"{logContext}: Got exception {e}");
                throw;
            }
        }