我正在使用自定义 Docker 映像运行 Azure 函数 (Python) 应用程序。我使用管道部署此映像,包括以下步骤
az functionapp config container set --docker-image-custom-name <image>
(https://learn.microsoft.com/en-us/cli/azure/functionapp/config/container?view=azure-cli-latest#az- functionapp-config-container-set)当我更改配置时,更改需要一些时间才能生效,即在新映像实际运行之前。我注意到,因此集成测试有时会在旧映像上运行。
我想知道是否有办法“等待”更改生效。
我尝试使用
az functionapp config container show
,但配置更改立即生效。
我当然可以等待几秒钟,但由于显而易见的原因,这并不理想。
其他人如何处理这个问题?
我创建了一个 powershell 脚本来帮助解决使用部署槽时的这种情况。
该脚本的关键是它在计时器上查询函数的当前状态并等待所需的状态。
它执行以下操作:
我相信类似的东西可以用于您的目的。
#Assuming your slots are named production and staging
param ($resourceGroupName, $functionAppName)
Write-Output "${resourceGroupName}"
Write-Output "${functionAppName}"
#Function Wait Function App State
#Used to wait for the function to either be running or stopped
function Wait-FunctionAppState {
param (
[string]$ResourceGroupName,
[string]$FunctionAppName,
[string]$SlotName = "production",
[string]$DesiredState,
[int]$TimeoutInSeconds = 60,
[int]$PollingIntervalInSeconds = 10
)
$elapsedTime = 0
while ($elapsedTime -lt $TimeoutInSeconds) {
$currentState = az functionapp show `
--resource-group $ResourceGroupName `
--name $FunctionAppName `
--slot $SlotName `
--query "state" -o tsv
Write-Output "Current state of slot '$SlotName': $currentState"
if ($currentState -eq $DesiredState) {
Write-Output "Slot '$SlotName' has reached the desired state '$DesiredState'."
return $true
}
Start-Sleep -Seconds $PollingIntervalInSeconds
$elapsedTime += $PollingIntervalInSeconds
}
Write-Error "Timeout: Slot '$SlotName' did not reach the state '$DesiredState' within $TimeoutInSeconds seconds."
return $false
}
# Start the staging slot
az functionapp start --resource-group "${resourceGroupName}" --name "${functionAppName}" --slot "staging"
# Wait until the staging slot is running
Wait-FunctionAppState -ResourceGroupName "${resourceGroupName}" `
-FunctionAppName "${functionAppName}" `
-SlotName "staging" `
-DesiredState "Running"
# Swap the slots
az functionapp deployment slot swap --resource-group "${resourceGroupName}" --name "${functionAppName}" --slot "staging" --target-slot "production"
# Stop the staging slot
az functionapp stop --resource-group "${resourceGroupName}" --name "${functionAppName}" --slot "staging"
# Wait until the staging slot is stopped
Wait-FunctionAppState -ResourceGroupName "${resourceGroupName}" `
-FunctionAppName "${functionAppName}" `
-SlotName "staging" `
-DesiredState "Stopped"
Write-Output "Function App Slots Swapped"
Exit 0