I've run into a rather perplexing issue with Quartz (version 2.2.4) in the C# project I'm working on, and in spite of my best efforts I haven't been able to resolve the issue.
The project requires several different tasks to be scheduled independently of one-another; this being the case I've written a class specifically to handle setting up jobs on a 24 hour cycle. The class is supposed to be instantiated once for each task that needs to be scheduled.
Below is the object in question:
namespace Project.Utilities {
public class QuartzScheduler {
private IScheduler scheduler;
private ITrigger trigger;
private IJobDetail job;
private JobKey jobKey;
public QuartzScheduler(IJob jobObject, string triggerId, string jobId,
string jobGroup, int syncHour, int syncMinute) {
scheduler = StdSchedulerFactory.GetDefaultScheduler();
scheduler.Start();
jobKey = new JobKey(jobId, jobGroup);
job = JobBuilder
.Create(jobObject.GetType())
.WithIdentity(jobKey)
.Build();
trigger = TriggerBuilder.Create()
.WithIdentity(triggerId)
.ForJob(jobKey)
.StartNow()
.WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(syncHour, syncMinute))
.Build();
scheduler.ScheduleJob(job, trigger);
}
public void Reschedule(int syncHour, int syncMinute) {
TriggerKey triggerKey = trigger.Key;
trigger = TriggerBuilder.Create()
.WithIdentity(triggerKey)
.ForJob(jobKey)
.StartNow()
.WithSchedule(CronScheduleBuilder
.DailyAtHourAndMinute(syncHour, syncMinute))
.Build();
scheduler.RescheduleJob(triggerKey, trigger);
}
public IScheduler GetScheduler() {
return scheduler;
}
public ITrigger GetTrigger() {
return trigger;
}
}
}
I've written a couple of NUnit tests to make sure that everything in the object works. It passes two of them, but always fails the third; the job definitely exists, and can even be rescheduled, but silently fails to fire when TriggerJob() is called. Rescheduling it and then waiting for it to fire similarly fails to work.
These are the tests:
namespace UnitTestProject.BackEnd_UnitTests.Util {
public class FakeController : IJob {
public static int fired = 0;
public FakeController() {
fired = 1;
}
public void Execute(IJobExecutionContext context) {
fired++;
}
}
[TestFixture]
public class QBSchedulerTest
{
private QuartzScheduler target;
private JobKey expectedJob;
private FakeController fake;
[TestFixtureSetUp]
public void fixture()
{
expectedJob = new JobKey("testJob", "testGroup");
}
[SetUp]
public void setup()
{
dummy = new FakeController();
target = new QuartzScheduler(dummy, "testTrigger", "testJob", "testGroup", 0, 1);
fakeController.fired = 0;
}
[TearDown]
public void teardown()
{
target.GetScheduler().Clear();
}
[Test]
public void HasBeenScheduled()
{
Assert.IsTrue(target.GetScheduler().CheckExists(expectedJob));
Assert.IsTrue(target.GetScheduler().IsStarted); Assert.IsTrue(target.GetScheduler().GetJobGroupNames().Contains("testJob"));
Assert.True(target.GetTrigger().JobKey.Equals(expectedJob));
Assert.Null(target.GetTrigger().GetPreviousFireTimeUtc());
Assert.IsTrue(target.GetTrigger().GetMayFireAgain());
Assert.AreEqual(0, dummyQboController.fired);
}
[Test]
public void CanBeRescheduled() {
target.Reschedule(1, 0);
string actual = target.GetTrigger().GetNextFireTimeUtc().ToString();
Assert.IsTrue(actual.Contains("5:00:00 AM"));
}
[Test]
public void CanBeExecuted() {
Assert.IsTrue(target.GetScheduler().CheckExists(expectedJob));
target.GetScheduler().TriggerJob(expectedJob);
Assert.IsTrue(target.GetScheduler().IsStarted);
Assert.IsTrue(target.GetTrigger().GetMayFireAgain());
Assert.NotNull(target.GetTrigger().GetPreviousFireTimeUtc());
Assert.AreEqual(1, fakeController.fired);
}
}
}
The only reason for the failure I've been able to find for why the job wouldn't activate has been that the scheduler can't find the default constructor. I've tried the tests with both the given method and with the default constructor, but the test fails anyway.
I'm at a loss as to why this could be. Can anyone provide possible suggestions?
Aucun commentaire:
Enregistrer un commentaire