gpt4 book ai didi

c# - 我的工作不会中断 IScheduler.Interrupt

转载 作者:太空宇宙 更新时间:2023-11-03 21:31:20 25 4
gpt4 key购买 nike

我正在尝试实现一个 IInterruptableJob 接口(interface),以便我可以停止我的工作。

这就是我的工作实现的样子(取自 https://github.com/quartznet/quartznet/blob/master/src/Quartz.Examples/example7/InterruptExample.cs )

public class HelloJob : IInterruptableJob
{
// logging services
private static readonly ILog Log = LogManager.GetLogger(typeof(HelloJob));
// has the job been interrupted?
private bool _interrupted;
// job name
private JobKey _jobKey;

/// <summary>
/// Called by the <see cref="IScheduler" /> when a <see cref="ITrigger" />
/// fires that is associated with the <see cref="IJob" />.
/// </summary>
public virtual void Execute(IJobExecutionContext context)
{
_jobKey = context.JobDetail.Key;
Log.InfoFormat("---- {0} executing at {1}", _jobKey, DateTime.Now.ToString("r"));

try
{
// main job loop... see the JavaDOC for InterruptableJob for discussion...
// do some work... in this example we are 'simulating' work by sleeping... :)

for (int i = 0; i < 4; i++)
{
try
{
Thread.Sleep(10 * 1000);
}
catch (Exception ignore)
{
Console.WriteLine(ignore.StackTrace);
}

// periodically check if we've been interrupted...
if (_interrupted)
{
Log.InfoFormat("--- {0} -- Interrupted... bailing out!", _jobKey);
throw new JobExecutionException("Interrupt job");
//return;
// could also choose to throw a JobExecutionException
// if that made for sense based on the particular
// job's responsibilities/behaviors
}
}
}
finally
{
Log.InfoFormat("---- {0} completed at {1}", _jobKey, DateTime.Now.ToString("r"));
}
}

/// <summary>
/// Called by the <see cref="IScheduler" /> when a user
/// interrupts the <see cref="IJob" />.
/// </summary>
public virtual void Interrupt()
{
Log.Info("--- -- INTERRUPTING --");
_interrupted = true;
}

}

这是我的主要方法。

private static void Main(string[] args)
{
try
{
LogManager.Adapter = new Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter
{
Level = LogLevel.Info
};

// Grab the Scheduler instance from the Factory
IScheduler scheduler = GetScheduler();

//scheduler.Interrupt(@"group1.69decadb-2385-4c30-8d19-22088601670c");
Guid guid = Guid.NewGuid();

IJobDetail job = JobBuilder.Create<HelloJob>()
.WithIdentity(guid.ToString(), "group1")
.Build();

ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(10)
.RepeatForever()
.WithMisfireHandlingInstructionNextWithRemainingCount())
.Build();

var key = job.Key;
// Tell quartz to schedule the job using our trigger
scheduler.ScheduleJob(job, trigger);

}
catch (SchedulerException se)
{
Console.WriteLine(se);
}

Console.WriteLine("Press any key to close the application");
Console.ReadKey();
}

作业以正确的间隔时间正常触发。但是,当我发送中断时,它不会停止工作:(

我正在尝试停止工作

IScheduler scheduler = GetScheduler();
var jobkey = new JobKey("69decadb-2385-4c30-8d19-22088601670c","group1");

scheduler.Interrupt(jobkey);

我是在job调度的时候断点取到job key,保存key然后测试中断。从数据库交叉验证。

但即使我发送了中断,工作仍在继续并且触发器仍在触发。我从 qrtz_simple_triggers 检查了这一点 table.TIMES_TRIGGERED 即使在发送中断后也总是递增。

我不确定我在做什么错。

最佳答案

中断意味着用于运行作业。它不会将整个作业置于暂停状态。无论是否在处理过程中被中断,都会触发作业(计数器递增)。中断适用于需要发出停止信号的长时间运行的作业 - 下一次运行将按计划进行。

您可能正在寻找的是 IScheduler.PauseJob(JobKey),这将暂停所有作业触发器,从而停止执行,直到再次恢复触发器。

关于c# - 我的工作不会中断 IScheduler.Interrupt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24015835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com