Exemplo n.º 1
0
        /// <summary>
        /// Clears messages in a queue
        /// </summary>
        private async Task ClearMessages(MqClient client, HorseMessage message)
        {
            HorseQueue queue = _server.FindQueue(message.Target);

            if (queue == null)
            {
                if (message.WaitResponse)
                {
                    await client.SendAsync(message.CreateResponse(HorseResultCode.NotFound));
                }

                return;
            }

            string prio      = message.FindHeader(HorseHeaders.PRIORITY_MESSAGES);
            string msgs      = message.FindHeader(HorseHeaders.MESSAGES);
            bool   clearPrio = !string.IsNullOrEmpty(prio) && prio.Equals("yes", StringComparison.InvariantCultureIgnoreCase);
            bool   clearMsgs = !string.IsNullOrEmpty(msgs) && msgs.Equals("yes", StringComparison.InvariantCultureIgnoreCase);

            foreach (IAdminAuthorization authorization in _server.AdminAuthorizations)
            {
                bool grant = await authorization.CanClearQueueMessages(client, queue, clearPrio, clearMsgs);

                if (!grant)
                {
                    if (message.WaitResponse)
                    {
                        await client.SendAsync(message.CreateResponse(HorseResultCode.Unauthorized));
                    }

                    return;
                }
            }

            if (clearPrio && clearMsgs)
            {
                queue.ClearAllMessages();
            }
            else if (clearPrio)
            {
                queue.ClearHighPriorityMessages();
            }
            else if (clearMsgs)
            {
                queue.ClearRegularMessages();
            }

            //if creation successful, sends response
            if (message.WaitResponse)
            {
                await client.SendAsync(message.CreateResponse(HorseResultCode.Ok));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Finds and dequeues a message from the queue.
        /// If there is not message, returns null
        /// </summary>
        private async Task <Tuple <QueueMessage, int, int> > DequeueMessage(bool fifo, bool getInfo, ClearDecision clear)
        {
            QueueMessage message          = null;
            int          prioMessageCount = 0;
            int          messageCount     = 0;

            await _queue.RunInListSync(() =>
            {
                //pull from prefential messages
                if (_queue.PriorityMessagesList.Count > 0)
                {
                    if (fifo)
                    {
                        // ReSharper disable once PossibleNullReferenceException
                        message = _queue.PriorityMessagesList.First.Value;
                        _queue.PriorityMessagesList.RemoveFirst();
                    }
                    else
                    {
                        // ReSharper disable once PossibleNullReferenceException
                        message = _queue.PriorityMessagesList.Last.Value;
                        _queue.PriorityMessagesList.RemoveLast();
                    }

                    if (message != null)
                    {
                        message.IsInQueue = false;
                    }
                }

                //if there is no prefential message, pull from standard messages
                if (message == null && _queue.MessagesList.Count > 0)
                {
                    if (fifo)
                    {
                        // ReSharper disable once PossibleNullReferenceException
                        message = _queue.MessagesList.First.Value;
                        _queue.MessagesList.RemoveFirst();
                    }
                    else
                    {
                        // ReSharper disable once PossibleNullReferenceException
                        message = _queue.MessagesList.Last.Value;
                        _queue.MessagesList.RemoveLast();
                    }

                    if (message != null)
                    {
                        message.IsInQueue = false;
                    }
                }

                if (getInfo)
                {
                    prioMessageCount = _queue.PriorityMessageCount();
                    messageCount     = _queue.MessageCount();
                }

                if (clear == ClearDecision.All)
                {
                    _queue.ClearAllMessages();
                }

                else if (clear == ClearDecision.HighPriority)
                {
                    _queue.ClearHighPriorityMessages();
                }

                else if (clear == ClearDecision.DefaultPriority)
                {
                    _queue.ClearRegularMessages();
                }
            });

            return(new Tuple <QueueMessage, int, int>(message, prioMessageCount, messageCount));
        }