Question

I successfully set up a two node scheduler cluster to execute a job. I used OracleODP-20 connector to connect to oracle database, where the QRTZ_ tables are created. Job scheduled is also getting executed by either one of the scheduler in every 5 minutes.

This is my trigger:

ISimpleTrigger trigger = (ISimpleTrigger) TriggerBuilder.Create()
                                                          .WithIdentity("mytrigger", schedId)
                                                          .StartNow()
                                                          .WithSimpleSchedule(x => x.WithRepeatCount(-1).WithInterval(TimeSpan.FromMinutes(5)))
                                                          .Build();

But there is one problem.When the scheduler is starting up, it always executes the job, even if time for the next execution of the job is not now.

e.g.,

  • Job1 will run every 5 minutes.
  • Scheduler1 started at 10.00 and it executes the Job1 and is waiting for 10.05 for the next execution.
  • When i start Scheduler2 at 10.01, even though Job1 was supposed to run next 10.05, it will run at 10.01

After this initial run, the Job1 will be executed by only one of the scheduler and it goes fine.

But I am not sure how to tell the Scheduler2 that, the job is already executed by Scheduler1 and don't execute it till the next fire time.

Any help in this?

Was it helpful?

Solution

You should create this trigger only once, not on each node's startup.

I see that you trigger contains instance's id as group which is very suspicious. It seems that you are having trigger for each node when you should have just single trigger created if it does not exist yet.

Easier to read trigger definition would thus be:

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

var jobDetail = JobBuilder.Create<NoOpJob>().Build();

var key = new TriggerKey("trigger-name", "trigger-group");
if (sched.GetTrigger(key) == null)
{
    ITrigger trigger = TriggerBuilder.Create()
        .WithIdentity(key)
        .StartAt(DateBuilder.EvenHourDate(DateTimeOffset.UtcNow))
        .WithSimpleSchedule(x => x
            .RepeatForever()
            .WithInterval(TimeSpan.FromMinutes(5)))
        .Build();

    sched.ScheduleJob(jobDetail, trigger);
}

I threw in the even hour date to get more precise timings instead of 'from now every 5 minutes'. You could easily get this with cron trigger too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top