How do i run a cron schedule a DAG in airflow so that it runs at 10:30 cst on first of one month and 10 cst on the other days of the month ? I have tr - cron

Like i have tried multiple combinations in cron guru but havent found any possible solution.
I have these two expressions to combine
30 10 1 * *
10 12 2-31 * *

You cannot implement this in one cron expression, you have two options to do that:
easy one: create two copy of the dag, each one with its own cron expression
hard one: create a Timetable and register it in an Airflow plugin, then you can use pass it to your dag as timetable argument instead of schedule_interval.
In this timetable, you can implement two methods:
next_dagrun_info: The scheduler uses this to learn the timetable’s regular schedule
infer_manual_data_interval: When a DAG run is manually triggered (from the web UI, for example), the scheduler uses this method to learn about how to reverse-infer the out-of-schedule run’s data interval (not mandatory).

Related

Combine two cron-scheduling intervals in a single DAG

Rewrite of the question:
Using airflow, I would like to schedule a process to run every two hours from 2 till 10 am and a single time at 22:30. The schedule_interval parameter accepts a cron-expression, but it is not possible to define a single cron-expression to achieve the above scheduling. Currently, I did:
dag = DAG(process_name, schedule_interval='30 2,4,6,8,10,12,14,16,18,20,22,23 * * *', default_args=default_args)
But this will execute the process every 30 minutes past the hour, and this every 2 hours from 2 till 23.
Is there a way I can combine two cron-schedules in Airflow?
0 2-10/2 * * *
30 22 * * *
Original question:
I have 2,4,6,10,12,14,16,18,20,22 00 * *
I need to have 23, 30 in my schedule, but I don't want 2-22 to be run at the 30 min interval.
So, I realized, it is not possible!
You can't use two cron expressions for the same DAG (Might change in the future if PR: Add support for multiple cron expressions in schedule_interval is accepted)
Starting Airflow >=2.2.0:
It is possible to get custom time based triggering using custom Timetable by customizing the DAG scheduling to match what you expect.
To do so you need to define the scheduling logic by implementing next_dagrun_info and infer_manual_data_interval functions - Airflow will leverage this logic to schedule your DAG.
You can view an example can be found here.

Cron Expression: starting a job after completion of another job

I have to perform 2 jobs - A and B. Job 'A' is to be prformed at 9:00 am of every weekday. I dont know the duration for job 'A' though, duration may vary.
Also I want to perform job 'B' after 3mins of completion of job 'A'.
Can anyone suggest the cron expression for this please.
Assuming you are trying to run the second job three minutes after the first job completes, let's say you have Job A which involves calling /home/user/job_a.sh and then once that completes, you want to run /home/user/job_b.sh. Instead of trying to set up two different Cron jobs, you could just make a separate script, say job_c.sh. And all job_c.sh does is run Job A, wait three minutes and then run Job B.
Basically, rather than calling two Cron Jobs and trying to sort out timing for both of them, you can just establish one Cron Job which runs both jobs.
On the other hand, if you want to run the second job three minutes after the first one starts then you might as well create two Cron Jobs with three minutes between them which would look something like this:
00 9 * * 1-5 /home/user/job_a.sh
03 9 * * 1-5 /home/user/job_b.sh

How to combine 2 different cron expressions?

i need to schedule a process on UiPath Orchestrator. The process should triggered at 9AM, 10.30AM, 12PM, 14PM, 15PM and 16PM in weekdays.
If there was no 10.30AM i could apply the schedule in one cron expression.
But now, i can only cover this issue with 2 cron expressions.
How to i combine this two cron expressions as one.
0 0 9,12,14,15,16 ? * MON-FRI *
0 30 10 ? * MON-FRI *
Unfortunately there is no way to combine two Cron-Expressions under one command.
Alternatively, what you can do is to create two Schedules for the same Process one per your expressions. Check image below:
Keep in mind that you need to set different names as each Schedule name needs to be unique
Hope these information would be useful.

Quartz cron expression to run job with interval of minutes

I wonder if it is possible to write an cron expression with several conditions:
Job should be run with given interval in minutes. For example with interval 42 minutes the fire times would be 10:00, 10:42, 11:24, 12:06 and etc.
If the current minute does not end with 0 (e.g. 10:28,10:29), then cron first fire time should be 10:30. So it means that first fire time should have "round" minutes.
I hope that you understand these conditions. Is it possible to describe them with quartz cron?
You can use job trigger like described below in Quartz.net 3.0:
var jobTrigger = TriggerBuilder.Create()
.StartNow()
.WithSimpleSchedule(s => s
.WithIntervalInMinutes(42)
.RepeatForever())
.Build();
And you can restart app at first round time, so it will fire first time at the same time only.
I usually use http://www.cronmaker.com/ to generate my cron expressions. And if you try the every 42 mins option you'll get the following expression: " 0 0/42 * 1/1 * ? *". As for the "round" minutes thing, you can try this when building your trigger:
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity(JobTrigger, JobGroup)
.WithCronSchedule(CroneExpression)
.StartAt(new DateTimeOffset(DateTime.Now,
TimeSpan.FromMinutes(DateTime.Now.Minute % 10)))
.Build();
It is not possible, see for explanation and similar issue: Quartz.net - Repeat on day n, of every m months?
it is also not possible by Cron expressions. To do this, you would need to apply some complex logic, use some operator that is not present in evaluators. Why do you need this? Would you like to combine those 2 requirements and create single complex pattern?

Cron Expression for running a job at different intervals at different hours

I want to schedule a job to run every 5 mins during morning (4-8)AM and evening (7-11)PM and rest of the day every 30 minutes, how can i write a cron expression for it?
I can do this by using two cron expressions for scheduling, but can i do the task using one expression?
No, I believe you'll need 2 separate CRON expressions.
The timings (or intensity) are different, so you need different expressions.
So something like the following is needed:
*/5 4-7,19-22 * * * your/command
*/30 0-3,8-18,23 * * * your/command

Resources