Search code examples
sitecorestaging

Rebuild Sitecore Search index and link database in a background process


We have a staged environment with 1 CMS and 3 Slave servers

I want to create a page on the slave server, which will be called by the staging module on a successful publish, that will rebuild all indexes and the links database.

I know I can use:

Globals.LinkDatabase.Rebuild(Factory.GetDatabase("web"));

to rebuild the link database.

How do I get the above code in a separate process that has access to the sitecore context and also how do I rebuild all the indexes for the web database - again in a separate background thread.

Thanks


Solution

  • I've come across this issue before with Sitecore and took a slightly different approch. Instead of having a page that the staging module calls I tapped into the publish:end event and added a custom handler to rebuild the Link Database.

    <event name="publish:end">
        <handler type="Sitecore.Publishing.HtmlCacheClearer, Sitecore.Kernel" method="ClearCache">
            <sites hint="list">
                <site>website</site>
            </sites>
        </handler>
        <handler type="Sitecore.EventHandlers.CredentialCacheClearer, Sitecore.EventHandlers" method="ClearCache">
            <sites hint="list">
                <site>website</site>
            </sites>
        </handler>
    
        // Custom Publish Action Below
        <handler type="Customized.Publish.LinkDatabase, Customized" method="Process"/>
    </event>
    
    namespace Customized.Publish
    {
        public class LinkDatabase
        {
            /// <summary>
            /// Rebuild the web link database.
            /// </summary>
    
            public void Process()
            {
                // Web db
                Sitecore.Globals.LinkDatabase.Rebuild(Sitecore.Configuration.Factory.GetDatabase("web"));
            }
    
            /// <summary>
            /// For invoking as an event, typically publish:end.
            /// </summary>
            public void Process(object sender, EventArgs args)
            {
                this.Process();
            }
        }
    }