Lukas Möller
Lukas Möller

Reputation: 143

Deactivate and activate Azure WebJobs in C#

i have a question, i want to deactivate webjobs of a appservice while deploying a new version and activate them after the deploying again.

is there a possibility in c# to do that?

thanky you guys

Upvotes: 0

Views: 1927

Answers (2)

rishit_s
rishit_s

Reputation: 350

To stop the WebJob from an external client you just need to make a REST call: https://github.com/projectkudu/kudu/wiki/WebJobs-API#stop-a-continuous-job

 POST https://{sitename}.scm.azurewebsites.net/api/continuouswebjobs/{job name}/stop

which will add a file of disabled.job

To start the WebJob again

   POST https://{sitename}.scm.azurewebsites.net/api/continuouswebjobs/{job name}/start

which will remove disabled job file and webjob would run again

Upvotes: 2

Bruce Chen
Bruce Chen

Reputation: 18465

As I known, you couldn't stop a triggered WebJob directly, you need to leverage process explorer to kill it via KUDU. For the continuous WebJobs, you could leverage WebJobs API to start/stop the WebJobs, you need to invoke the specificed Rest API with basic auth using Deployment credentials of your web app. Here is the c# code snippet to stop the WebJob:

string username = "{username}";
string password = "{password}";
string jobname = "{your-webjob-name}";
string authorization = Convert.ToBase64String(System.Text.UTF8Encoding.UTF8.GetBytes($"{username}:{password}"));
using (var client = new HttpClient())
{
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authorization);
    var res = await client.PostAsync($"https://{your-webapp-name}.scm.azurewebsites.net/api/continuouswebjobs/{jobname}/stop", null);
    Console.WriteLine($"StatusCode:{res.StatusCode}");
}

Note: At this point, a file named disable.job would be added with your WebJob as follows:

For start the WebJob, just call /api/continuouswebjobs/{job name}/start, then the disable.job file would be removed and your WebJob would run again.

Upvotes: 1

Related Questions