问题
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