【问题标题】:Azure Automation Powershell - The runbook job was attempted 3 times, but it suspendedAzure 自动化 Powershell - Runbook 作业已尝试 3 次,但已暂停
【发布时间】:2022-08-21 00:48:05
【问题描述】:

我在 powershell 中创建了一个 azure 自动化帐户,以获取文件的名称和某些容器中的大小。

我正在通过带有 Webhook 活动的 Azure 数据工厂运行该代码。

该代码对于中小型容器运行得非常好。

问题是,当我尝试为已经有大量文件的某个容器运行代码时,它尝试了 3 次并被挂起,但没有任何反应。我看到了日志,看到了这条消息:

由于沙盒内存不足,Runbook 作业失败。每个 Azure 自动化沙盒都分配有 400 MB 的内存。该作业在暂停之前尝试了 3 次。请参阅https://aka.ms/AAMemoryLimit 解决此问题的一些常用方法

有谁知道如何解决这种情况或者是否可以增加内存?谢谢 !

PS代码:

#define parameters
param (
    [Parameter (Mandatory = $false)]
    [object] $WebHookData,
    [string]$StorageAccountName,
    [string]$StorageAccountKey
)


$Parameters = (ConvertFrom-Json -InputObject $WebHookData.RequestBody)

<#If ($Parameters.callBackUri)
{
    $callBackUri = $Parameters.callBackUri
}#>


$containerName = $Parameters.containerName
\"->\"+$StorageAccountName
\"->\"+$StorageAccountKey


$connectionName = \"AzureRunAsConnection\"
try
{
    # Get the connection \"AzureRunAsConnection \"
    $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName       

    \"Logging in to Azure...\"
    Connect-AzAccount `
        -ServicePrincipal `
        -TenantId $servicePrincipalConnection.TenantId `
        -ApplicationId $servicePrincipalConnection.ApplicationId `
        -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 
}
catch {
    if (!$servicePrincipalConnection)
    {
        $ErrorMessage = \"Connection $connectionName not found.\" 
        throw $ErrorMessage 
    } else{
        Write-Error -Message $_.Exception 
        throw $_.Exception 
    }
}


#storage account
$StorageAccountName = $StorageAccountName

#storage key
$StorageAccountKey = $StorageAccountKey

#Container name - change if different
$containerName = $containerName

#get blob context
$Ctx = New-AzStorageContext $StorageAccountName -StorageAccountKey $StorageAccountKey 

# get a list of all of the blobs in the container 
$listOfBlobs = Get-AzStorageBlob -Container $containerName -Context $Ctx 

# zero out our total
$length = 0 

# this loops through the list of blobs and retrieves the length for each blob
#   and adds it to the total
$listOfBlobs | ForEach-Object {$length = $length + $_.Length} 

# output the blobs and their sizes and the total 
Write-Host \"List of Blobs and their size (length)\"
Write-Host \" \" 
$select = $listOfBlobs | Select-Object -Property @{Name=\'ContainerName\';Expression={$containerName}}, Name, @{name=\"Size\";expression={$($_.Length)}}, LastModified 
#$listOfBlobs | select Name, Length, @{Name=\'ContainerName\';Expression={$containerName}}
Write-Host \" \"
Write-Host \"Total Length = \" $length

#Define location and Export to CSV file
$SourceLocation = Get-Location 

$select | Export-Csv $SourceLocation\'File-size/File-size-\'$containerName\'.csv\' -NoTypeInformation -Force -Encoding UTF8 

$Context = New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey

Set-AzureStorageBlobContent -Context $Context -Container \"Name\" -File $SourceLocation\"File-size/File-size-$containerName.csv\" -Blob \"File-Size/File-size-$containerName.csv\" -Force

    标签: azure powershell azure-devops azure-automation


    【解决方案1】:

    我也遇到过同样的情况...

    您无法增加运行时的内存。您可以下载一个混合工作代理(或者它现在已重命名),以使 azure 自动化帐户在您的本地计算机或您设置的服务器上运行您的脚本。

    你的内存问题是由$listOfBlobs = Get-AzStorageBlob ...引起的

    不能一次全部拉取,必须使用分页(或者换句话说,分批列出)

    $MaxReturn = 10000
    $ContainerName = "abc"
    $Total = 0
    $Token = $Null
    do
     {
         $Blobs = Get-AzStorageBlob -Container $ContainerName -MaxCount $MaxReturn  -ContinuationToken $Token
         $Total += $Blobs.Count
         if($Blobs.Length -le 0) { Break;}
         $Token = $Blobs[$blobs.Count -1].ContinuationToken;
     }
     While ($null -ne $Token)
    
    Echo "Total $Total blobs in container $ContainerName"
    

    来源 -> https://docs.microsoft.com/en-us/powershell/module/az.storage/get-AzStorageblob?view=azps-8.2.0#example-4-list-blobs-in-multiple-batches

    【讨论】:

    • 非常感谢您的回复!抱歉,我对 powershell 很陌生,我试图实现该逻辑,但到目前为止我还不能。给出我的场景,我该如何结合这个逻辑?我试图将逻辑嵌入到“做”中,但我做不到
    • 您不需要合并所有逻辑,只需包含分配变量 '$select' 的部分,例如 '$select = $listOfBlobs |选择对象......'。您在 do-loop 之前将 $select 设置为数组,在循环内添加 blob 列表的已处理部分,如 '$select += select-object...' 并在循环之后执行 export-csv 调用和其他东西
    • 再次感谢 D.J 的理解和关注。我会根据您的提示进行尝试,然后提供反馈!
    • DJ我试过这个 $select = @() $MaxReturn = 10000 $Token = $Null do { $listOfBlobs = Get-AzStorageBlob -Container $ContainerName -Context $Ctx -MaxCount $MaxReturn -ContinuationToken $Token $listOfBlobs | ForEach-Object {$length = $length + $_.Length} $select += $listOfBlobs | Select-Object -Property @{Name='ContainerName';Expression={$containerName}}, 名称, @{name="Size";expression={$($_.Length)}}, LastModified $Token = $listOfBlobs [$listOfBlobs.Count -1].ContinuationToken;虽然 ($null -ne $Token) 但它一直在尝试运行但不幸的是它被暂停了
    • 为 $MaxReturn 尝试较低的值,您也可以在循环结束时使用“clear-variable”显式清除变量 $list Of Blob,以强制 shell 清除一些内存
    【解决方案2】:

    默认情况下,Runbook 在 Azure 沙盒上执行,这些沙盒是 MS 托管的机器,并且每个帐户/执行都实施了一定的资源配额作为合理使用策略。

    对于资源密集型和长时间运行的作业,始终建议使用 Hybrid Worker 执行模式。

    在这种模式下,您可以将机器添加为混合工作节点(可以是本地或云虚拟机),它们有足够的资源来执行您的工作负载。

    您可以在此处参考my blog 以了解有关如何配置混合工作节点和本地/云机器以执行工作负载的更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-11
      • 2020-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多