2

Is there any automation script or way to find out if Any items in Publishing Queue for publishing before stop/start Publisher via a schedule via batch job using "net stop service name"

Please suggest

Just to add, what i have tried: have tried by writing a .batch file, whcih will clean up the : D\Tridion\log folder (depends on Publisher), so i stop/start the server by using net stop "Tridion Content Manager Publisher", but it invloves the risk of losing item whcih are in waiting/in progress state in pub queue. I dont have any idea if this can be achieved by using .bat script. So asking for suggestions. Please let me know your concerns on this

5 Answers 5

7

Here's the Powershell script that is using TOM and will be working on 2009:

$tdse = new-object -com TDS.TDSE
$tdse.Initialize()
$managementInfo = $tdse.GetManagementInfo()

$filter = $tdse.CreateListRowFilter()
$filter.SetCondition( "InfoType", "1" ) 

$list =[xml] $managementInfo.GetListPublishQueue($filter)
if ($list.ListPublishQueue.Item.ID -eq $null)
{
    "Restarting service"
    Restart-Service TcmPublisher
    "Restarted"
}
else
{
    "There are queue messages"  
}
0
5

On SDL Tridion 2013 you can use PowershellModule. Just type:

Import-Module Tridion.ContentManager.Automation

And then

Get-TcmQueueInfo

to see the state of all queues. And finally this:

(Get-TcmQueueInfo | where-object {$_.Title -eq "Publish queue"}).NumberOfMessages

to get number of messages in the publishing queue. If it's 0 you can safely restart your publisher

2
  • Tthis is great, hanks for your response.But We are using 2009 version, can this be done in 2009 as well? Commented Jul 23, 2013 at 13:26
  • No, you can write VBScript on TOM to get the same information Commented Jul 23, 2013 at 13:30
3

update: this answer assumed version 2011 and up where the Core Service is available, for 2009 you could use the TOM interop but this PowerShell answer really is easier.

If you dislike PowerShell and thus this PowerShell answer, you can always create a Console application using the core service to list the queue:

using System;
using System.ServiceModel;
using System.Xml;
using Tridion.ContentManager.CoreService.Client;

namespace PublishQueueTaskLister
{
    class Program
    {
        // use net.tcp core service client as we are on the machine itself
        const string EndpointUri = "net.tcp://localhost:2660/CoreService/2012/netTcp";

        static int Main(string[] args)
        {
            bool verbose = false;
            if (args.Length > 0)
            {
                if (args[0].Equals("-v"))
                verbose = true;
            }

            var binding = new NetTcpBinding
            {
                MaxReceivedMessageSize = 2147483647,
                ReaderQuotas = new XmlDictionaryReaderQuotas
                {
                    MaxStringContentLength = 2147483647,
                    MaxArrayLength = 2147483647
                }
            };
            CoreServiceClient client = new CoreServiceClient(binding, new EndpointAddress(EndpointUri));

            int active = 0;
            foreach (PublishTransactionData publishTransaction in client.GetSystemWideList(new PublishTransactionsFilterData()))
            {
                // check for active states
                if (publishTransaction.State == PublishTransactionState.InProgress ||
                    publishTransaction.State == PublishTransactionState.ReadyForTransport ||
                    publishTransaction.State == PublishTransactionState.Rendering ||
                    publishTransaction.State == PublishTransactionState.Resolving || 
                    publishTransaction.State == PublishTransactionState.Throttled ||
                    publishTransaction.State == PublishTransactionState.Transporting ||
                    publishTransaction.State == PublishTransactionState.Undoing)
                {
                    active++;
                }
            }

            if (verbose)
            {
                Console.WriteLine(string.Format("Number of active Publish Transactions: {0}", active));
            }
            return active;            
        }
    }
}

That will return the number of active items in the %ERRORLEVEL% variable which you can use in a batch file like this:

@ECHO OFF
PublishQueueTaskLister.exe

IF NOT %ERRORLEVEL% == 0 GOTO error

ECHO Publish Queue is empty.
REM restart publisher
net stop "Tridion Content Manager Publisher"
net start "Tridion Content Manager Publisher"
GOTO end

:error
ECHO %ERRORLEVEL% active Publish Transactions in Queue.

:end
3
  • Many thanks and i will try to do something like this and will inform you, but we dont have core service installed in the system. Commented Jul 24, 2013 at 6:27
  • We do not have Core Service installed in the system, can i get the errorlevel details from the exe but using TOM/any other Tridion APIs but not the core service API i.e. Tridion.ContentManager.CoreService.Client? Please suggest. Commented Jul 24, 2013 at 6:42
  • as mentioned on the top of my answer, I had given this specific answer as an alternative to the PowerShell answer when you hadn't specified what version you were on. Now that you specified you are on 2009, you don't have a Core Service (that is only available as of version 2011) so this answer does not apply to you. You could rewrite the code to use TOM Interop instead of the Core Service (that will be possible for 2009), but it will be a lot more code compared to the given PowerShell answer, so that one applies best to you. Commented Jul 24, 2013 at 6:48
