Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Azure App Services: Automate Application restart using Web Job

Use Case

Application is hosted on Azure as App Service and you want to restart your Application on a scheduled basis.

You can achieve this by creating a Web Job and placing a PowerShell scrip to STOP and START the web app. Please follow the steps below to implement this for your web app:

Get Azure profile for silent authentication

In order to perform START/STOP operation of you app service, the job should have access to your subscription. Please run below PowerShell script to download and save your Azure profile.

Login-AzureRmAccount

Save-AzureRmProfile -Path “C:azureprofile.json”

Note :When you run the above PowerShell, you will get an prompt for login.

Create PwerShell to STOP/START App Service

  • Create a new folder and place the publish profile downloaded in previous step.
  • Create a PowerShell and save as run.ps1
$ProgressPreference=”SilentlyContinue”

Select-AzureRmProfile -Path “azureprofile.json”

Select-AzureRmSubscription -SubscriptionId ‘

Stop-AzureRmWebApp -Name ‘AppService-Name’ -ResourceGroupName ‘AppService-Resource-Group-Name’

Start-AzureRmWebApp -Name ‘AppService-Name’ -ResourceGroupName ‘AppService-Resource-Group-Name’

If the application is running on multiple Instance than you can use below script to leverage Advance Application restart. In this case below script will restart worker process on each instance one by one. This can help in minimizing the downtime.

$ProgressPreference=”SilentlyContinue”

Select-AzureRmProfile -Path “azureprofile.json”

Select-AzureRmSubscription -SubscriptionId ‘

$siteName = “BackupDemo-karan”

$rgGroup = “ResourceGroup-Karan”

$webSiteInstances = @()

#This gives you list of instances

$webSiteInstances = Get-AzureRmResource -ResourceGroupName $rgGroup -ResourceType Microsoft.Web/sites/instances -ResourceName $siteName -ApiVersion 2015-11-01

$sub = (Get-AzureRmContext).Subscription.SubscriptionId

foreach ($instance in $webSiteInstances)

{    $instanceId = $instance.Name

“Going to enumerate all processes on {0} instance” -f $instanceId

# This gives you list of processes running

# on a particular instance

$processList =  Get-AzureRmResource `

-ResourceId /subscriptions/$sub/resourceGroups/$rgGroup/providers/Microsoft.Web/sites/$sitename/instances/$instanceId/processes `

-ApiVersion 2015-08-01

foreach ($process in $processList)

{

if ($process.Properties.Name -eq “w3wp”)

{

$resourceId = “/subscriptions/$sub/resourceGroups/$rgGroup/providers/Microsoft.Web/sites/$sitename/instances/$instanceId/processes/” + $process.Properties.Id

$processInfoJson = Get-AzureRmResource -ResourceId  $resourceId  -ApiVersion 2015-08-01

# is_scm_site is a property which is set

# on the worker process for the KUDU

if ($processInfoJson.Properties.is_scm_site -ne $true)

{

$computerName = $processInfoJson.Properties.Environment_variables.COMPUTERNAME

“Instance ID” + $instanceId  + “is for ” +   $computerName

“Going to stop this process ” + $processInfoJson.Name + ” with PID ” + $processInfoJson.Properties.Id

# Remove-AzureRMResource finally STOPS the worker process

$result = Remove-AzureRmResource -ResourceId $resourceId -ApiVersion 2015-08-01 -Force

if ($result -eq $true)

{

“Process {0} stopped ” -f $processInfoJson.Properties.Id

}

}

}

}

}

Create a scheduled web job

  • Create a zip file which contains both the files (ps1 and azureprofile.json).
  • Go to App Service -> Web Job and then create a triggered web job by uploading the zip file. I have attached a sample zip for your reference.

  • Make sure that Always on is enabled on the App Service where you are deploying this web job. Always on is required for triggered web jobs.

Note: Please follow the link below for information about scheduling web job using CRON expression:

  • https://docs.microsoft.com/en-us/azure/app-service-web/web-sites-create-web-jobs 

Share the post

Azure App Services: Automate Application restart using Web Job

×

Subscribe to Msdn Blogs | Get The Latest Information, Insights, Announcements, And News From Microsoft Experts And Developers In The Msdn Blogs.

Get updates delivered right to your inbox!

Thank you for your subscription

×