private async Task OnMessage(BrokeredMessage message)
        {
            if (_workerAction == null)
            {
                return;
            }

            Interlocked.Increment(ref _dequeuedCount);
            var data = message.GetBody <T>();

            var workItem = new QueueEntry <T>(message.LockToken.ToString(), data, this);

            try {
                await _workerAction(workItem);

                if (_workerAutoComplete)
                {
                    workItem.Complete();
                }
            } catch (Exception ex) {
                Interlocked.Increment(ref _workerErrorCount);
                Log.Error().Exception(ex).Message("Error sending work item to worker: {0}", ex.Message).Write();
                workItem.Abandon();
            }
        }
Exemple #2
0
        private Task WorkerLoop(CancellationToken token)
        {
            Log.Trace().Message("WorkerLoop Start {0}", _queueName).Write();
            while (!token.IsCancellationRequested && _workerAction != null)
            {
                Log.Trace().Message("WorkerLoop Pass {0}", _queueName).Write();
                QueueEntry <T> queueEntry = null;
                try {
                    queueEntry = Dequeue();
                } catch (TimeoutException) { }

                if (token.IsCancellationRequested || queueEntry == null)
                {
                    continue;
                }

                try {
                    _workerAction(queueEntry);
                    if (_workerAutoComplete)
                    {
                        queueEntry.Complete();
                    }
                } catch (Exception ex) {
                    Log.Error().Exception(ex).Message("Worker error: {0}", ex.Message).Write();
                    queueEntry.Abandon();
                    Interlocked.Increment(ref _workerErrorCount);
                }
            }

            Log.Trace().Message("Worker exiting: {0} Cancel Requested: {1}", _queueName, token.IsCancellationRequested).Write();

            return(TaskHelper.Completed());
        }
        private Task WorkerLoop(CancellationToken token)
        {
            Log.Trace().Message("WorkerLoop Start {0}", typeof(T).Name).Write();
            while (!token.IsCancellationRequested)
            {
                if (_queue.Count == 0 || _workerAction == null)
                {
                    _autoEvent.WaitOne(TimeSpan.FromMilliseconds(250));
                }

                Log.Trace().Message("WorkerLoop Signaled {0}", typeof(T).Name).Write();
                QueueEntry <T> queueEntry = null;
                try {
                    queueEntry = Dequeue(TimeSpan.Zero);
                } catch (TimeoutException) { }

                if (queueEntry == null || _workerAction == null)
                {
                    return(TaskHelper.Completed());
                }

                try {
                    _workerAction(queueEntry);
                    if (_workerAutoComplete)
                    {
                        queueEntry.Complete();
                    }
                } catch (Exception ex) {
                    Log.Error().Exception(ex).Message("Worker error: {0}", ex.Message).Write();
                    queueEntry.Abandon();
                    Interlocked.Increment(ref _workerErrorCount);
                }
            }

            return(TaskHelper.Completed());
        }