SharePoint Tidbit - Making Graceful shutdown actually work in SharePoint 2013
Hello All,
We have discussed Distributed Cache in SharePoint 2013, and I believe we discussed Graceful shutdown….oh boy have things changed. It was discovered that running the command was actually not persisting the data as the service was stopped before data was moved to another server.
Luckily Distributed Cache was built on App Fabric and it had the controls available for us to work around this issue. Here is a sample piece of code that will move the data to another server then shutdown that service in SharePoint. This needs to run on a SharePoint server and I would suggest either running it before shutting down a server, or having it run as a managed task in Windows on shutdown.
# This Sample Code is provided for the purpose of illustration only and is not intended to be used in a production environment.
# THIS SAMPLE CODE AND ANY RELATED INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
# TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS for A PARTICULAR PURPOSE. We grant You a nonexclusive, royalty-free right to use and modify
# the Sample Code and to reproduce and distribute the object code form of the Sample Code, provided that You agree: (i) to not use Our name, logo, or
# trademarks to market Your software product in which the Sample Code is embedded; (ii) to include a valid copyright notice on Your software product in
# which the Sample Code is embedded; and (iii) to indemnify, hold harmless, and defend Us and Our suppliers from and against any claims or lawsuits,
# including attorneys fees, that arise or result from the use or distribution of the Sample Code.
$startTime = Get-Date
$currentTime = $startTime
$elapsedTime = $currentTime - $startTime
[int]$timeOut = 900
[string]$SharePointServerName = "SP2013app.contoso.com"
[int]$Count = 1
$Error.Clear()
try
{
Use-CacheCluster
$HostInfo = Stop-CacheHost -Graceful -CachePort 22233 -ComputerName $SharePointServerName
while($elapsedTime.TotalSeconds -le $timeOut-and $HostInfo.Status -ne 'Down')
{
$Status = $hostInfo.Status
Write-Progress -Activity "Shutting down distributed cache host..." -CurrentOperation $Status -PercentComplete $Count
Start-Sleep(3)
$currentTime = Get-Date
$elapsedTime = $currentTime - $startTime
$Count = $timeOut/(($elapsedTime.Minute * 60) + $elapsedTime.Seconds)
$hostInfo = Get-CacheHost -HostName $SharePointServerName -CachePort 22233
}
Add-PSSnapin Microsoft.SharePoint.PowerShell -ea SilentlyContinue
Write-Progress -Activity "Shutting down distributed cache host..." -CurrentOperation "Service fully Stopped" -PercentComplete 100
Stop-SPDistributedCacheServiceInstance
Write-Host "To start service, please use Central Administration site."
}
catch [System.Exception]
{
Write-Host "[ERROR]Failed stopping cache host. See full error below. `n"
Write-Host $Error
}