how to combine cron expressions? - cron

I have a task which requires to be executed every Thursday, but if the day is a speical holiday such as Christmas, the task should be changed to another day( wednesday or friday), so I get is a general expression plus some special dates, how should I combine all these expressions in one cron expression to be used in Quartz? Thanks.

I don't think you can do it with just one expression since there's no way to specify holidays.
But you can define two triggers T1 and T2 and a "smarter" Job J1:
A trigger T1 on J1 that fires every Thursday using the HolidayCalendar that will exclude arbitrary days from triggering the job execution
//Define an HolidayCalendar and add it to Scheduler
HolidayCalendar cal = new HolidayCalendar();
cal.addExcludedDate( someDate );
sched.addCalendar("myHolidays", cal, false);
//Create the trigger T1 to use the holydayCalendar
...
triggerT1.setCalendarName("myHolidays");
//schedule job with trigger T1
Define a Trigger T2 on J1 that fires on the alterantive day you choose (Wednesday or Friday).
The job J1 must decide whether start executing its business logic or not using the HolydayCalendar. There are many easy ways to do this, for exemple:
//Get Holyday calendar from Scheduler instance
HolidayCalendar cal = sched.getCalendar("myHolidays");
//Decide here if execute or not
if (today is an "alternative" day && lastThursday is an holiday) {
//Job real business logic
}
See this Quartz tutorial for more exemple about Quartz Calendars.
Also you can use this tool to build Quartz-compatible Cron expressions.
Hope this helps!

Related

Logic Apps- Compare the modified date in the trigger body with the utcnow() and process only if it is within 1 day of utcnow()

I have a use case where we are triggering the Logic App only when a record is modified in Salesforce. But the issue is that in testing we had disabled the Logic App for couple of days and when I enabled them back, some 35K records triggered our Logic Apps that overwhelmed the system.
So I am trying to add a trigger condition in my Logic App that would compare the last modified date from the trigger body with utcnow() and trigger the Logic App only if the last modified was within 1 day of UTC time. I tried a couple of conditions but nothing is working.
I tried the hardcoded value like below and it works.
#greater(triggerBody()?['LastModifiedDate'],'2022-02-02T17:25:49Z')
I am trying to modify this like but it is not working.
#greater(triggerBody()?['LastModifiedDate'],utcNow()-1)
#greater(equals(formatDateTime(triggerBody()?['LastModifiedDate'],'yyyy-MM-dd')),utcnow()-1)
I am new to Logic Apps and this kind of scenarios, so any help is appreciated!
You'll need to adjust your logic for adding/minusing the days from a date, you need to use the addDays function.
This worked for me ...
#greater(formatDateTime(triggerBody()?['LastModifiedDate'], 'yyyy-MM-dd'), formatDateTime(addDays(utcNow(), -1), 'yyyy-MM-dd'))
... or this if you want to simply the formatting, they'll both do the same thing ...
#greater(triggerBody()?['LastModifiedDate'], addDays(utcNow(), -1))

Is it possible to run a Job using Cron expression just for the current day in Java using Quartz api

Say for example i want to run a job for a day at "3:22, 4:22, 6:22, 10:22, 18:22, 21.22" on these times just for the current day.
How could we write a cron expresion for it or using other Simple Trigger of Quartz API.
Using cron trigger you can set endAt() to stop trigger at the end of the day
trigger = newTrigger()
.withSchedule(cronSchedule(0 22 3,4,6,10,18,21 ? * * *))
.endAt(TRIGGER_END_TIME)
.forJob(myJobKey)
.build();

Schedule by specific date and time in agendajs

I am using agendajs to schedule the jobs.
In docs, it is shown how to schedule jobs like
agenda.schedule('in 2 minutes', 'some jobs')
How can I schedule the job in specific date and time.
I tried to use like below but didn't work
agenda.schedule('at 2018-7-24 09:47')
Any help is appreciated. Thanks
As per the docs:
schedule(when, name, [data], [cb])
Schedules a job to run name once at a given time. when can be a Date
or a String such as tomorrow at 5pm.
So something like:
agenda.schedule(new Date(), 'MyJob')
Should do it.
On the end of the day all this is doing is crating the job and setting the nextRunAt ISODate for that job record.

How to run cron trigger immediately?

What i should do to run CRON trigger to run now once and follow the expression for Example
trigger that simply fires every 5 minutes.
I have gone through the below post
https://groups.google.com/forum/#!topic/quartznet/GAv10E4TJ50
If you want to make sure your job is run immediately you can set start
time to one day before DateTime.Now, so you change your code to:
CronTrigger trigger = new CronTrigger("trig", "grp", "job", "grp",
DateTime.Now.AddDays(-1), null, "0 0 0 * * ?");
But does the above work for any scenario. like
0 0/5 14 * * ? Fire every 5 minutes starting at 2pm and ending at 2:55pm, every day (if current time is 2:15pm)
Thanks,
Kusuma
What about creating a CRON trigger with a temporary schedule to "trigger the job now" (e.g. 0 * * * * ?) and implementing a JobListener that would update the CRON trigger's expression once the job has been executed for the first time? You can use, for example, a job data map parameter to distinguish the first and subsequent executions in the listener.
If you do not insist that it must always be the same CRON trigger that "executes the job now" and then continues to execute it regularly, then you can use one of the triggerJob methods that both create a temporary on-off SimpleTrigger that is used to execute the job now.
The time at which the trigger's scheduling should start. May or may not be the first actual fire time of the trigger, depending upon the type of trigger and the settings of the other properties of the trigger. However the first actual first time will not be before this date.
So i could able to run immediately by setStartTime

Cron Expression to not run on a particular date

I have a cron job that Runs using Quartz.NET.
<cron-expression>0 5 * * * ?</cron-expression>
It runs every 5 minutes as you can see above, but I want it to not run from 5pm on 30/Dec/2013 to 7am on 01/Jan/2014..
Is this possible does anyone know?
Thanks
Neil
Exclusion are most easily made with calendars. With calendar you can exclude a set of days, weekdays, time of days or a cron expression from schedule. Calendar is checked always checked after fire time has been determined (is this OK to run?).
You could either chain multiple calendars because you have nasty complexity of passing the year, multiple days and time range on top of that. More readable and understandable solution could be to implement your own fixed time range calendar (implementing ICalendar) that takes end and start time and excludes them.
You could naturally also make your first cron trigger end at 2013-12-30 17:00 and add a new trigger that starts at 2014-01-01 07:00.
Here's a trivial sample utilizing CronCalendar:
var calendar = new CronCalendar("0 * 1 * * ?");
scheduler.AddCalendar("myCalendar", calendar, replace: false, updateTriggers: false);
var trigger = TriggerBuilder.Create()
.WithCronSchedule("0 5 * * * ?")
.ModifiedByCalendar("myCalendar")
.Build();
scheduler.ScheduleJob(trigger);

Resources