2

I have written a event system which will be triggered on any page save to publish it. It shows in the event viewer that the event is initiated but taken 30 mins to 1 hr or more to complete it's execution and log the necessary outcome. Does it take so long to execute. This is the code.

using System;
using System.IO;
using System.Collections.Generic;
using Tridion.ContentManager;
using Tridion.ContentManager.CommunicationManagement;
using Tridion.ContentManager.ContentManagement;
using Tridion.ContentManager.ContentManagement.Fields;
using Tridion.ContentManager.Extensibility;
using Tridion.ContentManager.Extensibility.Events;
using Tridion.Logging;
using Tridion.ContentManager.Publishing;
using Tridion.ContentManager.Templating;


namespace ClassLibrary1
{
    [TcmExtension("PagePublishOnItemSave")]
    public class Class1 : TcmExtension
    {
        EventSubscription _subscription;
       public Class1()
        {
            _subscription = EventSystem.Subscribe<IdentifiableObject, SaveEventArgs>
                                             (OnItemSavePost, EventPhases.Processed);

        }

       private static void OnItemSavePost(IdentifiableObject item, 
                                    SaveEventArgs args, EventPhases phase)
            {             
                IEnumerable<IdentifiableObject> items = new List<IdentifiableObject> { item };
                TcmUri pageid = new TcmUri(item.Id);
                bool rollBackOnFailure = true;
                bool includeComponentLinks = true;
                PublishInstruction instruction = new PublishInstruction(item.Session)
                {
                    DeployAt = DateTime.Now,
                    RenderInstruction = new RenderInstruction(item.Session)
                    {
                       RenderMode = Tridion.ContentManager.Publishing.RenderMode.Publish
                    },
                    ResolveInstruction = new ResolveInstruction(item.Session)
                    {
                         IncludeComponentLinks = includeComponentLinks
                    },
                    RollbackOnFailure = rollBackOnFailure,
                    StartAt = DateTime.MinValue
               };
               string publicationId = pageid.PublicationId.ToString();
               Publication publication = new Publication(new TcmUri(publicationId), item.Session);
               PublicationTarget ptar = new PublicationTarget(new TcmUri("tcm:0-11-65537"), 
                                                              item.Session);
               IEnumerable<PublicationTarget> pubTarget = new List<PublicationTarget>() {ptar};
               IList<Publication> links = ptar.Publications;
               if (links.Contains(publication))
               {
                   if (item.Id.ItemType == ItemType.Page)
                   {
                       PublishEngine.Publish(items, instruction, pubTarget);
                       using (StreamWriter writer = File.CreateText(
                                             @"c:\logfolder\Initiated " 
                                             + DateTime.Now.ToString("HHmmssffff") + ".txt"))
                       {
                           writer.WriteLine(item.Title + " is published");
                           writer.Close();
                       }
                   }
               }
           }
           public void Dispose()
           {
               _subscription.Unsubscribe();
           } 
       }
  }
3
  • Try running the code in debug mode. On the Tridion server, Open Visual Studio and this project. Attach to the Publisher and/or Tridion Service Host processes and your breakpoint will be hit.
    – robrtc
    Jun 20, 2014 at 18:12
  • This should not be the case, it should happen pretty much instantaneously. As per Robert. Try debugging in VS and see if anything becomes clear. Jun 21, 2014 at 21:23
  • I don't have the VS installed on Tridion server. Will try to debug remotely.
    – user945
    Jun 23, 2014 at 12:01

1 Answer 1

10

I may just not be seeing it in the code, but you're not implementing IDisposable and there's nothing in your code calling Dispose(). If you're not disposing of the subscription it never actually finishes executing, as Dispose is what enables garbage collection to clean up all of your references. Have your class implement IDisposable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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