skip to Main Content

I have an App Service which is running on multiple instances and also has a CI/CD set, so WebJobs isn’t really an option here. I’ve found an elegant solution with a Logic App and Azure API but I was only able to schedule a restart of the whole App Service. This works fine, but it will cause some downtime. I would like to restart instances one by one using the same Logic App + API approach to decrease the downtime.

2

Answers


  1. Chosen as BEST ANSWER

    I was able to set the Logic App to iterate through all App Service instances and reboot workers in the App Service Plan one by one, using a suggestion from @VenkateshDodda

    1. Create an App Registration and obtain a Client Secret.

    2. Grant permissions for user_impersonation in Azure Service Management -> API permissions inside your App Registration

    3. Grant permissions for your App Registration in the App Service Plan and App Service under Access Control.

    4. Paste the attached complete code into your Logic App.

    5. Modify it with your client_secret, tenant_id, subscription_id, resourceGroup, appServicePlan, appServiceName, etc:

       {
       "definition": {
           "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
           "actions": {
               "For_each_instance_Reboot_and_Wait": {
                   "actions": {
                       "Delay": {
                           "inputs": {
                               "interval": {
                                   "count": 1,
                                   "unit": "Hour"
                               }
                           },
                           "runAfter": {
                               "HTTP": [
                                   "Succeeded",
                                   "TimedOut",
                                   "Skipped"
                               ]
                           },
                           "type": "Wait"
                       },
                       "HTTP": {
                           "inputs": {
                               "authentication": {
                                   "type": "Raw",
                                   "value": "Bearer @{body('Parse_OAuth_Token')?['access_token']}"
                               },
                               "headers": {
                                   "Content-Type": "application/json"
                               },
                               "method": "POST",
                               "uri": "https://management.azure.com/subscriptions/{subscription_id}/resourceGroups/{resourceGroup}/providers/Microsoft.Web/serverfarms/{appServicePlan}/workers/@{items('For_each_instance_Reboot_and_Wait')?['properties']?['machineName']}/reboot?api-version=2022-03-01"
                           },
                           "runAfter": {},
                           "type": "Http"
                       }
                   },
                   "foreach": "@body('Parse_Instances_Names')?['value']",
                   "runAfter": {
                       "Parse_Instances_Names": [
                           "Succeeded"
                       ]
                   },
                   "runtimeConfiguration": {
                       "concurrency": {
                           "repetitions": 1
                       }
                   },
                   "type": "Foreach"
               },
               "HTTP_-_Get_All_Instances": {
                   "inputs": {
                       "authentication": {
                           "type": "Raw",
                           "value": "Bearer @{body('Parse_OAuth_Token')?['access_token']}"
                       },
                       "headers": {
                           "Content-Type": "application/json"
                       },
                       "method": "GET",
                       "uri": "https://management.azure.com/subscriptions/{subscription_id}/resourceGroups/{resourceGroup}/providers/Microsoft.Web/sites/{appServiceName}/instances?api-version=2022-03-01"
                   },
                   "runAfter": {
                       "Parse_OAuth_Token": [
                           "Succeeded"
                       ]
                   },
                   "type": "Http"
               },
               "HTTP_-_Token": {
                   "inputs": {
                       "body": "grant_type=client_credentials&client_id={client_id}client_secret={client_secret}&resource=https://management.core.windows.net/",
                       "headers": {
                           "Content-Type": "application/x-www-form-urlencoded"
                       },
                       "method": "POST",
                       "uri": "https://login.microsoftonline.com/{tenant_id}/oauth2/token"
                   },
                   "runAfter": {},
                   "type": "Http"
               },
               "Parse_Instances_Names": {
                   "inputs": {
                       "content": "@body('HTTP_-_Get_All_Instances')",
                       "schema": {
                           "properties": {
                               "id": {},
                               "nextLink": {},
                               "value": {
                                   "items": {
                                       "properties": {
                                           "id": {
                                               "type": "string"
                                           },
                                           "location": {
                                               "type": "string"
                                           },
                                           "name": {
                                               "type": "string"
                                           },
                                           "properties": {
                                               "properties": {
                                                   "consoleUrl": {
                                                       "type": "string"
                                                   },
                                                   "containers": {},
                                                   "detectorUrl": {
                                                       "type": "string"
                                                   },
                                                   "healthCheckUrl": {},
                                                   "machineName": {
                                                       "type": "string"
                                                   },
                                                   "name": {
                                                       "type": "string"
                                                   },
                                                   "siteInstanceName": {
                                                       "type": "string"
                                                   },
                                                   "state": {
                                                       "type": "string"
                                                   },
                                                   "statusUrl": {
                                                       "type": "string"
                                                   }
                                               },
                                               "type": "object"
                                           },
                                           "type": {
                                               "type": "string"
                                           }
                                       },
                                       "required": [
                                           "id",
                                           "name",
                                           "type",
                                           "location",
                                           "properties"
                                       ],
                                       "type": "object"
                                   },
                                   "type": "array"
                               }
                           },
                           "type": "object"
                       }
                   },
                   "runAfter": {
                       "HTTP_-_Get_All_Instances": [
                           "Succeeded"
                       ]
                   },
                   "type": "ParseJson"
               },
               "Parse_OAuth_Token": {
                   "inputs": {
                       "content": "@body('HTTP_-_Token')",
                       "schema": {
                           "properties": {
                               "access_token": {
                                   "type": "string"
                               },
                               "expires_in": {
                                   "type": "string"
                               },
                               "expires_on": {
                                   "type": "string"
                               },
                               "ext_expires_in": {
                                   "type": "string"
                               },
                               "not_before": {
                                   "type": "string"
                               },
                               "resource": {
                                   "type": "string"
                               },
                               "token_type": {
                                   "type": "string"
                               }
                           },
                           "type": "object"
                       }
                   },
                   "runAfter": {
                       "HTTP_-_Token": [
                           "Succeeded"
                       ]
                   },
                   "type": "ParseJson"
               }
           },
           "contentVersion": "1.0.0.0",
           "outputs": {},
           "parameters": {
               "appServiceName": {
                   "defaultValue": "{appServiceName}",
                   "type": "String"
               },
               "appServicePlan": {
                   "defaultValue": "{appServicePlan}",
                   "type": "String"
               },
               "resourceGroup": {
                   "defaultValue": "{resourceGroup}",
                   "type": "String"
               }
           },
           "triggers": {
               "Recurrence": {
                   "evaluatedRecurrence": {
                       "frequency": "Day",
                       "interval": 1,
                       "schedule": {
                           "hours": [
                               "6",
                               "0",
                               "14"
                           ],
                           "minutes": [
                               0
                           ]
                       },
                       "startTime": "2023-04-28T00:00:00Z",
                       "timeZone": "Eastern Standard Time"
                   },
                   "recurrence": {
                       "frequency": "Day",
                       "interval": 1,
                       "schedule": {
                           "hours": [
                               "1"
                           ],
                           "minutes": [
                               0
                           ]
                       },
                       "startTime": "2023-04-28T00:00:00Z",
                       "timeZone": "Eastern Standard Time"
                   },
                   "type": "Recurrence"
               }
           }
       },
       "parameters": {}
      

      }


  2. Unfortunately,we dont have any default action that can you help you in restarting the app service scaled-out instances one-by-one.

    You need to create a custom logic app and by calling the below Azure Management Rest API’s using HTTP actions.

    1. Web Apps – List Instance Identifiers –> To get the list of scale-out instance with their respective name’s and ID under a App service.
    2. App Service Plans – Reboot Worker –> To restart specific worker instances ( for workername property pass the Machine Name pulled from above list operation)
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search