Track the status of Azure web job using c#
Let’s start with a basic introduction to Azure web jobs.
What is an Azure web job?
Azure WebJobs is a feature of App service that enables you to run a program or script in the same instance as a web app or API app. There is no additional cost to use WebJobs. However, the web jobs is not yet supported for App Service on Linux.
Why we use it?
It can be very useful for running background jobs like sending notifications, emails, logging, and so on.
Create a class to store the information about the web jobs details
public class Runs{public string id { get; set; }public string name { get; set; }public string status { get; set; }public DateTime start_time { get; set; }public DateTime end_time { get; set; }public string duration { get; set; }public string output_url { get; set; }public string error_url { get; set; }public string url { get; set; }public string job_name { get; set; }public string trigger { get; set; }}
Add one more class to keep the Run class detail into the List
public class WebJobHistory{public IList<Runs> runs { get; set; }}
How to get last run information using C#?
var url = https://{YOUR DOMAIN NMAE}/api/{TRIGGEREDWEBJOBS/CONTINUOUSWEBJOBS}/{WEBJOB NAME}/historyvar authcode = “Keep your auth code here”HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(string.Format(“{0}”, url));request.Method = “GET”;var byteArray = Encoding.ASCII.GetBytes(authCode);request.Headers.Add(“Authorization”, “Basic “ + Convert.ToBase64String(byteArray));request.ContentLength = 0;var response = (HttpWebResponse)request.GetResponse();using (Stream responseStream = response.GetResponseStream()){StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);var result = reader.ReadToEnd();var list = JsonConvert.DeserializeObject<WebJobHistory>(result);}
The list will have all the history of your web job run.
Hope this helps.