public override void Run() { Trace.WriteLine("Backend Processing worker role started.", "Information"); int queuePollingIntervalFloor = Convert.ToInt32(RoleEnvironment.GetConfigurationSettingValue("QueuePollingIntervalFloor")); int queuePollingIntervalCeiling = Convert.ToInt32(RoleEnvironment.GetConfigurationSettingValue("QueuePollingIntervalCeiling")); int currentInterval = queuePollingIntervalFloor; // connect to repositories contactQueue = new ContactQueue(); contactRepository = new ContactRepository(); SQLContacts = new SQLContactRepository(); while (true) { // grab message ContactQueueMessage msg = contactQueue.GetMessage(); if (msg != null) { Contact contact = contactRepository.GetFromFacebookId(msg.FacebookId); // email person SendEmail(contact); // map to sqlcontact object and store in sql StoreContact(contact); // remove message from queue now that we are done with it contactQueue.DeleteMessage(msg); // adjust queue polling interval to floor to speed up since we found something in the queue currentInterval = queuePollingIntervalFloor; } else { if (currentInterval < queuePollingIntervalCeiling) currentInterval = currentInterval * 2; // the doubling of the interval may have pushed it above our ceiling if (currentInterval > queuePollingIntervalCeiling) currentInterval = queuePollingIntervalCeiling; } Thread.Sleep(TimeSpan.FromSeconds(currentInterval)); Trace.WriteLine("Working. Polling Interval in seconds is " + currentInterval.ToString(), "Information"); } }