Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Logan P. 47 posts 217 karma points
    Mar 09, 2016 @ 19:44
    Logan P.
    0

    Courier 2.52.3 events

    Ugh, I am having a very frustrating experience trying to work with events in courier. I am trying to clear the cache on the Target server whenever a piece of content is transferred.

    The documentation that I am finding on Courier's event models seems to be out of date. DocumentItemProvider no longer provides an Instance() method which, most documentation points to. After digging in the code some I found the ExtractionManagerEvents class, which I tried to hook into the ExtractingItem, PostProcessed and PostProcessedItem events. Unfortunately, only the PostProcessed event fires, but most items in the RevisionExtraction object passed into the event are null, including the CurrentItem property.

    In the app starting event, I have subscribe to the events like this

    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    {
    
            //Hook into the courier event so we can delete content finder cache.
            ExtractionManagerEvents.ExtractingItem += ExtractionManagerEvents_ExtractingItem;
    
            ExtractionManagerEvents.PostProcessed += ExtractionManagerEvents_PostProcessed;
    
            Log.Info("We are hooking into the PostProcessed Event");            
    
            ExtractionManagerEvents.PostProcessedItem += DeleteContentFinderCache;        
    
    
            base.ApplicationStarted(umbracoApplication, applicationContext);
     }
    

    Then my event handlers look like this:

    void ExtractionManagerEvents_PostProcessed(object sender, ExtractionEventArgs args)
        {
            RevisionExtraction re = (RevisionExtraction)sender;
    
            try
            {
                re.Context.ExecuteEvent("ClearContentFinderCache", re.CurrentItem.ItemId, null);
                Log.Info("In the ExtractionManagerEvents_PostProcessed Method. We are queing the evernt.");
            }
            catch(Exception e)
            {
    
            }
        }
    
        void ExtractionManagerEvents_ExtractingItem(object sender, ItemEventArgs e)
        {
            Log.Info("In the ExtractionManagerEvents_ExtractingItem Method. We are queing the evernt.");
        }
    
    
        //This ties into the courier event for when an item is done being processed. This will queue up the ClearContentFinderCache event that we wrote.
        void DeleteContentFinderCache(object sender, ItemEventArgs args)
        {
            Log.Info("In the DeleteContentFinderCache Method. We are queing the evernt.");
            //Get the COURIER document, this is NOT the umbraco document.
            Umbraco.Courier.ItemProviders.Document d = args.Item as Umbraco.Courier.ItemProviders.Document;
    
            //Get the item provider, which is the sender.
            ItemProvider provider = sender as ItemProvider;
    
            //Queue up the event so it is called when the courier deployment is complete. 
            //This is found in the ClearContentFinderCacheEventProvider.cs in the App_Start folder.
            provider.ExecutionContext.ExecuteEvent("ClearContentFinderCache", d.ItemId, null);
        }
    

    Unfortunately, I can't even call my ClearContentFinderCache class, which is an ItemProviderEvent, because the RevisionExtraction.CurrentItem is null. The ClearContentFinderCache looks like this:

    public class TestDIP : DocumentItemProvider
    {
        private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
    
        public override Umbraco.Courier.Core.SystemItem FindSystemItem(string id)
        {
            return base.FindSystemItem(id);
        }
    
        public override Umbraco.Courier.Core.Item HandlePostProcess(Umbraco.Courier.Core.Item item)
        {
            return base.HandlePostProcess(item);
        }
    
        protected override void OnPostProcessed(Umbraco.Courier.Core.ItemEventArgs e)
        {
            Log.Info("MyCourier OnPostProcessed");
            base.OnPostProcessed(e);
        }
    }
    
    public class ClearContentFinderCacheEventProvider : ItemEventProvider
    {
        private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
    
    
        public override string Alias
        {
            get { return "ClearContentFinderCache"; }
        }
    
        public override void Execute(Umbraco.Courier.Core.ItemIdentifier itemId, Umbraco.Courier.Core.SerializableDictionary<string, string> parameters)
        {
            //Get the umbraco helper
            UmbracoHelper uh = new UmbracoHelper(UmbracoContext.Current);
    
            //get the ipublished content
            IPublishedContent content = uh.TypedContent(itemId);
    
            if(content == null)
            {
                Log.Info("Trying to clear the Content Finder cache for item id " + itemId + ", but the item is not published or doesn't exist.");
                return;
            }
    
            bool clearedSuccessfully = ContentFinderCache.ClearRouteCacheForItemUrl(content.Url);
    
            if(!clearedSuccessfully)
            {
                Log.Info("Trying to clear the Content Finder cache for item id " + itemId + ", but there was an issue.");
                return;
            }
            else
            {
                Log.Info("Cleared the cached for content id " + itemId + " successfully!");
            }
        }
    }
    

    As you can see, I even have tried to extend the DocumentItemProvider in hopes that it would get called on PostProcessing and I could call my Cache Clearer. No luck though. Any pointers on how to clear the cache on the target server on transfer?

    Once again this is the latest version of Courier 2.52.3.

    Thanks!

  • Logan P. 47 posts 217 karma points
    Mar 21, 2016 @ 18:15
    Logan P.
    0

    Could anyone give me a little push with this? I still am having trouble with these events in Courier.

  • Graham 1 post 71 karma points
    Apr 04, 2016 @ 08:16
    Graham
    0

    Thanks for posting this as it got me going in the right direction. I hooked into all the events available on ExtractionManagerEvents and tried transferring a media item.

    I found that the events only fire after you have created a revision transferred it and are installing that revision. If you use courier direct from a content item page to just transfer that item I could not see the events firing.

    Anyway the events fired as follows

    1. Extracting – Empty Event args
    2. ExtractingItemResources – eventargs contain name but no other info
    3. ExtractedItemResources – eventargs contain image name but no other info
    4. ExtractingItemResources – eventargs contain property data that has the item and path in
    5. ExtractedItemResources - eventargs contain property data that has the item and path in
    6. Extracting - Empty Event args
    7. PostProcessing – empty eventagrs
    8. PostProcessed – empty eventargs

    Here's an example of the event handler

        void ExtractionManagerEvents_ExtractedItemResources(object sender, ItemEventArgs e)
        {
            // Log stuff here 
        }
    

    As you can see I'm using ItemEventArgs not ExtractionEventArgs and that seems to work. I used the same signature with all the event handlers.

    Hope that helps

  • Jay 425 posts 652 karma points
    Mar 20, 2017 @ 11:54
    Jay
    0

    Hey Logan, did you came across any solution. Need to do something similar. Thanks

Please Sign in or register to post replies

Write your reply to:

Draft