Example #1
0
        private async Task TransmitUnacknowledgedMessages(ConsumerConnectedArgs obj)
        {
            int timeoutCounter = 0;

            while (File.Exists(ConsumerLocations.GetConsumerPointerFile(obj.Tenant, obj.Product, obj.Component, obj.Topic, obj.ConsumerName)) != true)
            {
                // try every second for 5 seconds if the db is created.
                timeoutCounter++;
                Thread.Sleep(1000);
                if (timeoutCounter == 5)
                {
                    return;
                }
            }

            string consumerKey = GenerateConsumerKey(obj.Tenant, obj.Product, obj.Component, obj.Topic, obj.ConsumerName);
            ConsumerPointerContext consumerPointerContext = _consumerIOService.GetConsumerConnector(consumerKey).ConsumerPointerContext;

            try
            {
                consumerPointerContext = _consumerIOService.GetConsumerConnector(consumerKey).ConsumerPointerContext;
            }
            catch (Exception)
            {
                _logger.LogError($"Couldn't sent unacknoledge messages to consumer '{obj.ConsumerName}' at {consumerKey.Replace("~", "/")}");
                ReleaseUnacknoledgedMessageTasks(consumerKey);
                return;
            }

            List <MessageFile> partitionFiles = GetPartitionFiles(obj.Tenant, obj.Product, obj.Component, obj.Topic);
            bool isNewConsumer = false;

            try
            {
                // check if connection is open
                CheckPointerDbConnection(consumerPointerContext, consumerKey);

                var unackedMessages = consumerPointerContext.ConsumerMessages.Where(x => x.IsAcknowledged == false).OrderBy(x => x.SentDate).ToList();
                if (unackedMessages.Count == 0)
                {
                    int totalCount = consumerPointerContext.ConsumerMessages.Count();
                    if (totalCount == 0)
                    {
                        // Checking if this is a new consumer.
                        if (obj.InitialPosition == InitialPosition.Latest)
                        {
                            return;
                        }

                        unackedMessages = consumerPointerContext.ConsumerMessages.OrderBy(x => x.SentDate).ToList();
                        isNewConsumer   = true;
                    }
                }

                await AnalysePartitionFiles(obj, partitionFiles, isNewConsumer, unackedMessages);
            }
            catch (Exception ex)
            {
                _logger.LogError($"Couldn't sent unacknoledge messages to consumer '{obj.ConsumerName}' at {consumerKey.Replace("~", "/")}; errorDetails = {ex.Message}");
            }

            ReleaseUnacknoledgedMessageTasks(consumerKey);
        }
        private void InitializeConsumerConnection(string tenant, string product, string component, string topic, string consumer)
        {
            string consumerKey = $"{tenant}~{product}~{component}~{topic}~{consumer}";

            try
            {
                if (connectors.ContainsKey(consumerKey))
                {
                    return;
                }

                var connector = new ConsumerConnector(_logger, tenant, product, component, topic, consumer, new ConsumerPointerContext(ConsumerLocations.GetConsumerPointerFile(tenant,
                                                                                                                                                                                product, component, topic, consumer)), _partitionConfiguration, _agentConfiguration.MaxNumber);

                connectors.TryAdd(consumerKey, connector);
            }
            catch (Exception)
            {
                _logger.LogError($"Failed to create consumer connector at '{consumerKey}'");
            }
        }