Get all jobs in Quartz.NET 2.0
Solution 1:
You can use fetch a list of executing jobs:
var executingJobs = sched.GetCurrentlyExecutingJobs();
foreach (var job in executingJobs)
{
// Console.WriteLine(job.JobDetail.Key);
}
or fetch all the info about scheduled jobs (sample console application):
private static void GetAllJobs(IScheduler scheduler)
{
IList<string> jobGroups = scheduler.GetJobGroupNames();
// IList<string> triggerGroups = scheduler.GetTriggerGroupNames();
foreach (string group in jobGroups)
{
var groupMatcher = GroupMatcher<JobKey>.GroupContains(group);
var jobKeys = scheduler.GetJobKeys(groupMatcher);
foreach (var jobKey in jobKeys)
{
var detail = scheduler.GetJobDetail(jobKey);
var triggers = scheduler.GetTriggersOfJob(jobKey);
foreach (ITrigger trigger in triggers)
{
Console.WriteLine(group);
Console.WriteLine(jobKey.Name);
Console.WriteLine(detail.Description);
Console.WriteLine(trigger.Key.Name);
Console.WriteLine(trigger.Key.Group);
Console.WriteLine(trigger.GetType().Name);
Console.WriteLine(scheduler.GetTriggerState(trigger.Key));
DateTimeOffset? nextFireTime = trigger.GetNextFireTimeUtc();
if (nextFireTime.HasValue)
{
Console.WriteLine(nextFireTime.Value.LocalDateTime.ToString());
}
DateTimeOffset? previousFireTime = trigger.GetPreviousFireTimeUtc();
if (previousFireTime.HasValue)
{
Console.WriteLine(previousFireTime.Value.LocalDateTime.ToString());
}
}
}
}
}
I've used the code found here.
UPDATE:
If someone is interested a sample code can be downloaded from my GitHub repository.
Someone asked how to get a list of job completed.
I don't think there's an easy way for that.
The only option which comes to mind is using a job (or trigger) listener.
I've uploaded a sample on github where my main program can receive events of completed jobs.
Solution 2:
Since Quartz.NET version 2.2.1 you can make use of GroupMatcher<>.AnyGroup()
, here implemented as an extension method to IScheduler
:
public static List<IJobDetail> GetJobs(this IScheduler scheduler)
{
List<IJobDetail> jobs = new List<IJobDetail>();
foreach (JobKey jobKey in scheduler.GetJobKeys(GroupMatcher<JobKey>.AnyGroup()))
{
jobs.Add(scheduler.GetJobDetail(jobKey));
}
return jobs;
}
This will get you a list of IJobDetail
s for every scheduled job.
Solution 3:
If you want to get the Repeat Interval, Repeat Count etc cast the ITrigger to ISimpleTrigger
private void LogInfo(IScheduler scheduler)
{
log.Info(string.Format("\n\n{0}\n", Scheduler.GetMetaData().GetSummary()));
var jobGroups = scheduler.GetJobGroupNames();
var builder = new StringBuilder().AppendLine().AppendLine();
foreach (var group in jobGroups)
{
var groupMatcher = GroupMatcher<JobKey>.GroupContains(group);
var jobKeys = scheduler.GetJobKeys(groupMatcher);
foreach (var jobKey in jobKeys)
{
var detail = scheduler.GetJobDetail(jobKey);
var triggers = scheduler.GetTriggersOfJob(jobKey);
foreach (ITrigger trigger in triggers)
{
builder.AppendLine(string.Format("Job: {0}", jobKey.Name));
builder.AppendLine(string.Format("Description: {0}", detail.Description));
var nextFireTime = trigger.GetNextFireTimeUtc();
if (nextFireTime.HasValue)
{
builder.AppendLine(string.Format("Next fires: {0}", nextFireTime.Value.LocalDateTime));
}
var previousFireTime = trigger.GetPreviousFireTimeUtc();
if (previousFireTime.HasValue)
{
builder.AppendLine(string.Format("Previously fired: {0}", previousFireTime.Value.LocalDateTime));
}
var simpleTrigger = trigger as ISimpleTrigger;
if (simpleTrigger != null)
{
builder.AppendLine(string.Format("Repeat Interval: {0}", simpleTrigger.RepeatInterval));
}
builder.AppendLine();
}
}
}
builder.AppendLine().AppendLine();
log.Info(builder.ToString);
}
Solution 4:
For Quartz.NET version 3.0 you can use
this will show you all running jobs and triggers in quartz.net 3.x
using Quartz;
using Quartz.Impl;
using Quartz.Impl.Matchers;
class Program
{
var allTriggerKeys = scheduler.GetTriggerKeys(GroupMatcher<TriggerKey>.AnyGroup());
foreach (var triggerKey in allTriggerKeys.Result)
{
var triggerdetails = scheduler.GetTrigger(triggerKey);
var Jobdetails = scheduler.GetJobDetail(triggerdetails.Result.JobKey);
Console.WriteLine("IsCompleted -" + triggerdetails.IsCompleted + " | TriggerKey - " + triggerdetails.Result.Key.Name + " Job key -" + triggerdetails.Result.JobKey.Name);
}
}
I've used the code found here.