获取 Quartz.NET 2.0 中的所有作业

问题描述 投票:0回答:5

我已经在服务器上设置了 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 已被删除,查看源代码,已移至 JobStoreCMS 继承的基类 JobStoreSupport 中。然而,该方法已被标记为受保护,因此无法从外部访问。

在 2.0 中如何检索工作列表?

c# scheduled-tasks quartz.net
5个回答
49
投票

您可以使用获取正在执行的作业列表:

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,我的主程序可以接收已完成作业的事件。


10
投票

从 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
列表。


6
投票

如果您想获取重复间隔、重复计数等,请将 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
投票

对于 Quartz.NET 版本 3.0 您可以使用

这将向您显示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);

            }
}

我使用了找到的代码这里


0
投票

对于.Net Core最新版本您可以使用

private async Task getScheduledJobListAsync(IScheduler scheduler)
{
    var jobKeys = await scheduler.GetJobKeys(GroupMatcher<JobKey>.AnyGroup());
    var jobDetails = new List<(string, string, string, string)>();

    foreach (var jobKey in jobKeys)
    {
        var jobDetail = await scheduler.GetJobDetail(jobKey);

        if (jobDetail != null)
        {
            var triggers = await scheduler.GetTriggersOfJob(jobKey);
            foreach (var trigger in triggers)
            {
                Console.WriteLine("Job Key = " + jobKey.Name);
                Console.WriteLine("Job Schduled Time = " + trigger.GetNextFireTimeUtc()?.ToString() ?? "Not scheduled");
                Console.WriteLine("Job Group = " + jobKey.Group);
                Console.WriteLine("Job Trigger Name= " + trigger.Key.Name);
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.