aminexplo
aminexplo

Reputation: 362

How to make Quartz.net execute only remaining repeat count of a job?

Let's say we have a job and corresponding trigger configured as follows:

var jobKey = new JobKey(typeof(HelloJob).Name, typeof(HelloJob).Name);
        IJobDetail job = JobBuilder.Create<HelloJob>()
            .WithIdentity(jobKey)
            .RequestRecovery()
            .Build();

        ITrigger trigger = TriggerBuilder.Create()
          .WithIdentity(string.Format("{0}Trigger", typeof(HelloJob).Name), typeof(HelloJob).Name)
          .StartAt(DateBuilder.FutureDate(1, IntervalUnit.Second))
          .WithSimpleSchedule(x => x
              .WithIntervalInSeconds(1)
              .WithRepeatCount(9))
          .Build();

using "quartz.jobStore.type = Quartz.Impl.AdoJobStore.JobStoreTX, Quartz".

Well I expect the job execute 10 times in normal situation and of course it does. The problem is that I want the job to continue the remaining repeat count if any stop occurres during its life cycle.

For example I stop the program when the fifth execution of the job is done and the next time that I run the program I want to execute the job only five times more but it is executed 10 times.

How should I configure Scheduler, Job and Trigger in order to do this?

Upvotes: 1

Views: 1807

Answers (2)

mfatih
mfatih

Reputation: 476

Make sure the database objects are created and configure jobstores.

Look this example. It can help you.

https://github.com/Leftyx/QuartzNetAdoJobStoreSQLite

Upvotes: 1

Set
Set

Reputation: 49809

You may do this by storing remaining count between program execution:

  • on program "Stop" save remaining count in external storage (file, db, etc..) The best way actualy is to do this after each job execution.
  • on program "Start" read value from storage and use it in .WithRepeatCount() instead of constant.

Upvotes: 1

Related Questions