How to start and stop schedule webjob using asp .net c#

380 views Asked by At

I have created a trigger web job for some web application call, but I got a requirement that trigger job customer should be able to start or stop when want, and can also schedule.

I have done the schedule part using webjobs rest API but I am not able to complete start and stop work as trigger job does not have any option.

Is there a way to start and stop trigger webjob. I have tried to use kudo kill process but after webjob trigger completes it does not appear in process explorer.

var base64Auth = Convert.ToBase64String(Encoding.Default.GetBytes($"{azureUserName}:{azurePassword}"));

if (string.Equals(jobStatus, "STOP", StringComparison.InvariantCultureIgnoreCase)) 
{
  using(var client = new HttpClient()) 
  {
    client.DefaultRequestHeaders.Add("Authorization", "Basic " + base64Auth);
    
    //var baseUrl = new Uri($"https://{webAppName}.scm.azurewebsites.net/");
    var requestURl = string.Format("{0}/api/processes", azureWebAppUrl);
    
    var response = client.GetAsync(requestURl).Result;
    
    if (response.IsSuccessStatusCode == true) 
    {
      string res = response.Content.ReadAsStringAsync().Result;
      var processes = JsonConvert.DeserializeObject < List < ProcessData >> (res);
      
      if (processes.Any(x => x.name.Contains(azureWebJobName))) 
      {
        requestURl = string.Format("{0}/api/processes/{1}", azureWebAppUrl, processes.Where(x => x.name.Contains(azureWebJobName)).FirstOrDefault().id);
        response = client.DeleteAsync(requestURl).Result;
      }

      returnStr = "success";
    } 
    else 
    {
      returnStr = response.Content.ToString();
    }
  }
}

Please help me to understand a better way to stop and start the trigger web job process. I also found that we can add app settings to the main application like WEBJOBS_STOPPED and WEBJOBS_DISABLE_SCHEDULE but it will depend and need to update it every time, I want to rely on webjob setting completely instead of main application settings.

1

There are 1 answers

1
Sajad Bagherzadeh On

I do not know if I understand your problem or not.

I think, You can use Quartz(1) for start job/jobs and schedule it with application setting.

https://www.quartz-scheduler.net/