/// <summary>
        ///     Gets the next queue request from the queues.
        /// </summary>
        /// <returns>a subclass of <see cref="BaseQueueRequest" /> representing the next request from the queues</returns>
        protected virtual BaseQueueRequest GetNextQueueRequest()
        {
            //Order for queue orchestration is as follows:
            //
            // 1) ShardCreationRequests:make the databases that are necessary to handle the rest of the activity
            // 2) ShardMapPublishing: Determine all of the shardlet moves that will be necessary and actually update the shard map in the published store
            // 3) ShardletMoves: Move all of the shardlets to their new homes
            // 4) ShardDeletionRequest: Delete retired shards (which should have been emptied by the steps above);
            // 5) ShardSyncRequest: Apply a SQL statement to a specific shard.

            BaseQueueRequest returnItem = GetNextQueuedRequest <ShardCreationRequest>();

            if (returnItem != null)
            {
                return(returnItem);
            }

            returnItem = GetNextQueuedRequest <ShardMapPublishingRequest>();
            if (returnItem != null)
            {
                WaitForEmptyQueue <ShardCreationRequest>();
                return(returnItem);
            }

            returnItem = GetNextQueuedRequest <ShardletMoveRequest>();
            if (returnItem != null)
            {
                WaitForEmptyQueue <ShardMapPublishingRequest>();

                if (ShardletMoveDelayInMilliseconds > 0)
                {
                    Thread.Sleep(ShardletMoveDelayInMilliseconds);
                }

                return(returnItem);
            }

            returnItem = GetNextQueuedRequest <ShardDeletionRequest>();
            if (returnItem != null)
            {
                WaitForEmptyQueue <ShardletMoveRequest>();
                return(returnItem);
            }

            returnItem = GetNextQueuedRequest <ShardSyncRequest>();
            if (returnItem != null)
            {
                WaitForEmptyQueue <ShardDeletionRequest>();
                return(returnItem);
            }

            return(null);
        }
 private static string GetErrorMessage(string requestName, BaseQueueRequest request)
 {
     return(string.Format("Error {0}({1}):{2}", requestName, request.QueueId, request.Message));
 }
 private static string GetCompletionMessage(string requestName, BaseQueueRequest request)
 {
     return(string.Format("Processing complete for {0}({1})", requestName, request.QueueId));
 }
 private void SendQueueProcessingEvent(string requestName, BaseQueueRequest itemToProcess)
 {
     SendQueueProcessingEvent(string.Format("Processing {0} ({1})", requestName, itemToProcess.QueueId));
 }