Private/GetUrls.ps1

. "$PSScriptRoot/Constants.ps1";
. "$PSScriptRoot/GetSingleUrl.ps1";

# Make a get request to all $urls while forcing the instance using the ARRAfinity cookie with $instanceGuid
function GetUrls(
    [Parameter(Mandatory=$True)]
    [string]$defaultHostName,
    [Parameter(Mandatory=$True)]
    [string[]]$urls,
    [Parameter(Mandatory=$True)]
    [System.TimeSpan]$requestTimeOut,
    [Parameter(Mandatory=$True)]
    [int16]$ConsecutiveErrorThreshold,
    [Parameter(Mandatory=$True)] [AllowNull()]
    [Nullable[int16]]$TotalErrorThreshold,
    [Parameter(Mandatory=$False)]
    [string]$instanceGuid) {

    if ($ConsecutiveErrorThreshold -le 0){
        throw "ConsecutiveErrorThreshold needs to be a positive integer larger than 0";
    }
    if ($TotalErrorThreshold -ne $null -and $TotalErrorThreshold -le 0){
        throw "TotalErrorThreshold needs to be a positive integer larger than 0 or null";
    }

    $urlCount = @($urls).length;
    $consecutiveErrorCount = 0;
    $totalErrorCount = 0;
    Write-Host "Getting $urlCount urls for instance $instanceGuid";
    for($i = 0; $i -lt $urlCount; $i++){
        $url = $urls[$i];
        $index = $i + 1;
        $requestStart = [System.DateTime]::UtcNow;

        try {
            Write-Progress -Id 99 -ParentId 91 -Activity ("Getting $urlCount urls for instance $instanceGuid") `
                -status "Processing $index / $urlCount urls" `
                -percentComplete (($i / $urlCount) * 100);

            $httpResponse = GetSingleUrl -defaultHostName $defaultHostName -url $url -instanceGuid $instanceGuid `
                -requestTimeOut $requestTimeOut;
            $responseMessage = $httpResponse.StatusDescription;
            $responseCode = $httpResponse.StatusCode;
            Write-Host "$responseMessage ($responseCode)" -foregroundcolor "Green" -NoNewline;
            $requestTimeSpan = New-TimeSpan -Start $requestStart -End ([System.DateTime]::UtcNow);
            $timeString = $requestTimeSpan.ToString($Constants.TimeSpanFormat);
            Write-Host " [T: $timeString]";
            $consecutiveErrorCount = 0;
        }
        catch [System.Net.WebException] {
            $responseMessage = $_.Exception.Response.StatusDescription;
            $responseCode = $_.Exception.Response.StatusCode.Value__;

            $errorMessage = "$_";
            foreach($message in $Constants.FilteredErrorMessages){
                if ($responseMessage.StartsWith($message)) { throw; }
                if ($errorMessage.StartsWith($message)) { throw; }
            }

            Write-Host $_;
            $foregroundColor = "Magenta";

            # Red if 5xx status code
            if([math]::Round($responseCode/100) % 5 -eq 0) {
                $foregroundColor = "Red"
                $consecutiveErrorCount++;
                $totalErrorCount++;

                if($consecutiveErrorCount -ge $ConsecutiveErrorThreshold) {
                    throw "The consecutive 5xx error threshold of $ConsecutiveErrorThreshold has been reached, terminating script"
                }
                if($TotalErrorThreshold -ne $null -and $totalErrorCount -ge $TotalErrorThreshold) {
                    throw "The total 5xx error threshold of $TotalErrorThreshold has been reached, terminating script"
                }
            } else{
                $consecutiveErrorCount = 0;
            }

            Write-Host "$responseMessage ($responseCode)" -foregroundcolor $foregroundColor -NoNewline;
            $requestTimeSpan = New-TimeSpan -Start $requestStart -End ([System.DateTime]::UtcNow);
            $timeString = $requestTimeSpan.ToString($Constants.TimeSpanFormat);
            Write-Host " [T: $timeString]";
        }
        catch { throw $_; }
    }

    Write-Progress -Id 99 -ParentId 91 -Activity ("Getting $urlCount urls for instance $instanceGuid") `
        -status "Completed $urlCount urls" `
        -percentComplete 100 `
        -Completed;
}