We are on Sitecore 10.1.2 and we use Sitecore Publishing Service module. We often encounter random unresponsiveness of Publishing Service where content authors report that they tried publish an item or a site but the publishing dialog seemed unresponsive and errored out as in snapshot below after few seconds -
When we looked at the publishing service logs, we could see that it disposed off all the connections but did not attempt at recreating them.
An application pool recycle solves the problem but the catch is this blocks the content authors from publishing their changes until a developer gets to recycle the application pool for the publishing service.
We had an option of scheduled application pool recycle for the Publishing service but our application is used 24x7 across the globe by content authors for editing their content. So there were chances that application pool recycle may kill any publishing jobs and then create further problems.
As an attempt to solve this problem, we started developing a powershell script. The script hits the status API of publishing service to find if the publishing service is unresponsive or not -
http://$($pubService):$($port)/api/publishing/operations/status/
Based on the response of this API, the script decides whether to recycle the application pool for the publishing service or not. Here is the script -
//script to restart publishing service
param ($pathToAppCMD = "C:\Windows\System32\inetsrv\appcmd", $pubService = "<publshing service url without https e.g. publishingservice.com>", $port = "", $logFilePath = "c:\temp\logs\", $waitTime = 30)
function Write-LogFile
{
Param ([string]$logstatement)
$date = Get-Date -format yyyy-MM-dd
$timestamp = Get-Date -format "yyyy-MM-dd HH:mm:ss K"
$logFile = "$($logFilePath)$($date).log"
if (-not(Test-Path -Path $logFile -PathType Leaf)) {
try {
New-Item -ItemType File -Path $logFile -Force -ErrorAction SilentlyContinue
}
catch {
Write-Host $_.Exception.Message
}
}
Add-Content $logFile -value "$($timestamp)|$($logstatement)"
}
function Check-AppPoolStatus()
{
[xml]$status = C:\Windows\System32\inetsrv\appcmd list site "$($pubService)" /xml | C:\Windows\System32\inetsrv\appcmd list app /in /xml | C:\Windows\System32\inetsrv\appcmd list apppool /in /xml
$appPoolStatus = ([xml]$status).appcmd.APPPOOL.state
return $appPoolStatus
}
function Start-AppPool()
{
$status = Check-AppPoolStatus
Write-LogFile "App pool status $($status)"
if ($status -eq "Started")
{
Write-LogFile "Waiting for service to retart"
$status = C:\Windows\System32\inetsrv\appcmd list site "$($pubService)" /xml | C:\Windows\System32\inetsrv\appcmd list app /in /xml | C:\Windows\System32\inetsrv\appcmd list apppool /in /xml | C:\Windows\System32\inetsrv\appcmd recycle apppool /in
}
elseif ($status -eq "Stopped") {
Write-LogFile "Waiting for service To start"
C:\Windows\System32\inetsrv\appcmd list app /site.name:"$($pubService)" /xml | C:\Windows\System32\inetsrv\appcmd list apppool /in /xml | C:\Windows\System32\inetsrv\appcmd start apppool /in
}
$loop = 1
while ($loop)
{
$status = Check-AppPoolStatus
Write-Host $status
if ($status -eq "Started")
{
$loop = 0
}
elseif ($status -eq "Stopped")
{
C:\Windows\System32\inetsrv\appcmd list app /site.name:"$($pubService)" /xml | C:\Windows\System32\inetsrv\appcmd list apppool /in /xml | C:\Windows\System32\inetsrv\appcmd start apppool /in
}
start-sleep -seconds $waitTime
}
Write-LogFile "Service $($status)"
}
Write-LogFile "-------- Monitor-PublishingService Script Started --------"
while(1)
{
try {
if ($port -ne "") {
$url = "http://$($pubService):$($port)/api/publishing/operations/status/"
} else {
$url = "http://$($pubService)/api/publishing/operations/status/"
}
$request = [System.Net.WebRequest]::Create($url)
$request.Timeout=($waitTime * 1000)
$response = $request.GetResponse()
if ($response -ne $null -and $response.StatusCode.value__ -eq [System.Net.HttpStatusCode]::OK) {
$response.StatusCode # This is a System.Net.HttpStatusCode enum value
$response.StatusCode.value__ # This is the numeric version.
Write-LogFile "Status Code: $($response.StatusCode.value__)"
} else {
Start-AppPool
}
} catch {
Start-AppPool
}
start-sleep -seconds $waitTime
}
Comments
Post a Comment