예제 #1
0
        protected void ProcessQueuedItem()
        {
            var        key          = Guid.Empty;
            IQueueItem itemToReduce = null;

            if (!ReductionRepository.HasLoadedSavedEntries)
            {
                return;
            }
            try
            {
                queueLock.EnterWriteLock();

                try
                {
                    if (Queue.Count > 0)
                    {
                        itemToReduce = Queue.Dequeue();
                    }
                }
                finally
                {
                    queueLock.ExitWriteLock();
                }

                if (itemToReduce != null &&
                    ReductionRepository.FindReduction(itemToReduce.Urls) == null)
                {
                    ItemBeingProcessed = itemToReduce;
                    key = Hasher.Hash(itemToReduce.Urls);
                    RRTracer.Trace("dequeued and processing {0} with key {1}.", itemToReduce.Urls, key);
                    string url = Store.GetUrlByKey(key, itemToReduce.ResourceType);
                    if (url != null)
                    {
                        RRTracer.Trace("found url {0} in store for key {1}", url, key);
                        ReductionRepository.AddReduction(key, url);
                    }
                    else
                    {
                        if (DictionaryOfFailures.ContainsKey(key) &&
                            DictionaryOfFailures[key].Count >= FailureThreshold)
                        {
                            RRTracer.Trace("{0} has exceeded its failure threshold and will not be processed.",
                                           itemToReduce.Urls);
                            return;
                        }

                        IReducer reducer = RRContainer
                                           .Current
                                           .GetAllInstances <IReducer>()
                                           .SingleOrDefault(x => x.SupportedResourceType == itemToReduce.ResourceType);

                        if (reducer == null)
                        {
                            RRTracer.Trace(string.Format("Failed to retrieve an RRContainer for {0}.",
                                                         itemToReduce.ResourceType));
                            return;
                        }

                        reducer.Process(key, itemToReduce.Urls, itemToReduce.Host);
                    }
                    RRTracer.Trace("dequeued and processed {0}.", itemToReduce.Urls);
                }
            }
            catch (Exception e)
            {
                string message = string.Format("There were errors reducing {0}",
                                               itemToReduce != null ? itemToReduce.Urls : "");
                var wrappedException = new ApplicationException(message, e);
                RRTracer.Trace(message);
                RRTracer.Trace(e.ToString());

                AddFailure(key, wrappedException);

                if (Registry.CaptureErrorAction != null)
                {
                    Registry.CaptureErrorAction(wrappedException);
                }
            }
            finally
            {
                ItemBeingProcessed = null;
            }
        }