示例#1
0
        private Task RunWorkersAsync()
        {
            if (Count == 0 || _workers.Count == 0)
            {
                return(Task.FromResult(0));
            }

            return(Task.Factory.StartNew(() => {
                QueueEntry <T> queueEntry = null;
                while (Count > 0)
                {
                    try {
                        queueEntry = DequeueAsync(0).Result;
                    } catch (TimeoutException) {}
                    if (queueEntry == null)
                    {
                        return;
                    }

                    // get a random worker
                    var worker = _workers.ToList().Random();
                    if (worker == null)
                    {
                        return;
                    }

                    try {
                        worker.Action(queueEntry);
                        if (worker.AutoComplete)
                        {
                            queueEntry.CompleteAsync().Wait();
                        }
                    } catch (Exception ex) {
                        Interlocked.Increment(ref _workerErrorsCounter);
                        Log.Error().Exception(ex).Message("Error sending work item to worker: {0}", ex.Message).Write();
                        queueEntry.AbandonAsync().Wait();
                    }
                }
            }));
        }