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. Defaults to 60 seconds
            $timeOut = New-TimeSpan -Seconds 60;
            A list of string containing relative urls
        .PARAMETER ConsecutiveErrorThreshold
            This parameter requires a positive integer larger than 0,
            it defines the amount of 5xx status codes are allowed to appear in series before terminating.
            It defaults to 10
        .PARAMETER TotalErrorThreshold
            This parameter requires a positive integer larger than 0 but is optional,
            it defines the amount of 5xx status codes are allowed to appear in total before terminating
            It defaults to not having a 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]$ConsecutiveErrorThreshold = $Constants.Defaults.ConsecutiveErrorThreshold,
        [Parameter(Mandatory=$False)] [AllowNull()]
        [Nullable[int16]]$TotalErrorThreshold = $null
    )

    BEGIN {
        $scriptStart = [System.DateTime]::UtcNow;

        try{
            $webApp = Get-AzureRmWebApp -Name $TargetWebAppName;

            $defaultHostName = $webApp.DefaultHostName;
            $instanceGuids = GetInstanceGuidsForWebApp -webApp $webApp;
            $domainUrls = MapUrlsWithHostName -defaultHostName $defaultHostName -urls $WarmupUrls;
        } catch {
            Write-Host "Something went wrong contacting Azure, are you sure you are logged in? (Login-AzureRMAccount)"
            exit 1;
        }
        $instanceCount = @($instanceGuids).length;

    }

    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);

            GetUrls -defaultHostName $defaultHostName -urls $domainUrls -instanceGuid $instanceGuid `
                -requestTimeOut $RequestTimeOut `
                -consecutiveErrorThreshold $ConsecutiveErrorThreshold `
                -totalErrorThreshold $TotalErrorThreshold;
        }

    }

    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