Last active
October 30, 2020 13:40
-
-
Save aaronhoffman/212dd858ff5d4275369f5fde01d9fd5d to your computer and use it in GitHub Desktop.
Start and Stop Azure WebJobs via the Kudu REST API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class KuduWebJobService | |
{ | |
private string KuduUserName { get; set; } | |
private string KuduPassword { get; set; } | |
private string AzureWebsiteName { get; set; } | |
public KuduWebJobService() | |
{ | |
this.KuduUserName = ""; | |
this.KuduPassword = ""; | |
this.AzureWebsiteName = ""; | |
} | |
public List<string> GetWebJobStatuses() | |
{ | |
var result = new List<string>(); | |
var client = this.CreateWebClient(); | |
var allJobsJson = client.DownloadString($"https://{this.AzureWebsiteName}.scm.azurewebsites.net/api/webjobs"); | |
dynamic allJobs = JsonConvert.DeserializeObject(allJobsJson); | |
foreach (var job in allJobs) | |
{ | |
var name = job["name"] ?? "[unknown]"; | |
var status = job["status"] ?? "[triggeredWebJob]"; | |
result.Add($"name:{name}; status:{status}"); | |
} | |
return result; | |
} | |
public string StartWebJob(string webJobName) | |
{ | |
var client = this.CreateWebClient(); | |
var response = client.UploadString($"https://{this.AzureWebsiteName}.scm.azurewebsites.net/api/continuouswebjobs/{webJobName}/start", "POST", ""); | |
return response; | |
} | |
public string StopWebJob(string webJobName) | |
{ | |
var client = this.CreateWebClient(); | |
var response = client.UploadString($"https://{this.AzureWebsiteName}.scm.azurewebsites.net/api/continuouswebjobs/{webJobName}/stop", "POST", ""); | |
return response; | |
} | |
private WebClient CreateWebClient() | |
{ | |
var client = new WebClient(); | |
var base64Auth = Convert.ToBase64String(Encoding.Default.GetBytes($"{this.KuduUserName}:{this.KuduPassword}")); | |
client.Headers.Add(HttpRequestHeader.Authorization, $"Basic {base64Auth}"); | |
return client; | |
} | |
} |
Hi,
Good little helper class. Thank you for sharing.
Minor typo at line 25:
var staus = job["status"] ?? "[triggeredWebJob]";
Should be:
var status = job["status"] ?? "[triggeredWebJob]";
Regards.
fixed -- thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
blog post for more info: http://aaron-hoffman.blogspot.com/2017/01/stop-azure-webjob-from-website.html