How to check if a particular job is running in quartz scheduler c#

本小妞迷上赌 提交于 2019-12-11 07:53:29

问题


I am using quartz scheduler to schedule a job of writing to a file depending on user input on basis of triggers. I want to check if the job is still running in the stop() method. How can I check if the job is still running?

 public class JobScheduler
{
    static StdSchedulerFactory schedulerFactory = new StdSchedulerFactory();
    public IScheduler scheduler = schedulerFactory.GetScheduler().Result;
    public void start()
    {
        scheduler.Start();

        IJobDetail jobDetail = JobBuilder.Create<FileWritingJob>().WithIdentity("FileWriteJob","FileWriteGroup").Build();

        string date_time = System.Configuration.ConfigurationManager.AppSettings["datetime"];
        System.Diagnostics.Debug.WriteLine(date_time);
        DateTime dateTime = DateTime.Parse(date_time);
        System.Diagnostics.Debug.WriteLine("Parse datetime:"+dateTime);
        string frequency= System.Configuration.ConfigurationManager.AppSettings["frequency"];
        System.Diagnostics.Debug.WriteLine("Frequency:" + frequency);
        if (frequency.Equals("hourly", StringComparison.InvariantCultureIgnoreCase) && !frequency.Equals("default"))
        {
            ITrigger hourlyTrigger = TriggerBuilder.Create().WithIdentity("hourlyTrigger").WithCronSchedule("0 " + dateTime.Minute + " " + dateTime.Hour + "/1 1/1 * ? *").StartAt(DateBuilder.DateOf(dateTime.Hour, dateTime.Minute, dateTime.Second, dateTime.Day, dateTime.Month, dateTime.Year)).Build();
            scheduler.ScheduleJob(jobDetail, hourlyTrigger);
        }
        else if (frequency.Equals("daily", StringComparison.InvariantCultureIgnoreCase) && !frequency.Equals("default"))
        {
            ITrigger dailyTrigger = TriggerBuilder.Create().WithIdentity("dailyTrigger").WithCronSchedule("0 " + dateTime.Minute + " " + dateTime.Hour + " 1/1 * ? *").StartAt(DateBuilder.DateOf(dateTime.Hour, dateTime.Minute, dateTime.Second, dateTime.Day, dateTime.Month, dateTime.Year)).Build();
            scheduler.ScheduleJob(jobDetail, dailyTrigger);
        }
        else if (frequency.Equals("weekly", StringComparison.InvariantCultureIgnoreCase) && !frequency.Equals("default"))
        {
            ITrigger weeklyTrigger = TriggerBuilder.Create().WithIdentity("weeklyTrigger").WithCronSchedule("0 " + dateTime.Minute + " " + dateTime.Hour + " 1/7 * ? *").StartAt(DateBuilder.DateOf(dateTime.Hour, dateTime.Minute, dateTime.Second, dateTime.Day, dateTime.Month, dateTime.Year)).Build();
            scheduler.ScheduleJob(jobDetail, weeklyTrigger);
        }

    }
    public void stop()
    {
        scheduler.Shutdown().ConfigureAwait(false).GetAwaiter().GetResult();
    }
}

回答1:


In Quartz 2.x you can get all executing jobs from the scheduler, i didn't tested it with 3.x but i'm sure it is the same.

var executingJobs = scheduler.GetCurrentlyExecutingJobs();


来源:https://stackoverflow.com/questions/53308576/how-to-check-if-a-particular-job-is-running-in-quartz-scheduler-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!