locked
How do I stop a running Quartz.NET scheduler in WebMatrix? RRS feed

  • Question

  • User1504070297 posted

    I want to give the user the ability to "stop" a running Quartz.NET scheduler. The website is built using WebMatrix. The code listed below compiles and the scheduler starts and emails are sent. The problem I have is that I need to get the "handle" of the running Scheduler from a user webpage (a .cshtml file). All the user webpage code that I have tried will not compile. Listed below is the code to start the scheduler - it compiles and runs just fine. What should the code be for the user webpage (a .cshtml file) to get the handle (instance of the scheduler) of the running scheduler? I read Mike Brind's excellent article (http://www.mikesdotnetting.com/article/254/scheduled-tasks-in-asp-net-with-quartz-net) on Quartz.NET. Thanks Mr. Brind.

    I would like to write code something like this in my stop_scheduler.cshtml file:

    @using Quartz;
    @using Quartz.Impl;

    @{
    IScheduler sched = SchedulerManager.getInstance().getScheduler();
    sched.shutdown(true);
    }

    But the above code will not compile. The following code compiles and runs just fine.

    My _AppStart.cshtml file

    @using Quartz;
    @using Quartz.Impl;

    @{
    ISchedulerFactory schedFact = new StdSchedulerFactory();

    IScheduler sched = schedFact.GetScheduler();
    sched.Start();

    IJobDetail job = JobBuilder.Create<MyJob>()
    .WithIdentity("myJob", "group1")
    .Build();

    ITrigger trigger = TriggerBuilder.Create()
    .WithIdentity("myTrigger", "group1")
    .StartNow()
    .WithSimpleSchedule(x => x
    .WithIntervalInSeconds(40)
    .RepeatForever())
    .Build();

    sched.ScheduleJob(job, trigger);
    }

    My Classes.cs file

    using SendGrid;
    using Quartz;
    using Quartz.Impl;

    public class MyJob : IJob
    {
    public void Execute(IJobExecutionContext context)
    {
    string SendGrid_user_name = "";
    string SendGrid_password = "";
    string from_name = "";
    string from_email_address = "";

    string to_email_address = "jim@hotmail.com";
    string subject = "Repeating task";
    string email_body = "Lots of data";

    SendGrid_user_name = "a88d835@azure.com";
    SendGrid_password = "kdk333mm";
    from_name = "The new website";
    from_email_address = "xxx@not_real.com";

    SendGridMessage email_structure = new SendGridMessage();

    email_structure.AddTo(to_email_address);
    email_structure.From = new MailAddress(from_email_address, from_name);
    email_structure.Subject = subject;
    email_structure.Text = email_body;

    var credentials = new NetworkCredential(SendGrid_user_name, SendGrid_password);
    var transportWeb = new Web(credentials);
    transportWeb.DeliverAsync(email_structure);
    }
    }

    Sunday, December 20, 2015 11:28 PM

Answers

  • User-166373564 posted

    Hi,

    Namespace: Quartz

    Assembly: Quartz (in Quartz.dll) Version: 2.2.1.400

    Syntax

    void Shutdown(

             bool waitForJobsToComplete

    )

    Parameters

    waitForJobsToCompleteType: System.Boolean

    if true the scheduler will not allow this method to return until all currently executing jobs have completed.

    Try the following.

    IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();

    scheduler.Shutdown(false);

    Best regards,

    Angie

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 21, 2015 11:45 AM

All replies

  • User-166373564 posted

    Hi,

    Namespace: Quartz

    Assembly: Quartz (in Quartz.dll) Version: 2.2.1.400

    Syntax

    void Shutdown(

             bool waitForJobsToComplete

    )

    Parameters

    waitForJobsToCompleteType: System.Boolean

    if true the scheduler will not allow this method to return until all currently executing jobs have completed.

    Try the following.

    IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();

    scheduler.Shutdown(false);

    Best regards,

    Angie

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 21, 2015 11:45 AM
  • User325035487 posted

    Asked here http://stackoverflow.com/questions/12478054/unscheduling-job-in-quartz

    // Summary:
        //     Remove the indicated Quartz.Trigger from the scheduler.
        bool UnscheduleJob(string triggerName, string groupName);
    
    //or
    UnscheduleJob(Quartz.TriggerKey key)
    Monday, December 21, 2015 11:49 AM
  • User1504070297 posted

    Thanks Angie,

    Your solution worked great. How excellent. I was stuck. Thanks for taking your time to help me. Thank you very much. Happy Holidays. 

    Robert

    Monday, December 21, 2015 9:01 PM
  • User1504070297 posted

    Thanks Jkjhse, 

    Thanks for the link to the previous stackoverflow question. I tried it but the run-time machine responded with  "Quartz.IScheduler' does not contain a definition for 'unscheduleJob'". Because I'm way behind schedule I'm going to use Angie's solution. I'm keeping your answer, just in case I need it in the future. Thanks for taking your time to help me. I appreciate it. Thank you. Happy Holidays.

    Robert

    Monday, December 21, 2015 9:07 PM