protected virtual void Dispose(bool disposing)
        {
            if (!_disposedValue)
            {
                if (disposing)
                {
                    if (IsRoot)
                    {
                        UmbracoContext.Dispose();
                        _umbracoContextAccessor.Clear();
                    }
                }

                _disposedValue = true;
            }
        }
Beispiel #2
0
        /// <summary>
        /// This will inspect the event metadata and execute it's affiliated handler if one is found
        /// </summary>
        /// <param name="events"></param>
        internal static void HandleEvents(IEnumerable <IEventDefinition> events)
        {
            //TODO: We should remove this in v8, this is a backwards compat hack and is needed because when we are using Deploy, the events will be raised on a background
            //thread which means that cache refreshers will also execute on a background thread and in many cases developers may be using UmbracoContext.Current in their
            //cache refresher handlers, so before we execute all of the events, we'll ensure a context
            UmbracoContext tempContext = null;

            if (UmbracoContext.Current == null)
            {
                var httpContext = new HttpContextWrapper(HttpContext.Current ?? new HttpContext(new SimpleWorkerRequest("temp.aspx", "", new StringWriter())));
                tempContext = UmbracoContext.EnsureContext(
                    httpContext,
                    ApplicationContext.Current,
                    new WebSecurity(httpContext, ApplicationContext.Current),
                    UmbracoConfig.For.UmbracoSettings(),
                    UrlProviderResolver.Current.Providers,
                    true);
            }

            try
            {
                foreach (var e in events)
                {
                    var handler = FindHandler(e);
                    if (handler == null)
                    {
                        continue;
                    }

                    handler.Invoke(null, new[] { e.Sender, e.Args });
                }
            }
            finally
            {
                if (tempContext != null)
                {
                    tempContext.Dispose();
                }
            }
        }
        public override bool PerformRun()
        {
            if (_appContext == null)
            {
                return(true);                     // repeat...
            }
            if (Suspendable.ScheduledPublishing.CanRun == false)
            {
                return(true); // repeat, later
            }
            switch (_appContext.GetCurrentServerRole())
            {
            case ServerRole.Slave:
                Logger.Debug <ScheduledPublishing>("Does not run on replica servers.");
                return(true);    // DO repeat, server role can change

            case ServerRole.Unknown:
                Logger.Debug <ScheduledPublishing>("Does not run on servers with unknown role.");
                return(true);    // DO repeat, server role can change
            }

            // ensure we do not run if not main domain, but do NOT lock it
            if (_appContext.MainDom.IsMainDom == false)
            {
                LogHelper.Debug <ScheduledPublishing>("Does not run if not MainDom.");
                return(false); // do NOT repeat, going down
            }

            UmbracoContext tempContext = null;

            try
            {
                // DO not run publishing if content is re-loading
                if (content.Instance.isInitializing == false)
                {
                    //TODO: We should remove this in v8, this is a backwards compat hack
                    // see notes in CacheRefresherEventHandler
                    // because notifications will not be sent if there is no UmbracoContext
                    // see NotificationServiceExtensions
                    var httpContext = new HttpContextWrapper(new HttpContext(new SimpleWorkerRequest("temp.aspx", "", new StringWriter())));
                    tempContext = UmbracoContext.EnsureContext(
                        httpContext,
                        _appContext,
                        new WebSecurity(httpContext, _appContext),
                        _settings,
                        UrlProviderResolver.Current.Providers,
                        true);

                    var publisher = new ScheduledPublisher(_appContext.Services.ContentService);
                    var count     = publisher.CheckPendingAndProcess();
                    Logger.Debug <ScheduledPublishing>(() => string.Format("Processed {0} items", count));
                }
            }
            catch (Exception e)
            {
                Logger.Error <ScheduledPublishing>("Failed (see exception).", e);
            }
            finally
            {
                if (tempContext != null)
                {
                    // because we created an http context and assigned it to UmbracoContext,
                    // the batched messenger does batch instructions, but since there is no
                    // request, we need to explicitely tell it to flush the batch of instrs.
                    var batchedMessenger = ServerMessengerResolver.Current.Messenger as BatchedDatabaseServerMessenger;
                    if (batchedMessenger != null)
                    {
                        batchedMessenger.FlushBatch();
                    }

                    tempContext.Dispose(); // nulls the ThreadStatic context
                }
            }

            return(true); // repeat
        }