完成后如何强制石英.net 作业重新启动间隔

Jür*_*ock 2 .net c# quartz.net topshelf

我有一个项目,我使用TopShelfTopShelf.Quartz

按照这个例子,我正在构建我的工作

                s.ScheduleQuartzJob(q =>
                    q.WithJob(() => JobBuilder.Create<MyJob>().Build())
                    .AddTrigger(() => TriggerBuilder.Create()
                        .WithSimpleSchedule(builder => builder
                            .WithIntervalInSeconds(5)
                            .RepeatForever())
                        .Build())
                );
Run Code Online (Sandbox Code Playgroud)

即使前一个仍在运行,它也会每五秒触发一次我的工作。我真正想要实现的是开始一项工作,完成后等待五秒钟,然后重新开始。这是可能的还是我必须实现我自己的逻辑(例如通过静态变量)。

stu*_*rtd 5

@NateKerkhofs 提出的工作侦听器将起作用,如下所示:

public class RepeatAfterCompletionJobListener : IJobListener
{
    private readonly TimeSpan interval;

    public RepeatAfterCompletionJobListener(TimeSpan interval)
    {
        this.interval = interval;
    }

    public void JobExecutionVetoed(IJobExecutionContext context)
    {
    }

    public void JobToBeExecuted(IJobExecutionContext context)
    {
    }

    public void JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException)
    {
       string triggerKey = context.JobDetail.Key.Name + ".trigger";

        var trigger = TriggerBuilder.Create()
                .WithIdentity(triggerKey)
                .StartAt(new DateTimeOffset(DateTime.UtcNow.Add(interval)))
                .Build();

        context.Scheduler.RescheduleJob(new TriggerKey(triggerKey), trigger);
    }

    public string Name
    {
        get
        {
            return "RepeatAfterCompletionJobListener";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后将侦听器添加到调度程序:

var jobKey = "myJobKey";
var schedule = new StdSchedulerFactory().GetScheduler();
listener = new
   RepeatAfterCompletionJobListener(TimeSpan.FromSeconds(5));

schedule.ListenerManager.AddJobListener
         (listener, KeyMatcher<JobKey>.KeyEquals(new JobKey(jobKey)));

var job = JobBuilder.Create(MyJob)
                .WithIdentity(jobKey)
                .Build();

// Schedule the job to start in 5 seconds to give the service time to initialise
var trigger = TriggerBuilder.Create()
                .WithIdentity(CreateTriggerKey(jobKey))
                .StartAt(DateTimeOffset.Now.AddSeconds(5))
                .Build();

schedule.ScheduleJob(job, trigger);
Run Code Online (Sandbox Code Playgroud)

不幸的是,我不知道如何使用 Typshelf.Quartz 库使用的流畅语法来做到这一点(或者是否可以做到),我将它与 TopShelf 和常规 Quartz.Net 一起使用。