Esempio n. 1
0
        /// <summary>Starts a new timer.</summary>
        /// <param name="isGroup">If it should also start a Group.</param>
        /// <param name="idParts">The id parts.</param>
        public void Start(bool isGroup, params string[] idParts)
        {
            var id = WebGreaseContext.ToStringId(idParts);

            if (this.timers.Any(t => t.Id.Equals(id)))
            {
                throw new BuildWorkflowException("An error occurred while starting timer for {0}, probably a wrong start/end for key: ".InvariantFormat(id));
            }

            this.PauseLastTimer();
            this.timers.Add(new TimeMeasureItem(id, DateTime.Now));

            if (isGroup)
            {
                this.BeginGroup();
            }
        }
Esempio n. 2
0
        /// <summary>Ends an already started timer.</summary>
        /// <param name="isGroup">if it is a group.</param>
        /// <param name="idParts">The id parts.</param>
        public void End(bool isGroup, params string[] idParts)
        {
            if (isGroup)
            {
                this.EndGroup();
            }

            var id        = WebGreaseContext.ToStringId(idParts);
            var lastTimer = this.timers.Last();

            if (lastTimer.Id != id)
            {
                throw new BuildWorkflowException("Trying to end a timer that was not started.");
            }

            this.StopTimer(lastTimer);
            this.ResumeLastTimer();
        }
Esempio n. 3
0
        /// <summary>
        /// Executes the action for the cached section
        /// the boolean/success result of the action will determine if the action will be stored in cache.
        /// </summary>
        /// <param name="cachableSectionAction">The section action.</param>
        /// <returns>If all was successfull (being passed from within the actions).</returns>
        public bool Execute(Func <ICacheSection, bool> cachableSectionAction)
        {
            var id = WebGreaseContext.ToStringId(this.idParts);
            var webGreaseSectionKey = new WebGreaseSectionKey(this.context, id, this.cacheVarByContentItem, this.cacheVarBySetting, this.cacheVarByFileSet);
            var sectionLock         = SectionLocks.GetOrAdd(webGreaseSectionKey.Value, new object());

            return(Safe.Lock(
                       sectionLock,
                       this.cacheInfiniteWaitForLock ? Safe.MaxLockTimeout : Safe.DefaultLockTimeout,
                       () =>
            {
                var errorHasOccurred = false;
                EventHandler logOnErrorOccurred = delegate { errorHasOccurred = true; };
                this.context.Log.ErrorOccurred += logOnErrorOccurred;
                var cacheSection = this.context.Cache.BeginSection(webGreaseSectionKey);

                try
                {
                    if (this.context.TemporaryIgnore(this.cacheVarByFileSet, this.cacheVarByContentItem) && !errorHasOccurred)
                    {
                        cacheSection.Save();
                        return true;
                    }

                    cacheSection.Load();
                    if (this.cacheIsSkipable && cacheSection.CanBeSkipped())
                    {
                        if (this.whenSkippedAction != null)
                        {
                            this.whenSkippedAction(cacheSection);
                        }

                        if (!errorHasOccurred)
                        {
                            return true;
                        }
                    }

                    if (this.restoreFromCacheAction != null && cacheSection.CanBeRestoredFromCache())
                    {
                        if (this.restoreFromCacheAction(cacheSection) && !errorHasOccurred)
                        {
                            return true;
                        }
                    }

                    this.context.Measure.Start(this.isGroup, this.idParts);
                    try
                    {
                        if (!cachableSectionAction(cacheSection) || errorHasOccurred)
                        {
                            return false;
                        }

                        cacheSection.Save();
                        return true;
                    }
                    finally
                    {
                        this.context.Measure.End(this.isGroup, this.idParts);
                    }
                }
                finally
                {
                    this.context.Log.ErrorOccurred -= logOnErrorOccurred;
                    cacheSection.EndSection();
                }
            }));
        }