2

I'm working with an Azure cloud service (classic) that has a couple role processes. One of them is a worker that's become a little unstable after a week so I want to restart it every few days. Eventually the worker role will be made stable but in the meantime it would be nice to auto-restart it every few days if possible.

Is there a way to restart an Azure classic cloud service worker role every day or so? Programmatically or via configuration?

Mark Rogers
  • 96,497
  • 18
  • 85
  • 138

2 Answers2

2

Absolutely Yes, there are two ways to restart an Azure classic Cloud Service role instance via triggered programmatically per an interval.

  1. Call the REST API Reboot Role Instance with a crontab trigger in programming
  2. You can restart these Virtual Machines of the role via call the REST API Virtual Machines - Restart in programming or directly use the same feature API of Azure SDK for a programming language.
Peter Pan
  • 23,476
  • 4
  • 25
  • 43
  • +1 I posted this question to the Microsoft forum as well and someone mentioned that Automation could be used to do this. – Mark Rogers Dec 18 '18 at 14:21
2

I asked this question on the Azure forum and on Reddit.

The first response was at the Azure Forum, Marcin said:

You can use for this purpose Azure Automation

https://learn.microsoft.com/en-us/azure/cloud-services/automation-manage-cloud-services

https://gallery.technet.microsoft.com/scriptcenter/Reboot-Cloud-Service-PaaS-b337a06d

Then on Reddit, quentech said:

You can do it with a PowerShell Workflow Runbook:

workflow ResetRoleClassic
{
    Param
    (
        [Parameter (Mandatory = $true)]
        [string]$serviceName,
        [Parameter (Mandatory = $true)]
        [string]$slot,
        [Parameter (Mandatory = $true)]
        [string]$instanceName
    )  

    $ConnectionAssetName = "AzureClassicRunAsConnection"

    # Get the connection
    $connection = Get-AutomationConnection -Name $connectionAssetName        

    # Authenticate to Azure with certificate
    Write-Verbose "Get connection asset: $ConnectionAssetName" -Verbose
    $Conn = Get-AutomationConnection -Name $ConnectionAssetName
    if ($Conn -eq $null)
    {
        throw "Could not retrieve connection asset: $ConnectionAssetName. Assure that this asset exists in the Automation account."
    }

    $CertificateAssetName = $Conn.CertificateAssetName
    Write-Verbose "Getting the certificate: $CertificateAssetName" -Verbose
    $AzureCert = Get-AutomationCertificate -Name $CertificateAssetName

    if ($AzureCert -eq $null)
    {
        throw "Could not retrieve certificate asset: $CertificateAssetName. Assure that this asset exists in the Automation account."
    }

    Write-Verbose "Authenticating to Azure with certificate." -Verbose    
    Set-AzureSubscription -SubscriptionName $Conn.SubscriptionName -SubscriptionId $Conn.SubscriptionID -Certificate $AzureCert 

    Select-AzureSubscription -SubscriptionId $Conn.SubscriptionID

    Write-Verbose "Getting $serviceName Role." -Verbose

    $results = Get-AzureRole -ServiceName $serviceName -InstanceDetails
    Write-Output $results

    Write-Verbose "Resetting Role Instance $instanceName" -Verbose

    $results = Reset-AzureRoleInstance -ServiceName $serviceName -Slot $slot -InstanceName $instanceName -Reboot    
    Write-Output $results
}

I made some minor changes to the parameters and removed the outer braces. And thus was able to use the script as is for the most part.

Community
  • 1
  • 1
Mark Rogers
  • 96,497
  • 18
  • 85
  • 138