4

I have a requirement to invalidate the file in cdn cache on completion of sitecore item publishing. To achieve this I have created custom processor and patched into "publishEndResultBatch" pipeline, my patches are displaying in showconfig. But the code is not triggered, I have debugged and added logs, no symbol to trigger this processor.

I have even created custom event handler and patched in "publishingservice:publishend" event. This is also not triggered.

No issue in item publish.

Is it need any additional settings to use the custom events or processor in sitecore publishing service?

Below are the package details,

  • Instance
    • Sitecore 9 update 1
  • Sitecore Publishing Service
    • Sitecore Publishing Module 3.1.1 rev. 180807.zip
    • Sitecore Publishing Service 3.1.1 rev. 180807-x64.zip

Below are the code snippet

Custom processor:

public class CustomPublishEndResultBatch
{
    Log4NetLogger _logger = new Log4NetLogger();
    public void Process(PublishEndResultBatchArgs args)
    {
        _logger.Log("publishEndResult Batch", EnumUtil.LoggingType.Info, EnumUtil.ExceptionTier.PublishingService, null);

        try
        {
            if (args.TotalResultCount >= 1)
            {
                Guid filePathFieldGuid = new Guid("{2134867A-AC67-4DAC-836C-A9264FD9D6D6}");
                var affectedItem = args.Batch.Select(b => b.EntityId).Distinct().Count();
                var modifiedMediaItems = args.Batch.Where(b => b.Type == ManifestOperationResultType.Modified && b.EntityType == ManifestEntityType.Media).Select(b => b.Metadata).Distinct().ToList();
                var pdfItems = modifiedMediaItems.Select(b => b.FieldChanges.Where(x => x.FieldId == filePathFieldGuid && x.Value.ToLower().Contains(".pdf")).Any()).ToList();
            }
        }
        catch (Exception ex)
        {
            _logger.Log("PublishEndResultProcessor is failed", EnumUtil.LoggingType.Error, EnumUtil.ExceptionTier.PublishingService, ex);
        }

    }

}

Custom Event :

public class CustomPublishEndHandler
{
    public void OnItemPublished(object sender, EventArgs args)
    {
        List<string> publishedIds = new List<string>();
        ILogger _logger = new Log4NetLogger();
        _logger.Log("Custom Publish end event", EnumUtil.LoggingType.Info,EnumUtil.ExceptionTier.UserProfileService, null);

        var itemArgs = args as ItemProcessedEventArgs;

        if (itemArgs != null && !ID.IsNullOrEmpty(itemArgs?.Context?.ItemId))
        {
            GetPDFItemPath(itemArgs.Context.ItemId);

        }
    }

    public void GetPDFItemPath(ID processedItem)
    {
        //TODO: Change this step
        var item = Factory.GetDatabase("master").Items.GetItem(processedItem);

        if (item != null && item.TemplateID == Sitecore.Data.ID.Parse("{0603F166-35B8-469F-8123-E8D87BEDC171}"))
        {
            var thread = new Thread(() => CdnFileInvalidationHelper.InvalidateContent(item.Paths.FullPath));
            thread.IsBackground = true;
            thread.Start();
        }
    }
}

Note - This custom event is working fine when adding into normal sitecore publishing event "publish:itemProcessed"

Configuration Entries:

<event name="publishingservice:publishend" patch:source="Sitecore.Publishing.Service.config">
<handler type="Sitecore.Publishing.Service.Events.PublishingJobEndHandler, Sitecore.Publishing.Service" method="StartPublishEndPipeline">
    <param name="manifestResultLoadBatchSize">500</param>
</handler>
<handler type="Sitecore.Publishing.Service.Events.PublishEndHandler, Sitecore.Publishing.Service" method="TriggerPublishEnd"/>
<handler type="AllianzGI.DEXTER.Web.Extensions.Events.CustomPublishEndHandler, AllianzGI.DEXTER.Web.Extensions" method="OnItemPublished"/>

<publishEndResultBatch argsType="Sitecore.Publishing.Service.Pipelines.BulkPublishingEnd.PublishEndResultBatchArgs" patch:source="Sitecore.Publishing.Service.config">
<processor type="Sitecore.Publishing.Service.Pipelines.BulkPublishingEnd.RaiseRemoteEvents, Sitecore.Publishing.Service">
<param name="remoteEventCacheClearingThreshold">1000</param>
<param name="targetCacheClearHistory" ref="publishing.service/targetCacheClearHistory"/>
</processor>
<processor type="AllianzGI.DEXTER.Web.Extensions.Pipelines.CustomPublishEndResultBatch, AllianzGI.DEXTER.Web.Extensions"/>

6
  • Are you able to share your code?
    – Anicho
    Commented Feb 14, 2019 at 8:52
  • @Anicho, Added the code snippet in the question Commented Feb 14, 2019 at 9:39
  • Can you please add your config entries as well.. Commented Feb 14, 2019 at 16:01
  • @Gobinath, Added configuration details here, please check and let me know your point, it will be helpful. Commented Feb 20, 2019 at 5:57
  • @ThenmozhiSubramaniam if you place your handler namespace in the Sitecore Event publish:end and publish:end:remote does it work? Also, check your publishing logs for any errors Commented Feb 20, 2019 at 7:19

2 Answers 2

0

If I understand your requirement correct then you just want to invoke your custom code during the "publishingservice:publishend". So you can add a handler in the event "publishingservice:publishend", something like the below.

Added the below patch in the "App_Config\Include" folder. [give a name on your own z.anything.config, i always prefer adding a patch instead of editing the Sitecore's original config files/entries]

<sitecore>
  <events>
    <event name="publishingservice:publishend">
      <handler type="Portal.Poc.Foundation.CustomPublisher, Portal.Poc.Foundation" method="DoCustomCode"/>
    </event>
  </events>
</sitecore>

And the Code is,

using Sitecore.Diagnostics;
using System;
namespace Portal.Poc.Foundation
{
    public class CustomPublisher
    {
        public CustomPublisher()
        {
        }
        public void DoCustomCode(object sender, EventArgs args)
        {
            Log.Info("You are in custom Publish End Handler", "PublishEnd");
        }
    }
}

The above code is executed whenever an item publish is invoked, tested by putting a breakpoint as well as the above log is written in the log file.

Note - This is tested in the same version of Sitecore and Publish Module & Service you mentioned in the above.

The class PublishingJobEndHandler in the event "publishingservice:publishend" invokes the "publishEndResultBatch" pipeline. So I guess if you want to implement a processor then you can customize/extend the existing or you can create your own pipeline and invoke the same from your custom handler which you are going to add in the "publishingservice:publishend".

Hope this helps.

2
  • I have used the same step still it's not triggering, but items are published and publishing service has the log of the item but custom publish log is not there in sitecore. Commented Feb 21, 2019 at 10:00
  • @ThenmozhiSubramaniam Is there any custom config added in your code or customized the publish module or service.? also can you please check the sitecore log for any errors.? Commented Feb 21, 2019 at 14:54
0

This issue is resolved by removing the unused configuration file. In our instance, there was a backup folder which contains the configuration file, which blocked the custom event handler on publishing service.

1
  • Hi....How did you resolve this error? as i am also not ablt to hit any breakpoint and debug in my local? Commented Nov 3, 2020 at 7:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.