Does hangfire support every x years? - cron

I'm using Hangfire 1.7.31, as I can see it only support 6 parts of cron.
However, my requirement is to trigger a job every two years.
Can hangfire be used for this scenario?

You are able to set a cronexpression to fire at a specific date every year.
As per https://crontab.guru/every-year you can use following cron expression:
0 0 1 1 *, whichs translates to:
At 00:00 on day-of-month 1 in January.

Related

Azure function not triggering on specified time

I've been looking around for a solution to an issue I am having. I looked at various posts on the same topic but none of them solved my issue, hence creating a new thread for this, so kindly read it once before marking it as duplicate.
I've created a C# HTTP Timer Trigger Function on Azure and specified CRON timing, but it is behaving weirdly.
This is my function.js
My cron expression: 0 */60 15-3 * * 1,2,3,4,5,6
My function will trigger every 60 minutes between 03:00 PM and 03:00 AM, only on Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday
But to my surprise it is triggering from 3:00 AM to 3:00 PM, and I am unable to understand why.
I followed the accepted answer of Azure function is not triggering on scheduled time post but still it is behaving the same.
Note: I have not enabled internal logging but I am maintaining a separate log in a different folder on Azure.
Edit 1:
I just noticed that, if my function has been scheduled to run within the day i.e. if hour has been set anywhere between 0-24, it works properly but if hour has been set in such a way that it goes over the day i.e. 15-3 (in my case) its behavior changes completely and it runs from 3-15 and not the other way around.
*/60make non sense. Use 0
And following cron logic make two records like:
0 0 0-3 * * 1,2,3,4,5,6
0 0 15-23 * * 1,2,3,4,5,6
If you want to make them as only one record you can try this:
0 0 0-3,15-23 * * 1,2,3,4,5,6
If this do not work you should use record like:
0 0 0,1,2,3,15,16,17,18,19,20,21,22,23 * * 1,2,3,4,5,6

Complex MuleSoft Cron Schedule

I'm trying to schedule an app to run every 5 minutes, M-F from 6am-6pm, and every 2 hours, M-F from 6pm-6am. The fixed poll frequency doesn't allow this level of scheduling, so I'm trying to use a cron scheduler.
I set the time zone to America/Chicago and the 5M expression to 0 0/5 6-18 ? * 2-6, and I set the 2H expression to 0 0/120 18-23,0-6 ? * 2-6.
According to both Cron documentation and MuleSoft documentation, this should be setup correctly and should work, and it does work locally. When I publish this to our VPC (US-EAST), I found that the 2H scheduler was working during the day until about 4PM (Central time), so I changed the expressions to this:
5M 0 0/5 0-12 ? * 2-6
2H 0 0/120 12-23 ? * 2-6
Now my 5M scheduler started at 2:15AM and ran until 7AM.
How can I setup two central timezone cron schedules to run 6am-6pm M-F every 5 minutes and 6pm-6am M-F every 2 hours on a VPC server?
MuleSoft confirmed to me that their VPC servers are UTC based, regardless of waht time zone you place on the cron scheduler/poll connector. While not ideal, that means that I needed to back the schedulers up 6 hours (to get them to central time). I suspect that DST will cause the schedule to shift one direction by an hour.
This is a good resource for learning about Mule Quartz/Cron scheduler.
There is also a Free Online cron scheduler formatter.
For the purpose of my scheduling needs, I ended up needing a total of four cron schedulers
0 0/5 12-23 ? * 2-6 - runs M-F, 6am-5:55pm (central) every five minutes.
0 0 0-11/2 ? * 3-6 - runs T-F UTC, but actually runs 6pm-4am M-F every two hours. You only need to run the app up to 4am since it is every two hours, and the five minute schedule will start promptly at 6am. Two things to note here. First, to properly rune very two hours you need to do /2 on the hour slot, instead of /120 on the minute. The minute slot can only handle values from 0-59. Second, the days are set to start on Tuesday because of the 6 hour difference from UTC to Central. If you were to use hours 0-11 (UTC) on Monday, the app would actually start at 6pm on Sunday central time.
This brings us to the two additional schedules, one to capture 12am-4am on Monday (central), and a second to capture 6pm-12am on Friday (central).
0 0 6-11/2 ? * 2 - runs every two hours between 12am-4am (central) on Monday.
0 0 0-6/2 ? * 7 - runs every two hours between 6pm and 12am (central) on Friday.

Hangfire Cron expressions are not valid

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.
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:
C# Interactive window (as described in NCrontab Readme, or you could use this example )
Online cron visualizer (like https://crontab.guru or http://cron.schlitt.info)
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

Creating Cron Expression with of Days and Months Simultaneously

I simply want to create a cron expression that will execute a job after 'N' number of days. Where N can be any number greater than Zero.
So, It's alright if number is between 1 and 30. For Example Cron Expression to Execute Job after each
25 days at 11 AM will be:
0 0 11 1/25 * ? //'?' can only be specfied for Day-of-Month or Day-of-Week.
but if user exceeds this limit so it means we will have to execute job after 'M' months and 'D' days.
I am unable to understand how I can specify both day and month at the same time. Can anyone make me understand how I can create cron expression for this scenario. You may assume job to be execute after each '65' days
thanks for your time.
The short answer is that cron expressions don't support what you want to do. You'll need to pre-process the user's request and convert it into the appropriate cron expression, or implement your own timing routine, which could use cron behind the scene with some extra logic. Another suggestion is to put some restrictions on the user API that will only allow the user to enter cron friendly times like every month, every week, every 3 months, etc.

Cron Expression to be executed every n weeks

I am trying to write a Cron expression that triggers every n weeks.
I have thought about something like:
0 0 */21 * *
2013-09-01 00:00:00
2013-09-22 00:00:00
2013-10-01 00:00:00
2013-10-22 00:00:00
Per this Cron tester
But it triggers every 1st in addition to the 21st.
Ideas?
If you're using Quartz, then you may be able to accomplish that schedule with a SimpleTrigger instead:
Trigger trigger = newTrigger()
.withIdentity(triggerKey("myTrigger", "myGroup"))
.withSchedule(simpleSchedule()
.repeatHourlyForever(n * 7 * 24))
.startAt(...)
.build();
The '/' syntax specifies the increment during the period and not a repeat interval. Admittedly a subtle and confusing difference.
In this case there is only one available increment (21 days) during the 1 month period. The first number specifies the value to start with, in this case 0. Specifying '*' before the '/' is equivalent to specifying 0. So the job will only fire on the first day and at 21 days.
If you only want to fire the job once a month and not repeatedly then you could use the expression 0 0 21 * *.
If you want a job to trigger at a regular interval then you can use a Quartz SimpleTrigger with a repeatInterval specified.

Resources