WakeUpAzureWebApp.psm1
. "$PSScriptRoot/Private/Constants.ps1"; . "$PSScriptRoot/Private/GetUrls.ps1"; . "$PSScriptRoot/Private/GetInstanceGuidsForWebApp.ps1"; . "$PSScriptRoot/Private/MapUrlsWithHostName.ps1"; function Start-WakeUpAzureWebApp{ <# .SYNOPSIS Wake up Azure WebApp for the current subsribtion with name $TargetWebAppName .DESCRIPTION Use a list of $WarmupUrls to warm up the specified Azure WebApp for the current subsribtion with name $TargetWebAppName Targeting all instances one by one to make sure every instance is warmed up .PARAMETER TargetWebAppName The actual name of the Azure WebApp .PARAMETER WarmupUrls A list of string containing relative urls .PARAMETER RequestTimeOut This parameter requires System.TimeSpan. it defines the maximum amount of time allowed for every web request to respond within. Will terminate script on timeout. (Default 20 Seconds) .PARAMETER NoResponseRetries This parameter requires a positive integer larger than 0, it defines the amount of times to retry on a non-status code response or timeout. (Default 3) .PARAMETER ConsecutiveErrorThreshold This parameter requires a positive integer larger than 0, it defines the amount of request errors that are allowed to appear in series before terminating. (Default 10) .PARAMETER TotalErrorThreshold This parameter requires a positive integer larger than 0 but is optional, it defines the amount of request errors that are allowed to appear in total before terminating. (Default no maximum) .OUTPUTS Some details about the progress and the response code of the urls .NOTES This module is designed for use in Azure context, so Start-WakeUpAzureWebApp needs to be called before executing the sript. This is designed with Octopus deploy in mind. .LINK https://gitlab.com/Valtech-Amsterdam/PowerShell/WakeUpAzureWebApp .EXAMPLE Import-Module WakeUpAzureWebApp; $targetWebAppName = #"{WebAppName}"; $warmupUrls = @( "/nl-nl" "/nl-nl/some url/" "/nl-nl/another url/" ); Start-WakeUpAzureWebApp ` -targetWebAppName $targetWebAppName ` -warmupUrls $warmupUrls; #> [CmdletBinding()] Param( [Parameter(Mandatory=$True)] [string]$TargetWebAppName, [Parameter(Mandatory=$True)] [string[]]$WarmupUrls, [Parameter(Mandatory=$False)] [System.TimeSpan]$RequestTimeOut = $Constants.Defaults.RequestTimeOut, [Parameter(Mandatory=$False)] [Int16]$NoResponseRetries = $Constants.Defaults.NoResponseRetries, [Parameter(Mandatory=$False)] [int16]$ConsecutiveErrorThreshold = $Constants.Defaults.ConsecutiveErrorThreshold, [Parameter(Mandatory=$False)] [AllowNull()] [Nullable[int16]]$TotalErrorThreshold = $null ) BEGIN { $scriptStart = [System.DateTime]::UtcNow; if ([string]::IsNullOrEmpty($(Get-AzureRmContext).Account)) { Write-Host "Please login using Login-AzureRMAccount" -ForegroundColor Red; exit 1; } try { $webApp = Get-AzureRmWebApp -Name $TargetWebAppName -ErrorAction Stop; $defaultHostName = $webApp.DefaultHostName; $instanceGuids = GetInstanceGuidsForWebApp -webApp $webApp; $domainUrls = MapUrlsWithHostName -defaultHostName $defaultHostName -urls $WarmupUrls; $instanceCount = @($instanceGuids).length; } catch { Write-Host "A critical error occured, reason: $_" -ForegroundColor Red exit 1; } } PROCESS { Write-Host "Getting urls for $instanceCount instances" for($i = 0; $i -lt $instanceCount; $i++){ $instanceGuid = @($instanceGuids)[$i]; $index = $i + 1; Write-Host "Warming up instance $instanceGuid ($index / $instanceCount)"; Write-Progress -Id 91 -Activity "Getting urls for $instanceCount instances" ` -status "Warming up instance $instanceGuid $index / $instanceCount)" ` -percentComplete (($i / $instanceCount) * 100); try { GetUrls -defaultHostName $defaultHostName -urls $domainUrls -instanceGuid $instanceGuid ` -requestTimeOut $RequestTimeOut ` -consecutiveErrorThreshold $ConsecutiveErrorThreshold ` -totalErrorThreshold $TotalErrorThreshold; } catch { Write-Host "A critical error occured, reason: $_" -ForegroundColor Red exit 1; } } } End { $scriptTimeSpan = New-TimeSpan -Start $scriptStart -End ([System.DateTime]::UtcNow); $timeString = $scriptTimeSpan.ToString($Constants.TimeSpanFormat); Write-Host "Completed warming up $instanceCount instances, T: $timeString"; Write-Progress -Id 91 -Activity "Getting urls for $instanceCount instances" ` -status "Completed warming up $instanceCount instances, T: $timeString" ` -percentComplete 100 ` -Completed; } } Export-ModuleMember -Function Start-WakeUpAzureWebApp |