Sitecore Publishing Service Failed Publish Handling

If you have opted in to using Sitecore’s Publishing Service module to scale up your publishing operations you have probably come across situations where a particular publishing job fails behind the scenes. There are many reasons that a publish job can fail from incorrect configuration to issues related to the manifest calculation that the publishing service executes for every job.

The problem with this is that the failure will only really be detected if you happen to be looking at the publishing dashboard, which is often not the case for editors publishing content using workflow or automated publications that are triggered as part of a deployment pipeline.

So how can we detect and alert ourselves to a failed publish job? Well the publishing service comes with it’s own set of pipelines and event handlers defined in Sitecore.PublishingService.config (which is installed as part of the publishing service module package for Sitecore). The event to pay attention to is publishingservice:publishend. You will notice that the publishing service comes with a couple of out of the box handlers, one of which is responsible for raising “traditional” publish related events such as publish:end and publish:end:remote to ensure that code listening to these events continues to function. Go ahead and add a custom handler and patch it in after the out of the box job end handler:

patch:after=”*[@type=’Sitecore.Publishing.Service.Events.PublishingJobEndHandler, Sitecore.Publishing.Service’]” type=”YourClass, YourAssembly” method=”FailedPublishAlert”

Within your handler you need to reference libraries that are unfortunately not currently distributed via nuget so you will have to add local references for these by fetching copies from your local bin folder::

  • Sitecore.Publishing.Service.dll
  • Sitecore.Framework.Publishing.Abstractions.dll

You can also write to the publishing log with a bit of setup in the constructor which gives you access to the publishing service logger:


public class PublishingServiceEventHandler
{
    protected readonly IPublishingLog _logger;

    public PublishingServiceEventHandler() : this(
        new PublishingLogWrapper())
    {

    }

    public PublishingServiceEventHandler(IPublishingLog logger)
    {
        _logger = logger;
    }
}

In the FailedPublishAlert method the first job is to get access to the PublishingJobEndEventArgs:


protected PublishingJobEndEventArgs ExtractArgs(EventArgs args)
{
    var scArgs = args as SitecoreEventArgs;

    if (scArgs == null)
        return null;

    if (scArgs.Parameters == null || !scArgs.Parameters.Any() || !(scArgs.Parameters[0] is PublishingJobEndEventArgs))
        return null;

    return (PublishingJobEndEventArgs)scArgs.Parameters[0];
}

With access to the PublishingJobEndEventArgs EventData property you can check the status of the publish job which is an enumeration and then handle failure cases however you want to:


public void FailedPublishAlert(object sender, EventArgs args)
{
    var publishArgs = ExtractArgs(args);

    if (publishArgs == null)
        return;

    if (publishArgs.EventData.Status == PublishJobStatus.Failed)
    {
        HandleFailure(publishArgs);
    }
}

protected virtual void HandleFailure(PublishingJobEndEventArgs publishArgs)
{
    _logger.Info($"handling failed publish: {publishArgs.EventData.JobId}");
}

There are plenty of properties on the PublishingJobEndEventArgs EventData property that should allow you to do something meaningful in the event of a failure:

  • CompareRevisions
  • IncludeDescendants
  • ItemId
  • JobId
  • LanguageNames
  • MetaData
  • PublishDate
  • PublishType
  • RepublishAll
  • SourceDatabaseName
  • Status
  • Targets
  • Username

To wrap up, the publishing service certainly outperforms traditional publishing and is pretty much a must for solutions with large scale publishing requirements, however it is not without its problems (some would say that’s an understatement) and publish jobs can fail for many reasons. Fortunately the events and associated data are available so we can at least handle failure.

Happy publishing!

 

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