Skip to content

Instantly share code, notes, and snippets.

@aaronhoffman
Last active October 30, 2020 13:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronhoffman/212dd858ff5d4275369f5fde01d9fd5d to your computer and use it in GitHub Desktop.
Save aaronhoffman/212dd858ff5d4275369f5fde01d9fd5d to your computer and use it in GitHub Desktop.
Start and Stop Azure WebJobs via the Kudu REST API
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;
}
}
@aaronhoffman
Copy link
Author

@fwd079
Copy link

fwd079 commented Apr 26, 2017

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.

@aaronhoffman
Copy link
Author

fixed -- thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment