【问题标题】:Bash Retry - Azure CLIBash 重试 - Azure CLI
【发布时间】:2021-02-24 09:32:26
【问题描述】:

我在美国西部有一个 Azure 存储帐户,启用了异地复制以与美国东部同步,我想根据需要使用 bash 脚本执行故障转移。

我已经定义了以下函数

_STORAGE_ACCOUNT_FAILOVER () {
    echo "Storage account failover is initiated.."

    az storage account failover --name $STORAGEACCOUNT --no-wait --yes

    echo "Storage account failover is completed successfully.."
}

在尝试执行上述功能时,我收到以下错误

ERROR: (ResourceCollectionRequestsThrottled) Operation 'Microsoft.Storage/storageAccounts/read' failed as server encountered too many requests. 
Please try after '17' seconds. Tracking Id is ''.

如果出现任何问题,我想实现重试逻辑?如何实现重试逻辑?

类似

    _STORAGE_ACCOUNT_FAILOVER () {
        echo "Storage account failover is initiated.."

performFailover:
        az storage account failover --name $STORAGEACCOUNT --no-wait --yes

if [ "$?" -ne 0 ]; then
    goto performFailover;
fi
    
        echo "Storage account failover is completed successfully.."
    }

或类似的东西

_STORAGE_ACCOUNT_FAILOVER () {
    echo "Storage account failover is initiated.."

    while true; do
      az storage account failover --name $STORAGEACCOUNT --no-wait --yes

      if [ "$?" -eq 0 ]; then
        break;
      fi

      sleep 30s
    done

    echo "Storage account failover is completed successfully.."
}

【问题讨论】:

标签: bash azure azure-storage-account


【解决方案1】:

人类可读的错误信息有些问题;你能以机器可读的形式获得请求的重试等待吗?

这是一个速写,假设az 在超时时设置了一个非零退出代码。 (如果你幸运的话,它会有一个针对这个特定错误的唯一退出代码。)

_STORAGE_ACCOUNT_FAILOVER () {
    echo "$0: Storage account failover initiated" >&2
    while true; do
        if result=$(az storage account failover \
            --name "$STORAGEACCOUNT" \
            --no-wait --yes 2>&1 >/dev/null)
        then
            break
        else
            rc=$?
            case $result in
             *"Please try after '"*)
                seconds=${result#*Please try after \'}
                seconds=${seconds%%\'*}
                wait "$seconds"
                ;;
             *) echo "$0: $result" >&2
                exit $rc
                ;;
            esac
        fi
    done
    echo "$0: Storage account failover completed successfully" >&2
}

注意我们如何从az 命令捕获标准错误并解析出超时消息,或者如果我们不能返回错误。还要注意所有诊断消息如何打印为标准错误,并以电报格式进行格式化,但始终在消息中使用脚本的名称,这样如果您有脚本调用脚本调用脚本等,您就可以知道哪个脚本失败了。

【讨论】:

    猜你喜欢
    • 2017-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-22
    • 2021-07-12
    • 1970-01-01
    • 1970-01-01
    • 2023-01-20
    相关资源
    最近更新 更多