Search code examples
azure-cloud-servicesazure-worker-roles

Is there anyway to restart an Azure classic cloud service role every interval?


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?


Solution

  • 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.