Quartz.NET, Recur Every x Weeks

前端 未结 4 1247
灰色年华
灰色年华 2020-12-06 07:29

i need to implement the following scenario using Quartz.NET:

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

相关标签:
4条回答
  • 2020-12-06 07:53

    It is a complext trigger, you can manage by 3 triggers;

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

    first trigger will create 3. second trigger will remove 3.

    Good luck.

    0 讨论(0)
  • 2020-12-06 07:58

    Unfortunately Quartz.Net 2 is unreleased, undocumented, and introduces breaking changes.

    Like Aureliano and Bongo say, a combination of triggers might help but I do not quite understand their respective solutions.

    My solution is to wrap a CronTrigger and skip the 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 讨论(0)
  • 2020-12-06 07:59

    Is running Quartz.Net 2.0 and option for you? It has not been officially released yet, but in it there is a new trigger type that solves your problem. It's called the calendar interval trigger. Basically you define it just as you described in your question. You set the interval to 2 and the interval unit to weeks, and it fires every 2 weeks. I've written a post describing it here. You can access the source code documentation here.

    0 讨论(0)
  • 2020-12-06 08:01

    This is the solution that i used...

    When there is no recurence i use a cron trigger and select the days and make it run every week

    E.G. 0 0 * * 1,2,3

    when there is recurence for each selected day i add a SimpleTrigger, bassically the start date is the day of the week, and then i calculate the recurrence by multiplying the recurence for 7

    So i will end up wit one simpletrigger for each day.

    I hope this helps someone else!

    0 讨论(0)
提交回复
热议问题