How to swap an Azure consumption function app using Warmup_Path and Ping_Path set to a trigger.

0

Are you seeing an error “Swap failed. Details: Cannot swap slots for site ‘<Sitename>’ because the application initialization in ‘QA’ slot either took too long or failed. Please check AppInit module configuration or try using swap with preview if application initialization time is very long.” on a linux Function app on a Consumption plan while using the “WEBSITE_SWAP_WARMUP_PING_PATH” set to a trigger URL and “WEBSITE_SWAP_WARMUP_PING_STATUSES” set??

First Attempt

This post is short and sweet, but if you add your own variable to the top of the script and paste it into azure CLI, it should allow you to bypass this problem. The downside of this attempt is that it restarts the production function app and not the QA slot.

appName="<App-name>"
resourceGroupName="<Resource-group-name>"
slotName="<Slot-name>"
placeholderVariableName="<Variable-name>"
placeholderValue="1"
 
# Setting app setting on a function app
az functionapp config appsettings set --name $appName --resource-group $resourceGroupName --settings $placeholderVariableName=$placeholderValue && \
 
echo "Waiting 30 seconds..." && \
sleep 30 && \
 
echo "Starting Swap…"
 
# Swap slots in a loop until it's successful
while true; do
  if az functionapp deployment slot swap --name $appName --resource-group $resourceGroupName --slot $slotName --target-slot production; then
    echo "Swap completed successfully"
    break
  else
    echo "Swap failed, setting Variable and retrying in 30 seconds…"
    placeholderValue=$((placeholderValue+1))
    az functionapp config appsettings set --name $appName --resource-group $resourceGroupName --settings $placeholderVariableName=$placeholderValue && \
    echo "Waiting 30 seconds…" && \
    sleep 30 && \
    echo "Restarting Swap…"
  fi
done

Less Impactful Method

I later found that running the following script in CLI seemed work work on the first try 75% of the time and on 40 attempts it succeeded by the 2nd try 100% of the time. This script simply attempts to swap, and on failure it waits 10 seconds and tries again. The swap ping seems to be enough to get the service running.

appName="<App-name>"
resourceGroupName="<Resource-group-name>"
slotName="<Slot-name>"

# Swap slots in a loop until it's successful
while true; do
  if az functionapp deployment slot swap --name $appName --resource-group $resourceGroupName --slot $slotName --target-slot production; then
    echo "Swap completed successfully"
    break
  else
    sleep 10 && \
    echo "Restarting Swap…"
  fi
done

About Author

Leave a Reply

Your email address will not be published. Required fields are marked *