ravendb / quartznet-RavenDB

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

Issue after data recovering #26

Open mouradchama opened 1 year ago

mouradchama commented 1 year ago

Hi,

I try to use Quartz.Net with RavenDb, I'm blocking the concurrent execution with the attribute (DisallowConcurrentExecution).

When the scheduler start the code start to recover the data from the data base (RavenJobStore.Util.cs, method : RecoverSchedulerData), if the trigger state is Acquired or Blocked we restore the default state which is Waiting.

                var queryResult = await session
                    .Query<Trigger>()
                    .Include(t => t.Scheduler) // pre-load scheduler
                    .Where(t =>
                        t.Scheduler == InstanceName && (t.State == InternalTriggerState.Acquired ||
                                                        t.State == InternalTriggerState.Blocked)
                    )
                    .ToListAsync(cancellationToken);
                foreach (var trigger in queryResult)
                {
                    var triggerToUpdate = await session.LoadAsync<Trigger>(trigger.Key, cancellationToken);
                    triggerToUpdate.State = InternalTriggerState.Waiting; //    <=== here
                }

There is a big issue with this approach. You will not the see the issue if you have a job with CRON expression, specially when the next trigger is not at the startup of your application.

If the trigger is at the startup and your start 2 instances at the same time, the first instance will trigger and lock the the state, but the second instance will restore the state (from Blocked to Wating) and trigger for a second time the job.

Is there any thing that we can do with this? why not simply removing this recovery step?

Thank you in avance. Regards, Mourad