如何在 quartz scheduler c# 中检查特定作业是否为 运行

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

我正在使用 quartz 调度程序根据触发器的用户输入来安排写入文件的作业。我想在 stop() 方法中检查作业是否仍然是 运行。我如何检查作业是否仍然是 运行?

 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();
    }
}

在 Quartz 2.x 中,您可以从调度程序中获取所有正在执行的作业,我没有用 3.x 对其进行测试,但我确定它是相同的。

var executingJobs = scheduler.GetCurrentlyExecutingJobs();