Publishing Service: Scheduled publishing

Publishing Service enables you to schedule tasks through the task scheduler. It comes with the PublishTask and PublishJobCleanUpTask default tasks. The good news is that we can create custom scheduled tasks as well.

In this post, we are going to create a scheduled publishing task which on a given Interval queues a new publish job.

Update: Before we get started, the quickest way to add a scheduled publishing task is still by enabling the Sitecore.Tasks.PublishAgent agent. By default it is disabled. Just add to your CM instance a configuration file like the following:

The task scheduler is a service that manages the creation of tasks at startup as well as enabling the addition and execution of tasks at runtime. Publishing Service comes with some default scheduled tasks. These tasks are contained in the (PublishServiceInstallationPath)\config\sitecore\sc.publishing.tasks.xml configuration file.

default scheduled tasks
Figure 1: Default scheduled tasks
  • The PublishTask processes the next queued item in the publish queue.
  • The PublishJobCleanUp removes any publishing jobs that are over a certain age. It also removes the jobs associated manifests and manifest results.
  • PublishOperationCleanUpTask removes any publishing operations that have been processed and are no longerd required.

What we are going to do is to create a scheduled task that will queue a publish job every 10 seconds.

Let’s start by creating a new ASP.NET MVC project using the ASP.NET Web Application (.NET Framework) template and selecting .NET Framework 4.5.2. Also, add a class and a XML configuration file, and add the Publishing Service references according to the image below.

solution configuration edited.png
Figure 2: Solution structure

Also, I added a Post-build events that copies the bin\ folder to the Publishing Service installation folder.

<br>
XCopy /Y /E "$(TargetDir)*.*" "X:\inetpub\wwwroot\sandboxpublishingservice.dev.local\"<br>

Next, we are going to update the sc.publishing.custom.tasks.xml configuration file we just created.

basic task xml configuratin file
Figure 3: sc.publishing.custom.tasks.xml configuration file

All custom tasks must be added after Settings/Sitecore/Publishing/Services/Scheduler/Options/Tasks node. You can name it however you prefer. In my example, I used /CustomTask node, but you can for example name it /AutoPublish and add as many custom tasks as you want.

Within our CustomTask node we defined the TaskDefinition node and the TriggerDefinition.

The TaskDefinition defines the class that contains the logic for your custom scheduled task. It also contains binding options so you can provide parameters in your configuration file that binds to properties in your class.

mapping properties custom task
Figure 4: Binding properties

A task cannot run without triggers. You can register multiple triggers on the same task. e.g. two interval triggers that trigger a task at different polling intervals.

multiple triggers on a task
Figure 5: Multiple triggers on the same task

Now we are going to update the AutoPublishTask.cs file we created before.

basic auto publish task

Figure 6: AutoPublishTask.cs file

Publishing Service defines the dependencies (and their resolver) in the (PublishingServiceInstallationFolder)\config\sitecore\publishing\sc.publishing.services.xml configuration file. If you want to add a new dependency you can either change this configuration file (not recommended) or add a new XML configuration file to (PublishingServiceInstallationFolder)\config\sitecore\publishing folder and define the dependencies after Settings/Sitecore/Publishing/Services node.

custom dependency.png
Figure 7: Adding custom dependencies

Finally, publish your solution to the Publishing Service installation folder.

Make sure the your project’s assembly get copied to the root of the Publishing Service installation folder and that your configuration file gets published to \config\sitecore\publishing.

If you log in your Sitecore instance and open the Publishing Service Dashboard, you should see a publishing task job being added every (or nearly) 10 seconds.

publishing service dashboard.PNG
Figure 8: Publishing dashboard

You can find the complete code on my GitHub repository. There, I also implemented the binding of the auto publish task properties so the parameters are read from the XML configuration file.

Update – September 2, 2018
I thought it was important to mention that I set the publishing interval to 10 seconds just for the sake of local development and exemplification. You should never use such a short interval in a production environment.

