ravendb / quartznet-RavenDB

RavenDB JobStore support for Quartz.NET scheduler.
Apache License 2.0
27 stars 19 forks source link

Misfire handling #5

Closed georgiosd closed 8 years ago

georgiosd commented 8 years ago

Hey guys,

I can't, for the life of me, get a job executed after it has gone past the fire time. It seems to me that the misfire handling parameter is not used when the scheduler is fetching jobs.

Thoughts?

Thanks, Georgios

iftahbe commented 8 years ago

Hi Georgios, I'd like to investigate this issue. Can you elaborate on what you're trying to do, how to reproduce it? It would be great if you could write a test which demonstrates the failure.

iftahbe commented 8 years ago

Tom, I think it's not relevant. The problem is in the quartz scheduling logic. Implementing the job store wasn't trivial and required diving into quartz scheduling logic and I might have missed a few things (including misfire handling).

tomsawdayee commented 8 years ago

My bad. I seem to be having this issue as well. Might be an issue with AcquireNextTriggers method.

georgiosd commented 8 years ago

Here's a test!

public class QuartzTests
    {
        public class TestJob : IJob
        {
            public void Execute(IJobExecutionContext context)
            {

            }
        }

        public class JobListener : IJobListener
        {
            public void JobToBeExecuted(IJobExecutionContext context)
            {

            }

            public void JobExecutionVetoed(IJobExecutionContext context)
            {

            }

            public void JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException)
            {
                WasExecuted = true;
            }

            public bool WasExecuted { get; private set; }

            public string Name => "JobListener";
        }

        private readonly JobListener listener = new JobListener();

        private readonly ITrigger trigger = TriggerBuilder.Create()
            .StartAt(DateTime.Now - TimeSpan.FromHours(1))
            .WithSimpleSchedule(s => s.WithMisfireHandlingInstructionFireNow())
            .Build();

        private readonly IJobDetail job = JobBuilder.Create<TestJob>()
            .Build();

        private void ScheduleTestJobAndWaitForExecution(IScheduler scheduler)
        {
            scheduler.ListenerManager.AddJobListener(listener);
            scheduler.Start();
            scheduler.ScheduleJob(job, trigger);
            while (!scheduler.CheckExists(job.Key)) ;
            scheduler.Shutdown(true);
        }

        [Fact]
        public void InMemory()
        {
            var scheduler = StdSchedulerFactory.GetDefaultScheduler();

            ScheduleTestJobAndWaitForExecution(scheduler);

            Assert.True(listener.WasExecuted);
        }

        [Fact]
        public void InRavenDB()
        {
            NameValueCollection properties = new NameValueCollection
            {
                // Normal scheduler properties
                ["quartz.scheduler.instanceName"] = "TestScheduler",
                ["quartz.scheduler.instanceId"] = "instance_one",
                // RavenDB JobStore property
                ["quartz.jobStore.type"] = "Quartz.Impl.RavenDB.RavenJobStore, Quartz.Impl.RavenDB"
            };

            ISchedulerFactory sf = new StdSchedulerFactory(properties);
            IScheduler scheduler = sf.GetScheduler();

            ScheduleTestJobAndWaitForExecution(scheduler);

            Assert.True(listener.WasExecuted);
        }

    }
georgiosd commented 8 years ago

Seems to me the secret sauce is at the method called at https://github.com/quartznet/quartznet/blob/master/src/Quartz/Impl/AdoJobStore/JobStoreSupport.cs#L2422

iftahbe commented 8 years ago

Thanks a lot! I'm on it..

georgiosd commented 8 years ago

How's it going? :)

iftahbe commented 8 years ago

Sorry got a little delayed because of work stuff :) I'll give it another try tomorrow.

iftahbe commented 8 years ago

Problem was that the trigger's NextFireTimeUtc field wasn't being updated in case of a misfire. Please let me know if your problem is solved...

georgiosd commented 8 years ago

Thanks! Are you pushing a new nuget?

iftahbe commented 8 years ago

I did. Version 1.0.4 has the fix.

georgiosd commented 8 years ago

Sweet! I presume the test passes so no need to try it myself. Thank you for your attention to this!