سؤال

I have developed worker role application to process different tasks. For example Task1, task2 has 100 records and i have stored theme in queue and i would like to start processing both simultaneously and span load over multiple instances.

In future there will be more task and records inside task to process are going to increase. so how can i improve below method to process records efficiently?

Currently I have done code sequentially as below

  private void ProcessTaskQueues()
        {

            var currentInterval1 = 0;
            var maxInterval1 = 15;
            var currentInterval2 = 0;
            var maxInterval2 = 15;
            string queueName1 = RoleEnvironment.GetConfigurationSettingValue("Task1Queue");
            CloudQueue queue1 = storageAccount.CreateCloudQueueClient().GetQueueReference(queueName1);
            queue1.CreateIfNotExists();
            string queueName2 = RoleEnvironment.GetConfigurationSettingValue("Task2Queue");
            CloudQueue queue2 = storageAccount.CreateCloudQueueClient().GetQueueReference(queueName2);
            queue2.CreateIfNotExists();

            while (true)
            {
                try
                {
                    TaskPerformer tp = new TaskPerformer();
                    // Task 1 
                    Trace.WriteLine(string.Format("[{0}] - [TASK1] Fetch Message queue", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
                    var cloudQueueMessage1 = queue1.GetMessage();
                    if (cloudQueueMessage1 != null)
                    {
                        currentInterval1 = 0;
                        if (cloudQueueMessage1.DequeueCount <= 1)
                        {
                            var item = cloudQueueMessage1.FromMessage<Task1Item>();
                            tp.ExecuteTask1(item);
                            Trace.WriteLine(string.Format("[{0}] - [TASK1] Message Executed for ID : {1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), item.MLPID));
                            queue2.DeleteMessage(cloudQueueMessage1);
                        }
                    }
                    else
                    {
                        if (currentInterval1 < maxInterval1)
                        {
                            currentInterval1++;
                            Trace.WriteLine(string.Format("[{0}] - Waiting for {1} seconds", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), currentInterval1));
                        }
                        Thread.Sleep(TimeSpan.FromSeconds(currentInterval1));
                    }

                    // Task 2 
                    Trace.WriteLine(string.Format("[{0}] - [TASK2] Fetch Message queue", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
                    var cloudQueueMessage2 = queue2.GetMessage();
                    if (cloudQueueMessage2 != null)
                    {
                        currentInterval2 = 0;
                        if (cloudQueueMessage2.DequeueCount <= 1)
                        {

                            var dns = cloudQueueMessage2.FromMessage<DNS>();
                            tp.ExecuteTask2(dns);
                            Trace.WriteLine(string.Format("[{0}] - [TASK2] Message Executed for ID : {1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), dns.ID));
                            queue2.DeleteMessage(cloudQueueMessage2);
                        }
                    }
                    else
                    {
                        if (currentInterval2 < maxInterval2)
                        {
                            currentInterval2++;
                            Trace.WriteLine(string.Format("[{0}] - Waiting for {1} seconds", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), currentInterval2));
                        }
                        Thread.Sleep(TimeSpan.FromSeconds(currentInterval2));
                    }
                }
                catch (Exception)
                { }
            }
        }
هل كانت مفيدة؟

المحلول

From comment of this question it leads me to search for alternate solution with only 1 queue for multiple type of data items and i got solution of that here

How to use one object to store multiple type of data

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top