3

I have achieved the desired by modifiying the script in an exe (TOM apis) file and then calling it from a .bat file (as suggested by Bart) in the following way: The only thing it uses TDSE object whcih seems to be causing some meamory leckage issues (though we are releasing the TDSE object properly) in our environment. But it checks the queue and then retsrt services if nothing in queue except '6' status (Success).

using System.Text;
using Tridion.ContentManager.Interop.TDSDefines;
using Tridion.ContentManager.Interop.TDS;
using System.Runtime.InteropServices;

namespace PublishingQueueMonitor
{
    class PublishingQueueStatus
    {     
        static int Main(string[] args)
        {
            int active = 0;
            int successCount = 0;

            TDSE tdse = new TDSE();
            XmlDocument itemsXmlDoc = new XmlDocument();                     
            tdse.Initialize();
            ManagementInfo managementInfo = tdse.GetManagementInfo();
            ListRowFilter filter = tdse.CreateListRowFilter();
            //filter.SetCondition("InfoType", "1");

           String itemsXml = managementInfo.GetListPublishTransactions(filter); //for Publish Trasactions
           //String itemsXMLNew=managementInfo.GetListPublishQueue(filter);  //for publishing queue items
Marshal.ReleaseComObject(tdse);

            itemsXmlDoc.LoadXml(itemsXml);
            XmlElement root = itemsXmlDoc.DocumentElement;
            if (root.ChildNodes.Count>0)
            {
                foreach (XmlNode node in root.ChildNodes)
                {
                    int state = Convert.ToInt32(node.Attributes["State"].Value);
                    DateTime stateChangeDate = Convert.ToDateTime(node.Attributes["StateChangeDate"].Value).Date;
                    if ((state != 6) && (stateChangeDate >= DateTime.Now.AddDays(-2)))   //6 is Success and date is publish state change date
                    {
                        active++;
                    }

                    else if (state == 6)
                    {
                        successCount++;

                    }
                }
            }
            Console.WriteLine("No of Ongoing Publishing Items in Queue: {0}", active);
            Console.WriteLine("No of Successfully published Items in Queue: {0}", successCount);           
            Console.Read();
            return active;

        }
    }
}

The .bat script as follows: (suggested by Mr. Bart)

@ECHO OFF

PublishingQueueMonitor.exe

IF NOT %ERRORLEVEL% == 0 GOTO error

ECHO Publish Queue is empty.
REM restart publisher

:: set folder path

 set dump_path=D:\Tridion\log\

:: set min age of files and folders to delete

 set max_days=0

net stop "Tridion Content Manager Publisher"

:: remove files/subdirectories from %dump_path%

forfiles -p %dump_path% -s -m *.* -d -%max_days% -c "cmd  /c del /q @path"  

:: remove empty directories

dir /ad /b /s %dump_path% |sort /r >tmp.tmp
for /f "delims=" %%a in (tmp.tmp) do rd "%%a" 2>nul
del tmp.tmp

net start "Tridion Content Manager Publisher"

GOTO end

:error
ECHO %ERRORLEVEL% active Publish Transactions in Queue.

Please let me know your concerns. Thanks again for the indication

1

Tridion core service is the way to find which items are there in queue at any moment of time. You can fetch the publish transactions applying filters and then can run your script. each transaction gives the id of item published. Please refer to code

8
  • Is there any other way for this? Commented Jul 23, 2013 at 11:49
  • I am not sure, If anyone else can suggest you something Then I am eager to know as well
    – AlokBhatt
    Commented Jul 23, 2013 at 11:50
  • I am using 2009, so can i create an exe (.NET program) whcih will look for publishing queue and then will start stop service. Using TOM .net APIS (whcih apis need to be used) Please share your views. Commented Jul 23, 2013 at 12:02
  • If your are creating an exe then why don't you use Core Service. I think it very easy to use with the code I have shared with you.
    – AlokBhatt
    Commented Jul 23, 2013 at 12:05
  • Just to add, what i have tried: have tried by writing a .batch file, whcih will clean up the : D\Tridion\log folder (depends on Publisher), so i stop/start the server by using net stop "Tridion Content Manager Publisher", but it invloves the risk of losing item whcih are in waiting/in progress state in pub queue. I dont have any idea if this can be achieved by using .bat script. So asking for suggestions. Please let me know your concerns on this Commented Jul 23, 2013 at 12:09

Your Answer

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

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