Hangfire Cron expressions are not valid

2019-09-14 17:58发布

I am using Hangfire and I want to describe different scenarios for my RecurringJobs. But I am not being able to achieve what I am looking for, and if CRON is already limited, the CRON used by Hangfire is yet more.

I went on reading Hangfire documentation and I find a like to https://en.wikipedia.org/wiki/Cron#CRON_expression for more complex expressions then the ones supported by default on Hangfire. But they are not even compatible, for instance, Hangfire only has minutes, hour, month, day, days of the week, but if I use the L or the ? on the day like it says on the documentation it does not work. I have this error the following error for this expression 16 14 L ? ?:

InnerException = {"'L' is not a valid [Day] crontab field value. It must be a numeric value between 1 and 31 (all inclusive)."}

CRON from Hangfire has the following method: Monthly(int day); What happens If I choose for instance 31? It will still run on months like February or April for instance at the last day of each month? Or do I need to do something extra to achieve it?

That way what is happening? I do not seem able to define the condition of the day chosen by the user is 31, to run the background jobs always on the last day of the month. And I don't even talk about days 29 or 30 which are also special causes and which I would use always the last day of the month to process the background job.

I though of using the Month method from Hangfire.CRON but I don't think it will treat the days 29,30 and 31 the way I want.

Do you confirm that Hangfire Cron does not use the Cron expressions that are referenced by documentation and if there is any way to achieve what I am looking for? Also, any suggested tutorial or something to help me out? I have been reading https://github.com/atifaziz/NCrontab which I think it is the one Hangfire uses, but it does not help that much.

标签: cron hangfire
1条回答
爷的心禁止访问
2楼-- · 2019-09-14 18:32

You are right about NCrontab. Hangfire uses it, so you should ensure your cron expression is supported by this library. Two simple options to do it:

Cron.Monthly(31) is translated to 0 0 31 * * and job would be triggered only if current month has 31 days.

To run the background job always on the last day of the month, add three separate jobs:

0 0 30 4,6,9,11        *
0 0 31 1,3,5,7,8,10,12 *
0 0 28 2               *

Cron job to run on the last day of the month

查看更多
登录 后发表回答