Example #1
0
        private void OnChange(object sender, FileSystemEventArgs e)
        {
            var path = e.FullPath;

            RRTracer.Trace("watcher watched {0}", path);
            var guid             = uriBuilder.ParseKey(path.Replace('\\', '/'));
            var contentSignature = uriBuilder.ParseSignature(path.Replace('\\', '/'));

            if (guid != Guid.Empty)
            {
                var resourceType = RRContainer.Current.GetAllInstances <IResourceType>().SingleOrDefault(x => path.EndsWith(x.FileName, true, CultureInfo.InvariantCulture));
                if (resourceType != null)
                {
                    RRTracer.Trace("New Content {0} and watched: {1}", e.ChangeType, path);
                    if (e.ChangeType == WatcherChangeTypes.Deleted)
                    {
                        ReductionRepository.RemoveReduction(guid);
                    }
                    if ((e.ChangeType == WatcherChangeTypes.Created || e.ChangeType == WatcherChangeTypes.Changed))
                    {
                        ReductionRepository.AddReduction(guid, uriBuilder.BuildResourceUrl(guid, contentSignature, resourceType.GetType()));
                    }
                }
            }
        }
Example #2
0
        public void Save(byte[] content, string url, string originalUrls)
        {
            RRTracer.Trace("Saving {0} to db.", url);
            var fileName = uriBuilder.ParseFileName(url);
            var key      = uriBuilder.ParseKey(url);
            var id       = Hasher.Hash(fileName);
            var file     = repository[id] ?? new RequestReduceFile();

            file.Content             = content;
            file.LastUpdated         = DateTime.Now;
            file.FileName            = fileName;
            file.Key                 = key;
            file.RequestReduceFileId = id;
            file.OriginalName        = originalUrls;
            file.IsExpired           = false;
            FileStore.Save(content, url, originalUrls);
            repository.Save(file);
            if (!url.ToLower().EndsWith(".png"))
            {
                reductionRepository.AddReduction(key, url);
            }
            RRTracer.Trace("{0} saved to db.", url);
        }
Example #3
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;
            }
        }