简体   繁体   English

如何在Azure角色中同时开始处理任务

[英]How to start processing task simultaneously in azure worker role

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. 例如Task1,task2有100条记录,并且我已将主题存储在队列中,我想同时开始处理并跨多个实例进行负载加载。

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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM