Quartz.NET, Recur Every x Weeks

I need to implement the following script using Quartz.NET:

Repeat every week (s) on:
Sunday and / or Monday, Tuesday, Wednesday, Thursday, Friday, Saturday ...

So, for example, I could choose: Monday and Thursday, and repeat every 2 weeks, is this possible?

I understand that the way to go can use Cron Expressions, but so far Iโ€™m out of luck with Recur Every X Weeks

Thanks!

+7
source share
4 answers

This is the solution I used ...

When there is no return, I use the cron trigger and select the days and run every week.

eg. 0 0 * * 1,2,3

when there is a re-check for each selected day, I add a SimpleTrigger, basically the start date is the day of the week, and then I calculate the repeat by multiplying the repeat by 7

So, I will end up with one simple trigger for every day.

I hope this helps someone else!

+6
source

Is Quartz.Net 2.0 launched and an option for you? It has not yet been officially released, but a new type of trigger has appeared in it that solves your problem. He called the calendar interval trigger. Basically you define it in the same way as you described in your question. You set interval 2 and the interval block to weeks, and it fires every 2 weeks. I wrote a post with a description here . You can access the source code documentation here .

+7
source

This is an add-on trigger, you can control three triggers;

  • trigger 2 weeks sample cron: "0 0 0 1 *"
  • trigger 2 weeks sample cron: "0 0 0 15 *"
  • trigger trigger selected days sample cron: "0 0 0? * SUN-SAT"

the first trigger will create 3. the second trigger will be deleted 3.

Good luck.

+3
source

Unfortunately, Quartz.Net 2 is not released, is undocumented and introduces change shifts.

As Aureliano and Bongo say, a combination of triggers can help, but I donโ€™t quite understand their respective solutions.

My solution is to wrap CronTrigger and skip unwanted events:

var ct = new CronTrigger(); ct.CronExpression = new CronExpression( string.Format("0 {0} {1} ? * {2} *", minuteOfHour, hourOfDay, daysList)); ct = new WeeklyTriggerWrapper(ct, 2); public class WeeklyTriggerWrapper : CronTrigger { public CronTrigger Trigger { get; private set; } public int WeekInterval { get; private set; } public DateTime? LastFireDateTime { get; private set; } public WeeklyTriggerWrapper(CronTrigger trigger, int weekInterval) { Trigger = trigger; WeekInterval = weekInterval; } public override DateTime? ComputeFirstFireTimeUtc(ICalendar cal) { return Trigger.ComputeFirstFireTimeUtc(cal); } public override DateTime? GetFireTimeAfter(DateTime? afterTimeUtc) { var result = Trigger.GetFireTimeAfter(afterTimeUtc); if (result.HasValue) { DateTime reference = StartTimeUtc; if (LastFireDateTime.HasValue && LastFireDateTime.Value > reference) reference = LastFireDateTime.Value; reference = reference.AddDays(7 * WeekInterval); while (result.HasValue && result.Value < reference) result = Trigger.GetFireTimeAfter(result.Value); } LastFireDateTime = result; return result; } // TODO: handle events... } 
0
source

All Articles