public IFetchedJob Dequeue(string[] queues, CancellationToken cancellationToken)
        {
            BrokeredMessage message    = null;
            var             queueIndex = 0;

            var clients = queues
                          .Select(queue => _manager.GetClient(queue))
                          .ToArray();

            do
            {
                cancellationToken.ThrowIfCancellationRequested();

                try
                {
                    var client = clients[queueIndex];

                    message = queueIndex == queues.Length - 1
                        ? client.Receive(SyncReceiveTimeout)
                        : client.Receive(MinSyncReceiveTimeout);
                }
                catch (TimeoutException)
                {
                }

                queueIndex = (queueIndex + 1) % queues.Length;
            } while (message == null);

            return(new ServiceBusQueueFetchedJob(message));
        }
        public IEnumerable <int> GetEnqueuedJobIds(string queue, int @from, int perPage)
        {
            var client = _manager.GetClient(queue);
            var jobIds = new List <int>();

            // We have to overfetch to retrieve enough messages for paging.
            // e.g. @from = 10 and page size = 20 we need 30 messages from the start
            var messages = client.PeekBatch(0, @from + perPage).ToArray();

            // We could use LINQ here but to avoid creating lots of garbage lists
            // through .Skip / .ToList etc. use a simple loop.
            for (var i = 0; i < messages.Length; i++)
            {
                var msg = messages[i];

                // Only include the job id once we have skipped past the @from
                // number
                if (i >= @from)
                {
                    jobIds.Add(int.Parse(msg.GetBody <string>()));
                }

                msg.Dispose();
            }

            return(jobIds);
        }
Exemplo n.º 3
0
        public IFetchedJob Dequeue(string[] queues, CancellationToken cancellationToken)
        {
            BrokeredMessage message    = null;
            var             queueIndex = 0;

            var clients = queues
                          .Select(queue => _manager.GetClient(queue))
                          .ToArray();

            do
            {
                cancellationToken.ThrowIfCancellationRequested();

                try
                {
                    var client      = clients[queueIndex];
                    var isLastQueue = queueIndex == queues.Length - 1;

                    message = isLastQueue
                        ? client.Receive(_manager.Options.LoopReceiveTimeout) // Last queue
                        : client.Receive(MinSyncReceiveTimeout);
                }
                catch (TimeoutException)
                {
                }
                catch (MessagingEntityNotFoundException ex)
                {
                    var errorMessage = string.Format(
                        "Queue {0} could not be found. Either create the queue manually, " +
                        "or grant the Manage permission and set ServiceBusQueueOptions.CheckAndCreateQueues to true",
                        clients[queueIndex].Path);

                    throw new UnauthorizedAccessException(errorMessage, ex);
                }

                queueIndex = (queueIndex + 1) % queues.Length;
            } while (message == null);

            return(new ServiceBusQueueFetchedJob(message));
        }