ps/Modules/Alkami.PowerShell.ServiceFabric/Public/Restart-AlkamiServiceFabricApplication.ps1

115 lines
4.8 KiB
PowerShell
Raw Normal View History

2023-05-30 22:51:22 -07:00
function Restart-AlkamiServiceFabricApplication {
<#
.SYNOPSIS
Restarts a microservice by chocolatey package name.
.PARAMETER name
The chocolatey/nuget name of the microservice to be restarted.
.PARAMETER hostname
A single server hostname from the target Service Fabric cluster.
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true, ParameterSetName="Name")]
[string]$name,
[Parameter(Mandatory = $true, ParameterSetName="ApplicationTypeName")]
[string]$applicationTypeName,
[Parameter(Mandatory = $false, ParameterSetName="Name")]
[Parameter(Mandatory = $false, ParameterSetName="ApplicationTypeName")]
[string]$hostname = "localhost"
)
$loglead = (Get-LogLeadName)
# Connect to the SF cluster.
Connect-AlkamiServiceFabricCluster -hostname $hostname | Out-Null
$applications = $null
if(!([string]::IsNullOrWhiteSpace($name))) {
$applications = (Get-AlkamiServiceFabricApplications -Name $name -ComputerName $hostname)
} elseif(!([string]::IsNullOrWhiteSpace($applicationTypeName))) {
$applications = ((Get-AlkamiServiceFabricApplications -ComputerName $hostname) | Where-Object { $_.ServiceFabricApplicationTypeName -eq $applicationTypeName } )
}
if(Test-IsCollectionNullOrEmpty($applications)) {
Write-Error "$loglead : Could not find an application named $name to restart. Are you connected to a Seed Node?"
return
}
Write-Verbose "$loglead : Found $($applications.count) Application(s) to Restart:"
foreach($application in $applications) {
Write-Verbose $application.ServiceFabricApplicationName
}
$nodes = Get-ServiceFabricNode
$hostnameNode = $nodes | Where-Object { ($_.NodeName -like $hostname) -or ($_.IpAddressOrFQDN -like $hostname) } | Select-Object -First 1
if($null -eq $hostnameNode) {
Write-Error "$loglead : Unable to determine an environment/NodeType associated with server $hostname"
return
}
$nodeType = $hostnameNode.NodeType
# Filter the nodes down to the servers in the specific environment that are up.
$environmentNodes = $nodes | Where-Object { ($_.NodeType -eq $nodeType) -and ($_.NodeStatus -eq "Up") }
# Get the node-names.
$nodeNames = $environmentNodes | Select-Object -ExpandProperty "NodeName"
# Define script block to restart an individual application.
$restartApplicationScriptBlock = {
param($sbApplication, $sbNodeName)
# Get applications on the node, and filter the applications to those that are active.
$applicationsOnNode = Get-ServiceFabricDeployedApplication -NodeName $sbNodeName -ApplicationName $sbApplication.ServiceFabricApplicationName
$applicationsOnNode = $applicationsOnNode.Where({
$_.DeployedApplicationStatus -eq "Active"
})
if(Test-IsCollectionNullOrEmpty $applicationsOnNode) {
return
}
# In the hierarchy of Alkami-Style SF Microservices, all Application/Service services are named "svc"
$serviceName = "svc"
foreach($nodeApp in $applicationsOnNode) {
# Get the code package and restart it.
Write-Verbose "$loglead : Restarting app $($nodeApp.ApplicationName) on $sbNodeName"
$codePackageParams = @{
NodeName = $sbNodeName
ApplicationName = $nodeApp.ApplicationName
ServiceManifestName = $serviceName
}
# Get the code package for the specific node
$codePackage = Get-ServiceFabricDeployedCodePackage @codePackageParams
if($null -eq $codePackage) {
return
}
# Restart the service!
$restartParameters = @{
NodeName = $sbNodeName
ApplicationName = $nodeApp.ApplicationName
ServiceManifestName = $serviceName
CodePackageName = $codePackage.CodePackageName
CodePackageInstanceId = $codePackage.EntryPoint.CodePackageInstanceId
ServicePackageActivationId = $codePackage.ServicePackageActivationId
CommandCompletionMode = "Verify"
}
Restart-ServiceFabricDeployedCodePackage @restartParameters | Out-Null
}
}
# For each application we are restarting
foreach($application in $applications) {
Write-Host "$loglead : Restarting Application $($application.ServiceFabricApplicationName)"
# For each node, restart that application if it exists on the node.
foreach($nodeName in $nodeNames) {
Invoke-CommandWithRetry -Arguments ($application, $nodeName) -MaxRetries 5 -Exponential -Verbose -ScriptBlock $restartApplicationScriptBlock
}
}
Write-Host "$loglead : Rolling restarts are complete."
}