0

i need to trigger a job at particular time on particular weekdays. these weekdays are also dynamic and should be fetched from db.Also the job should trigger or repeat itself for particular count. let us say a Job j should trigger on every mon, wed, fri, for repeat count 15 i.e for 3 * 5 = 15 so it should trigger for next 3 weeks.

i tried using cronexpression as below but could not find how to stop a job from triggering after certain count.

   StringBuilder cronString = new StringBuilder("0 0 12 ? * ");
            if (plandata.Sunday == true)
                cronString.Append("SUN,");
            if (plandata.Monday == true)
                cronString.Append("MON,");
            if (plandata.Tuesday == true)
                cronString.Append("TUE,");
            if (plandata.Wednesday == true)
                cronString.Append("WED,");
            if (plandata.Thursday == true)
                cronString.Append("THU");
            if (plandata.Friday == true)
                cronString.Append("FRI,");
            if (plandata.Saturday == true)
                cronString.Append("SAT,");
            cronString.Append(" *");

            IJobDetail job = JobBuilder.Create<Demo>()
                .Build();
            ITrigger trigger = TriggerBuilder.Create()
                   //.StartAt((DateTime)plandata.StartDate)
                   .StartNow()
                   .WithPriority(1)
                   .WithCronSchedule(cronString.ToString())
                   .Build();

Please suggest me something so that i can achieve what i need.

Deepak Verma
  • 617
  • 5
  • 18
  • You can set a [RepeatCount](https://www.quartz-scheduler.net/documentation/quartz-3.x/tutorial/simpletriggers.html) and a [Calendar](https://www.quartz-scheduler.net/documentation/quartz-3.x/tutorial/more-about-triggers.html) to achieve that. – Rabban Jan 29 '19 at 10:29

2 Answers2

0

A cron schedule doesn't have the concept of only triggering a certain number of times. Maybe you'll have more luck with the other scheduling methods.

EndAt may be more helpful and looks like it's available regardless of scheduler.

Jesper
  • 7,477
  • 4
  • 40
  • 57
0

You can create Simple Trigger for each day by using ISimpleTrigger. You don't need to use any cron trigger for this work. First of all, you should get a matching date.

   DateTime today = DateTime.Today;
   // The (... + 7) % 7 ensures we end up with a value in the range [0, 6]
   int daysUntilMonday = ((int)DayOfWeek.Monday - (int)today.DayOfWeek + 7) % 7;
   DateTime nextMonday = today.AddDays(daysUntilMonday);

or

    DateTime today = DateTime.Today;
   // The (... + 7) % 7 ensures we end up with a value in the range [0, 6]
   int daysUntilWednesday = ((int)DayOfWeek.Wednesday - (int)today.DayOfWeek + 7) % 7;
   DateTime nextWednesday = today.AddDays(daysUntilWednesday);

Then you can create a trigger like this.

   ISimpleTrigger trigger = (ISimpleTrigger)TriggerBuilder.Create()
      .WithIdentity("DEFAULT")
      .StartAt(nextMonday)   // or .StartAt(nextWednesday)
      .WithSimpleSchedule(x => x
           .WithIntervalInHours(168) // 1 week = 168h
           .WithRepeatCount(3))   // Repeat on 3 weeks
           .Build();

This simple trigger will be deleted automatically, after completing its repetition.

For more details - https://www.quartz-scheduler.net/documentation/quartz-2.x/tutorial/simpletriggers.html

chan
  • 31
  • 9