7 thoughts on “Publishing Service: Scheduled publishing

  1. I attempted to implement this solution, and while it does appear to be “Auto publishing” it is not picking up my content changes after save. If I publish the item manually, or do a manual full site publish it works as expected. What could I be missing with the Auto Publish service, I’m not seeing anywhere in the C# Class or XML configs that instruct the service to do a “Full Publish”.

    Like

    1. Hi James. In the .config file, more specifically here /Settings/Sitecore/Publishing/Services/Scheduler/Options/Tasks/CustomTask/TaskDefinition/Options/Metadata/PublishType, you have the value “Full site publish” which should trigger a full site publish. Please, let me know if it works and also which version of the Publishing Service module you are using.

      Like

      1. It is set to “Full site publish” just as your github source. I dug into the .dlls for the Publishing service and it appears to support “Full, Incremental, Republish, and SingleItem”. The publishing queue displays type “Site publish”, and indicates “Item versions affected: 1” and completes in about 1-3 seconds for each iteration of the job.

        However, when I change items in my content tree and save those items, they are not picked up by the automated publishing job. I usually have to force the publish manually and that even sometimes does not pickup the changes. I’m not sure if there is some caching happening, or the Publishing Service is not working correctly.

        Like

      2. – “I usually have to force the publish manually and that even sometimes does not pickup the changes”
        Now that you mentioned that, I also noticed the same behavior in a project I am working on. We are using Sitecore 9.0 Update-1 and Publishing Module 3.1.3 rev. 190424. Due to time limitation, I didn’t investigate it further and had to switch the Publishing Module off. As soon as I find some time, I will investigate it further. Curiously, when I recycle the App’s pool, the publishing is effective and I am able to see the changes in the web database.

        Like

      3. I have narrowed it down to caching, it appears the Publishing Service is not clearing the cache often enough. I navigate to the cache admin tool, http:///sitecore/admin/cache.aspx and then clear cache and “boom” my content is there. We are using Sitecore 9.0.2 with Sitecore Publishing Service 3.1.3 rev. 190424.

        Like

      4. Gotcha! In this case you should file a ticket to Sitecore Support. It looks like some bug on their side.

        Like

    2. In the publishing servicing config file Sitecore.Publishing.Service.Delivery.config which should be on every CD server you deploy to, add all related publishing:end:remote event handlers, under new event called , the main one will be the html cache for the website.

      website
      yourcustomesite

      If you use glassmapper there will be glass mapper cache clearing and etc.
      Then when you deploy to CD1 (web1) check the CD1 logs and make sure you have the following:
      ManagedPoolThread #3 10:23:14 INFO HtmlCacheClearer clearing HTML caches for all sites (2).
      ManagedPoolThread #3 10:23:14 INFO HtmlCacheClearer done.
      ManagedPoolThread #3 10:23:14 INFO RenderingParametersCacheClearer clearing caches for the sites: yoursite, website.
      ManagedPoolThread #3 10:23:14 INFO RenderingParametersCacheClearer done.
      ManagedPoolThread #3 10:23:14 INFO Started clearing all Glass.Mapper caches
      ManagedPoolThread #3 10:23:14 INFO Started clearing Glass.Mapper cache for context Default
      ManagedPoolThread #3 10:23:15 INFO Finished clearing Glass.Mapper cache for context Default
      ManagedPoolThread #3 10:23:15 INFO Finshed clearing all Glass.Mapper caches
      ManagedPoolThread #4 10:23:15 INFO Job started: Index_Update_IndexName=sitecore_web_index
      ManagedPoolThread #2 10:23:15 INFO Job started: Index_Update_IndexName=sitecore_marketingdefinitions_web
      ManagedPoolThread #5 10:23:15 INFO Job started: Index_Update_IndexName=sitecore_marketing_asset_index_web
      ManagedPoolThread #0 10:23:15 INFO Job started: Index_Update_IndexName=sitecore_fxm_web_index
      ManagedPoolThread #2 10:23:15 INFO Job ended: Index_Update_IndexName=sitecore_marketingdefinitions_web (units processed: )
      ManagedPoolThread #5 10:23:15 INFO Job ended: Index_Update_IndexName=sitecore_marketing_asset_index_web (units processed: )
      ManagedPoolThread #0 10:23:15 INFO Job ended: Index_Update_IndexName=sitecore_fxm_web_index (units processed: )
      ManagedPoolThread #1 10:23:15 INFO CustomCacheClearer clearing all custom sitecore cache other than Sitemap XML for all sites.
      ManagedPoolThread #4 10:23:16 INFO Job ended: Index_Update_IndexName=sitecore_web_index (units processed: )
      3840 10:23:16 INFO CustomCacheClearer clearing all custom sitecore cache other than Sitemap XML for all sites.
      ManagedPoolThread #1 10:23:18 INFO CustomCacheClearer clearing all custom sitecore cache other than Sitemap XML for all sites.

      The same relates to your custom indexes:
      Make sure in the same config you add your custom index under
      publishingservice:searchindex:rebuild

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s