A community member has associated this post with a similar question:
How to Fix Runbook Error

Only moderators can edit this content.

Azure Runbook Error

Will McLennan 0 Reputation points
2024-10-22T22:01:19.06+00:00

Hi there,

I have setup an automation app to stop and start a stream analytics job, following a guide from this doc https://learn.microsoft.com/en-us/azure/stream-analytics/automation-powershell

Over the past month, I have gotten this error twice. Both occuring when the runbook attempts to start up the stream.

"The SSL connection could not be established, see inner exception. (The SSL connection could not be established, see inner exception.)"

I am unsure as to why this is occuring, any help would be much appreciated? The runbook runs fine apart from these two errors.

Any help would be much appreciated, thanks.

User's image

Param(
    [string]$subscriptionId = "09bc1b84-e082-438e-9b61-5d1d59a9c061",
    [string]$resourceGroupName = "IoTVista",
    [string]$asaJobName = "iotvistastream-2",

    [int]$restartThresholdMinute = 180,
    [int]$stopThresholdMinute = 5,
    
    [int]$maxInputBacklog = 5,
    [int]$maxWatermark = 10
)

$ErrorActionPreference = 'stop'

$currentUTCtime = (Get-Date).ToUniversalTime()
Write-Host "asaRobotPause - Starting at: $currentUTCtime"
 
$resourceId = "/subscriptions/$($subscriptionId)/resourceGroups/$($resourceGroupName)/providers/Microsoft.StreamAnalytics/streamingjobs/$($asaJobName)"

Disable-AzContextAutosave -Scope Process | Out-Null

try {
    $AzureContext = (Connect-AzAccount -Identity).context
} catch {
    Write-Output "No system-assigned identity. Aborting."
    exit
}

$scheduledStartTimes = @('03:00', '06:00', '09:00', '12:00', '15:00', '18:00', '21:00', '00:00')

function IsCurrentTimeWithinStartWindow {
    param($currentTime, $times)
    $currentTime = [DateTime]::ParseExact($currentTime, 'HH:mm', $null)

    foreach ($time in $times) {
        $scheduledTime = [DateTime]::ParseExact($time, 'HH:mm', $null)
        $timeBefore = $scheduledTime.AddMinutes(-5)
        $timeAfter = $scheduledTime.AddMinutes(5)

        if ($currentTime -ge $timeBefore -and $currentTime -le $timeAfter) {
            return $true
        }
    }
    return $false
}

try {
    $currentJobState = Get-AzStreamAnalyticsJob -ResourceGroupName $resourceGroupName -Name $asaJobName | ForEach-Object {$_.JobState}
    Write-Output "asaRobotPause - Job $asaJobName is $currentJobState."

    if ($currentJobState -eq "Running") {
        $startTimeStamp = Get-AzActivityLog -ResourceId $resourceId -MaxRecord 1000 -WarningAction Ignore | Where-Object {$_.operationName -like "Start Job*"} | Select-Object -First 1 | ForEach-Object {$_.EventTimeStamp}

        $currentBacklog = Get-AzMetric -ResourceId $resourceId -TimeGrain 00:01:00 -MetricName "InputEventsSourcesBacklogged" -DetailedOutput -WarningAction Ignore
        $currentWatermark = Get-AzMetric -ResourceId $resourceId -TimeGrain 00:01:00 -MetricName "OutputWatermarkDelaySeconds" -DetailedOutput -WarningAction Ignore

        $Backlog = $currentBacklog.Data | Where-Object {$_.Maximum -ge 0} | Sort-Object -Property Timestamp -Descending | Where-Object {$_.Timestamp -ge $startTimeStamp} | Select-Object -First $stopThresholdMinute | Measure-Object -Sum Maximum
        $BacklogSum = $Backlog.Sum

        $Watermark = $currentWatermark.Data | Where-Object {$_.Maximum -ge 0} | Sort-Object -Property Timestamp -Descending | Where-Object {$_.Timestamp -ge $startTimeStamp} | Select-Object -First $stopThresholdMinute | Measure-Object -Average Maximum
        $WatermarkAvg = [int]$Watermark.Average

        Write-Output "asaRobotPause - Running since $startTimeStamp, with $BacklogSum backlogged events, and avg watermark of $WatermarkAvg sec."

        if ($BacklogSum -le $maxInputBacklog -and $WatermarkAvg -le $maxWatermark -and $Watermark.Count -ge $stopThresholdMinute) {
            Write-Output "asaRobotPause - Stopping job..."
            Stop-AzStreamAnalyticsJob -ResourceGroupName $resourceGroupName -Name $asaJobName
        } else {
            Write-Output "asaRobotPause - Job is not stopping yet. Needs less backlog and lower watermark."
        }
    } elseif ($currentJobState -eq "Stopped") {
        $stopTimeStamp = Get-AzActivityLog -ResourceId $resourceId -MaxRecord 1000 -WarningAction Ignore | Where-Object {$_.operationName -like "Stop Job*"} | Select-Object -First 1 | ForEach-Object {$_.EventTimeStamp}

        $minutesSinceStopped = ((Get-Date).ToUniversalTime() - $stopTimeStamp).TotalMinutes
        $utcTime = (Get-Date).ToUniversalTime()
        $aestTimeZoneId = "E. Australia Standard Time"
        $aestTimeZone = [TimeZoneInfo]::FindSystemTimeZoneById($aestTimeZoneId)
        $aestTime = [TimeZoneInfo]::ConvertTimeFromUtc($utcTime, $aestTimeZone)
        $currentTime = $aestTime.ToString("HH:mm")
        Write-Output "$currentTime"

        if (IsCurrentTimeWithinStartWindow -currentTime $currentTime -times $scheduledStartTimes) {
            Write-Output "asaRobotPause - Scheduled to start now."
            Start-AzStreamAnalyticsJob -ResourceGroupName $resourceGroupName -Name $asaJobName -OutputStartMode LastOutputEventTime
        } else {
            Write-Output "asaRobotPause - Not scheduled to start."
        }
    } else {
        Write-Output "asaRobotPause - Job state $currentJobState is not manageable."
    }

    $newJobState = Get-AzStreamAnalyticsJob -ResourceGroupName $resourceGroupName -Name $asaJobName | ForEach-Object {$_.JobState}
    Write-Output "asaRobotPause - Job was $currentJobState, is now $newJobState. Done."
} catch {
    throw $_.Exception.Message
}

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
39,656 questions
{count} votes