开发者

Quartz.Net - Every 3 months

开发者 https://www.devze.com 2023-04-02 10:27 出处:网络
I\'m trying to call something every 3 months (quarterly) in Quartz.NET (using both stable and latest version 2 which is beta with same results).

I'm trying to call something every 3 months (quarterly) in Quartz.NET (using both stable and latest version 2 which is beta with same results).

I create cron trigger with 0 30 8 3 */3 ? * to be called every 3 months at 8.30am on third of the month it occurs.

So technically since its 2 of September today I would expect it to trigger tomorrow. However it next 开发者_如何学Gorun time shows as being next month. Why is that so?

Updated: As per answers I got I created following method - could be useful for someone:

public static string CalculateMonthsWithInterval(int startMonth, int interval)
        {
            var months = new List<string>();
            var monthNames = new [] {"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};

            var monthSelector = startMonth % interval;

            for (var i = 0; i < 12; i++)
            {
                if (i % interval == monthSelector)
                {
                    months.Add(monthNames[i]);
                }
            }

            return string.Join(",", months.ToArray());
        }

Ps: I didn't use indexes for months because for some reason it wasn't working well with my Quartz (v2 BETA). Also its easier to read in DB level.

Example call - Every 3 months based on startDate:

var cronMonths = CronUtils.CalculateMonthsWithInterval((startDate.Month - 1), 3);


Well I think that's because the scheduler will verify which month can be divided by 3, since all month in Quartz are based 0 (according to: http://www.quartz-scheduler.org/documentation/quartz-2.x/tutorials/tutorial-lesson-06), the month that will be scheduled will be january, april, july and october.

0 mod 3 = 0 -> JAN
1 mod 3 = 1 -> FEB
...
8 mod 3 = 2 -> SEP
9 mod 3 = 0 -> OCT

The Quartz scheduler will analyse your cron expression and keep only those where their modulus 3 equals to 0.

If you want it to be 1 month before that (march, june, september and october) you will have to set it to:

0 30 8 3 MAR,JUN,SEP,DEC ? *


A good page to create cron expressions: http://www.cronmaker.com/

Cron format:

0 0 12 1 1/3 ? *

Executes every:

1. Saturday, April 1, 2017 12:00 PM
2.  Saturday, July 1, 2017 12:00 PM
3.  Sunday, October 1, 2017 12:00 PM
4.  Monday, January 1, 2018 12:00 PM
5.  Sunday, April 1, 2018 12:00 PM
0

精彩评论

暂无评论...
验证码 换一张
取 消