我在服务器上设置了AdoJobStore,我的所有工作都运行得很好。现在我正在编写一个远程客户端来管理我的所有工作。
安排新作业非常简单,但我似乎无法检索2.0版中的现有作业列表。我找到的所有资源都做了类似的事情。
var groups = sched.JobGroupNames;
for (int i = 0; i < groups.Length; i++)
{
string[] names = sched.GetJobNames(groups[i]);
for (int j = 0; j < names.Length; j++)
{
var currentJob = sched.GetJobDetail(names[j], groups[i]);
}
}
我面临的问题是GetJobNames已被删除,并且查看源代码,已被移至JobStoreCpport继承的基类JobStoreSupport。但是,该方法已被标记为受保护,因此无法从外部访问。
如何在2.0中检索作业列表?
答案 0 :(得分:43)
您可以使用获取正在执行的作业列表:
var executingJobs = sched.GetCurrentlyExecutingJobs();
foreach (var job in executingJobs)
{
// Console.WriteLine(job.JobDetail.Key);
}
或获取有关预定作业的所有信息(示例控制台应用程序):
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());
}
}
}
}
}
我使用了here找到的代码。
<强>更新强>:
如果有人有兴趣,可以从我的GitHub repository下载示例代码。
有人询问如何获得完成的工作清单
我不认为有一个简单的方法。
想到的唯一选择是使用作业(或触发器)监听器。
我在github上传了一个sample,我的主程序可以接收已完成作业的事件。
答案 1 :(得分:4)
如果你想获得重复间隔,重复计数等将ITrigger转换为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);
}
答案 2 :(得分:4)
从Quartz.NET 2.2.1开始,您可以使用GroupMatcher<>.AnyGroup()
,这里实现为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;
}
这将为您提供每个预定作业的IJobDetail
列表。
答案 3 :(得分:2)