public async static void WriteInConsumerLogFile(string tenant, string product, string component, string topic, string consumerName, string rowLog)
 {
     try
     {
         await File.AppendAllTextAsync(ConsumerLocations.GetConsumerStateWeekLogFile(tenant, product, component, topic, consumerName), $"{rowLog}\n");
     }
     catch (Exception)
     {
         // TODO: handle this exception
     }
 }
        public static bool WriteConsumerConfigFile(string tenant, string product, string component, string topic, Consumer consumer)
        {
            try
            {
                if (File.Exists(ConsumerLocations.GetConsumerConfigFile(tenant, product, component, topic, consumer.Name)))
                {
                    File.Delete(ConsumerLocations.GetConsumerConfigFile(tenant, product, component, topic, consumer.Name));
                }

                File.WriteAllText(ConsumerLocations.GetConsumerConfigFile(tenant, product, component, topic, consumer.Name), consumer.ToJsonAndEncrypt());
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
        public bool TryCreateConsumerDirectory(string tenant, string product, string component, string topic, Consumer consumer)
        {
            try
            {
                if (Directory.Exists(ConsumerLocations.GetConsumerDirectory(tenant, product, component, topic, consumer.Name)) != true)
                {
                    Directory.CreateDirectory(ConsumerLocations.GetConsumerDirectory(tenant, product, component, topic, consumer.Name));
                    Directory.CreateDirectory(ConsumerLocations.GetConsumerLogsDirectory(tenant, product, component, topic, consumer.Name));

                    consumerLogsQueue.Enqueue(new ConsumerLog()
                    {
                        Tenant       = tenant,
                        Product      = product,
                        Component    = component,
                        Topic        = topic,
                        ConsumerName = consumer.Name,
                        Log          = $"{DateTime.Now:HH:mm:ss}|CONSUMER#|{consumer.Name}|{consumer.SubscriptionType}|{consumer.Id}|CREATED"
                    });
                    _logger.LogInformation($"Consumer '{consumer.Name}' with subscription type '{consumer.SubscriptionType}' at {tenant}/{product}/{component}/{topic} is created");
                }
Example #4
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}'");
            }
        }
 public static Consumer ReadConsumerConfigFile(string tenant, string product, string component, string topic, string consumer)
 {
     return(File
            .ReadAllText(ConsumerLocations.GetConsumerConfigFile(tenant, product, component, topic, consumer))
            .JsonToObjectAndDecrypt <Consumer>());
 }