How to restart an Azure WebJob from a .Net 6+ program

210 Views Asked by At

All guidance I've been able to find, points to using:

Microsoft.Azure.Management.ResourceManager.Fluent

but that package has been deprecated and suggests this (from the nuget page):

If you are looking for the latest packages to interact with Azure resources, please use the following libraries: Azure.ResourceManager Azure.ResourceManager.Resources

After installing those packages plus Azure.ResourceManager.AppService, I can get close with this:

var webApp = client.GetResourceGroupResource(ResourceIdentifier.Parse("resId"));
var webSite = webApp.GetWebSite("siteName").Value;
var webjob = webSite.GetWebSiteWebJob("jobname");

(resId, siteName and jobname aren't relevant for the question)

On the webSite object there is this:

webSite.Restart(); // also an Async version

But on the webjob, there is nothing I can see to kick off a restart. All of the following lack a restart option:

webjob.NotHere();
webjob.Value.NotHere();
webjob.value.Data.NotHere();

I feel like I'm missing something. How do I restart a webjob using the new nuget packages? Any guidance is greatly appreciated!

2

There are 2 best solutions below

0
On

Figured out I was missing something simple. Webjobs can be triggered or continuous, only continuous may be stopped and started. So if the objective is to restart, I need to explicitly get only continuous webjobs, not simply 'webjobs'.

The above code becomes:

var client = new ArmClient(new DefaultAzureCredential());
var webSite = client.GetWebSiteResource(ResourceIdentifier.Parse("resId"));
var webjob = webSite.GetWebSiteContinuousWebJob("webjobname");
webjob.Value.StopContinuousWebJob();

The key difference being, "GetWebSiteContinuousWebJob", which returns a "WebSiteContinuousWebJobResource", which has the required Start and Stop methods.

1
On

The webSite.Restart() method is used to restart the entire Azure Web App. If you want to restart a specific WebJob, you can use the Azure REST API to send an HTTP POST request.

using System.Net.Http;

var client = new HttpClient();
var url = $"https://{webAppName}.scm.azurewebsites.net/api/triggeredwebjobs/{webJobName}/run";
var response = await client.PostAsync(url, null);

enter image description here

Even you have an option in portal. Go to the kudo and select process explorer and directly kill the webjob of the specific instance.

  • Since web jobs run as child processes of Kudu, killing the Kudu process effectively stops and restarts the web jobs. enter image description here

This method is effective for restarting web jobs that are set to run continuously. If you have triggered or scheduled web jobs, they may need to be manually triggered or scheduled again after the restart. check for the reference.