C# 获取 Quartz.NET 2.0 中的所有工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12489450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:25:11  来源:igfitidea点击:

Get all jobs in Quartz.NET 2.0

c#scheduled-tasksquartz.net

提问by André Hauptfleisch

I've setup my AdoJobStore on the server and all my jobs are running perfectly. Now I am writing a remote client to manage all my jobs.

我已经在服务器上设置了我的 AdoJobStore,我的所有作业都运行良好。现在我正在编写一个远程客户端来管理我的所有工作。

Scheduling new jobs is straightforward enough, but I can't seem to retrieve a list of existing jobs in version 2.0. All the resources I found did something like the following.

安排新作业非常简单,但我似乎无法在 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]);
    }
}

The problem I'm facing is that GetJobNames has been removed, and looking at the source code, has been moved to the base class JobStoreSupport, which JobStoreCMS inherits from. The method has however been marked as protected, so it is inaccessible from the outside.

我面临的问题是 GetJobNames 已被删除,查看源代码,已移至 JobStoreCMS 继承的基类 JobStoreSupport。但是,该方法已标记为受保护,因此无法从外部访问。

How would one go about retrieving a job list in 2.0?

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

采纳答案by LeftyX

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.

如果有人感兴趣,可以从我的 GitHub存储库下载示例代码。

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 sampleon github where my main program can receive events of completed jobs.

我在 github 上上传了一个示例,我的主程序可以在其中接收已完成作业的事件。

回答by superlogical

If you want to get the Repeat Interval, Repeat Count etc cast the ITrigger to ISimpleTrigger

如果您想获得重复间隔、重复计数等,请将 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); 
}

回答by Basti M

Since Quartz.NET version 2.2.1 you can make use of GroupMatcher<>.AnyGroup(), here implemented as an extension method to IScheduler:

从 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;
}

This will get you a list of IJobDetails for every scheduled job.

这将为您IJobDetail提供每个预定作业的s列表。

回答by Hasan_Naser

For Quartz.NET version 3.0you can use

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

this will show you all running jobs and triggers in quartz.net 3.x

这将显示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 foundhere.

我使用了在这里找到的代码