//NOTE: sender will be the umbraco ApplicationContext
        public static void CheckPublishing(object sender)
        {
            if (_isPublishingRunning)
            {
                return;
            }
            _isPublishingRunning = true;
            try
            {
                //run the scheduled publishing - we need to determine if this server

                var publisher = new ScheduledPublisher(ApplicationContext.Current.Services.ContentService);
                publisher.CheckPendingAndProcess();

                // run scheduled url tasks
                try
                {
                    foreach (var t in UmbracoConfig.For.UmbracoSettings().ScheduledTasks.Tasks)
                    {
                        bool runTask = false;
                        if (!ScheduledTaskTimes.ContainsKey(t.Alias))
                        {
                            runTask = true;
                            ScheduledTaskTimes.Add(t.Alias, DateTime.Now);
                        }
                        // Add 1 second to timespan to compensate for differencies in timer
                        else if (
                            new TimeSpan(
                                DateTime.Now.Ticks - ((DateTime)ScheduledTaskTimes[t.Alias]).Ticks).TotalSeconds + 1 >= t.Interval)
                        {
                            runTask = true;
                            ScheduledTaskTimes[t.Alias] = DateTime.Now;
                        }

                        if (runTask)
                        {
                            bool taskResult = GetTaskByHttp(t.Url);
                            if (t.Log)
                            {
                                LogHelper.Info <publishingService>(string.Format("{0} has been called with response: {1}", t.Alias, taskResult));
                            }
                        }
                    }
                }
                catch (Exception ee)
                {
                    LogHelper.Error <publishingService>("Error executing scheduled task", ee);
                }
            }
            catch (Exception x)
            {
                LogHelper.Error <publishingService>("Error executing scheduled publishing", x);
            }
            finally
            {
                _isPublishingRunning = false;
            }
        }
예제 #2
0
        public JsonResult Index()
        {
            if (_isPublishingRunning)
            {
                Logger.Debug <ScheduledPublishController>(() => "Scheduled publishing is currently executing this request will exit");
                return(null);
            }

            _isPublishingRunning = true;

            try
            {
                // DO not run publishing if content is re-loading
                if (content.Instance.isInitializing == false)
                {
                    var publisher = new ScheduledPublisher(Services.ContentService);
                    var count     = publisher.CheckPendingAndProcess();
                    Logger.Debug <ScheduledPublishController>(() => string.Format("The scheduler processed {0} items", count));
                }

                return(Json(new
                {
                    success = true
                }));
            }
            catch (Exception ee)
            {
                var errorMessage = "Error executing scheduled task";
                if (HttpContext != null && HttpContext.Request != null)
                {
                    if (HttpContext.Request.Url != null)
                    {
                        errorMessage = string.Format("{0} | Request to {1}", errorMessage, HttpContext.Request.Url);
                    }
                    if (HttpContext.Request.UserHostAddress != null)
                    {
                        errorMessage = string.Format("{0} | Coming from {1}", errorMessage, HttpContext.Request.UserHostAddress);
                    }
                    if (HttpContext.Request.UrlReferrer != null)
                    {
                        errorMessage = string.Format("{0} | Referrer {1}", errorMessage, HttpContext.Request.UrlReferrer);
                    }
                }
                LogHelper.Error <ScheduledPublishController>(errorMessage, ee);

                Response.StatusCode = 400;

                return(Json(new
                {
                    success = false,
                    message = ee.Message
                }));
            }
            finally
            {
                _isPublishingRunning = false;
            }
        }
예제 #3
0
        public JsonResult Index()
        {
            if (_isPublishingRunning)
            {
                return(null);
            }
            _isPublishingRunning = true;

            try
            {
                // DO not run publishing if content is re-loading
                if (content.Instance.isInitializing == false)
                {
                    var publisher = new ScheduledPublisher(Services.ContentService);
                    publisher.CheckPendingAndProcess();
                }

                return(Json(new
                {
                    success = true
                }));
            }
            catch (Exception ee)
            {
                LogHelper.Error <ScheduledPublishController>("Error executing scheduled task", ee);

                Response.StatusCode = 400;

                return(Json(new
                {
                    success = false,
                    message = ee.Message
                }));
            }
            finally
            {
                _isPublishingRunning = false;
            }
        }
        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
        }