Sitecore publishing at a specific time


Sitecore ships with an agent that can be used to publish a database periodically.  The agent is defined in the web.config file.
 
        master
        web
        incremental
        en, da
      
The biggest drawback however is that the agent will publish at specified intervals. This might be a problem as publishing at every so many minutes or hours will force clear the cache and slow the system down. In most cases you would want to publish at a specific time of the day. This is tricky as Sitecore queues up tasks and may not get to the task at the exact desired time. If publishing at the exact same time every day is a must, you will have to create a windows scheduled task to trigger publishing. Here is an example module you could use http://sitecoreblog.alexshyba.com/2007/02/publish-at-specific-time.html However if you wish to use Sitecore task scheduler and are okay with publishing around the same time here is how you can do it. 1)      Create a custom agent that accepts time as a parameter
 
        master
        web
        incremental
        en, da
        13:50:00
      
This agent will be invoked every two minutes (interval=”00:02:00”) and the method “Run” would be executed 2)      In the “Run” method add a check to verify if it is time for publishing
      public void Run()
        {
            if (this.IsPublishingTime())
            {
                foreach (Language language in this._languages)
                {
                    this.StartPublish(language);
                }
                this.HasTaskExecuted = true;
            }
        }
        private bool IsPublishingTime()
        {
            // this.HasTaskExecuted prevents publishing more than once.
            bool isTime = !this.HasTaskExecuted;
            TimeSpan timeDiff = DateTime.Now.TimeOfDay.Subtract(this.PublishingTime);
            // only return true if the time diff is less than 30 minutes
            if (isTime)
                isTime = timeDiff.Hours == 0 && timeDiff.Minutes >= 0 && timeDiff.Minutes <= 30;
            // reset the setting to false after 30 minutes
            else {
                this.HasTaskExecuted = timeDiff.Minutes >= 30 ? false : true;
            }
            return isTime;
        }
Download the source code  
Categories: Content Management
Tags: Sitecore;

SEARCH ARTICLES