4

I need to run specific agents on demand. When I try to create a new instance of the agent using PowerShell's New-Object command it doesn't seem to work right.

7

Here are a few examples I was able to figure out today. I welcome any recommendations on a better way to do this.

Each example loops through the list of agents in the configuration then creates and starts a new job. Using the Sitecore Configuration Factory, agents are instantiated and applied as options to a new Sitecore Job.

CleanupAgent

foreach($node in [Sitecore.Configuration.Factory]::GetConfigNodes("scheduling/agent") | Where-Object { $_.Type -like "Sitecore.Tasks.CleanupAgent"}) {
    $obj = [Sitecore.Configuration.Factory]::CreateObject($node, $true)
    $name = [Sitecore.Xml.XmlUtil]::GetAttribute("type", $node)
    $method = [Sitecore.Xml.XmlUtil]::GetAttribute("method", $node)
    $options = New-Object Sitecore.Jobs.JobOptions $name, "", "", $obj, $method
    $options.AtomicExecution = $true
    $options.SiteName = "scheduler"
    [Sitecore.Jobs.JobManager]::Start($options)
}

CleanupPublishQueue, CleanupEventQueue, CleanupHistory

foreach($node in [Sitecore.Configuration.Factory]::GetConfigNodes("scheduling/agent") | Where-Object { $_.Type -like "*CleanupPublishQueue*" -or $_.Type -like "*CleanupEventQueue*" -or $_.Type -like "*CleanupHistory*"}) {
    $obj = [Sitecore.Configuration.Factory]::CreateObject($node, $true)
    $name = [Sitecore.Xml.XmlUtil]::GetAttribute("type", $node)
    $method = [Sitecore.Xml.XmlUtil]::GetAttribute("method", $node)
    $options = New-Object Sitecore.Jobs.JobOptions $name, "", "", $obj, $method
    $options.AtomicExecution = $true
    $options.SiteName = "website"
    [Sitecore.Jobs.JobManager]::Start($options)
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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