does anyone know if the following is possible with a cron schedule or another schedule type ?
Interval in Minutes 5
Daily between 1h30 and 23h00
Every second day
I tried DailyIntervalSchedule which comes close but without the every second day clause. I tried cron as well but failed because of the daily interval between 1h30 and 23h00.
Any help is really apreciated.
Thanks T4E
I hope that the method I'm describing now may help you. You should define two triggers. These triggers should schedule with same job.
// construct a scheduler factory
IScheduler sched = StdSchedulerFactory.GetDefaultScheduler();
// define the job and tie it to our HelloJob class
IJobDetail job = JobBuilder.Create<HelloJob>().StoreDurably()
.WithIdentity("myJob", "group1") // name "myJob", group "group1"
.Build();
sched.AddJob(job,true);
string cron = "0 0/5 2-23 1/2 * ?"; // interval in minutes 5 2h00 and 23h00 every second day
string cron1 = "0 30,35,40,45,50,55 1 1/2 * ?"; // 1h30 every second day
// Trigger the job to run now, and then every 40 seconds
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger", "group1")
.StartNow()
.WithCronSchedule(cron)
.ForJob(job)
.Build();
ITrigger trigger1 = TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.StartNow()
.WithCronSchedule(cron1)
.ForJob(job)
.Build();
// Tell quartz to schedule the job using our trigger
sched.ScheduleJob(trigger);
sched.ScheduleJob(trigger1);
sched.Start();
Related
I am currently working with Quartz Scheduler and I am trying to make a feature, which will be blocking an user if he is inactive for example for more than two days, but unfortunately I didn't work with Quartz Scheduler and I have no idea how to do that.
Well then, you will have a daily cron or an hourly cron (depending on the sensitivity) that checks that value and update the user.
public class CheckForInactiveUsers: IJob
{
public async Task Execute(IJobExecutionContext context)
{
// here you do your business
}
}
IJobDetail job = JobBuilder.Create<CheckForInactiveUsers>()
.WithIdentity("checkForInactiveUsers", "group1")
.Build();
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInHours(1)
.RepeatForever())
.Build();
await scheduler.ScheduleJob(job, trigger);
I would like to execute same job in different scheduled times.
Here is one sample which I wrote:
ISchedulerFactory schedFact = new StdSchedulerFactory();
var setoftrigs = new HashSet<ITrigger>();
for (int x=0; x<=2; x++)
{
setoftrigs.Add(TriggerBuilder.Create().WithIdentity("trigger" + x.ToString(), "group1").WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(21, 44+x)).Build());
}
// get a scheduler
IScheduler sched = schedFact.GetScheduler().GetAwaiter().GetResult();
sched.Start();
// create job
IJobDetail job = JobBuilder.Create<RunJob>()
.WithIdentity("job1", "group1")
.Build();
sched.ScheduleJob(job, setoftrigs, false);
The problem is that in certain time jobs is not executed.
But if I do on this way:
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(21, 49))
.Build();
sched.ScheduleJob(job, trigger);
...it works fine.
What I want to achieve is to have a list of triggers when job should be executed.
How to do that?
This question already has an answer here:
Multiple triggers of same Job Quartz.NET
(1 answer)
Closed 6 years ago.
i know how to fire my one routine every day at specific time of day. here is the code.
IScheduler sched = null;
//construct a scheduler factory
ISchedulerFactory schedFact = new StdSchedulerFactory();
//get a scheduler
sched = schedFact.GetScheduler();
sched.Start();
IJobDetail job = JobBuilder.Create<frmMain>()
.WithIdentity("Job", "group")
.Build();
ITrigger trigger = TriggerBuilder.Create()
.WithDailyTimeIntervalSchedule
(s =>
s.WithIntervalInHours(24)
.OnEveryDay()
.StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(19, 07))
)
.Build();
sched.ScheduleJob(job, trigger);
suppose now i am in scenario that i need to trigger many routine at different time of the day once.
say routine1 should fire at 08:00, routine2 should fire at 15:00 and routine2 should fire at 18:00
now give me suggestion how could i fire different routine at different time of the day. thanks
Like stuartd stated, you need multiple triggers for your job(routine2). I would also suggest to use CronTrigger instead of SimpleTrigger. You can easily create a CronTrigger with:
var trigger1 = TriggerBuilder.Create()
.WithDescription(name)
.WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(15, 0))
.Build();
var trigger2 = TriggerBuilder.Create()
.WithDescription(name)
.WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(18, 0))
.Build();
And then just schedule your job with the 2 triggers:
sched.ScheduleJob(job, trigger1);
sched.ScheduleJob(job, trigger2);
I'm trying to set up 2 and more scheduled tasks to run 4 hours difference since when the first task stared. To do so I'm using Quartz.NET library like following:
ITrigger firstTrigger = TriggerBuilder.Create()
.WithIdentity("Trigger1")
.StartNow()
.WithCronSchedule("0 0 0/4 * * ?")
.ForJob("Job1")
.Build();
IJobDetail secondJob = JobBuilder.Create<StoreAnalyticsUsersUpdate>()
.WithIdentity("Job2")
.Build();
ITrigger secondTrigger = TriggerBuilder.Create()
.WithIdentity("Trigger2")
.StartAt(DateTimeOffset.UtcNow)
.WithCronSchedule("0 0 0/4 * * ?")
.ForJob("Job2")
.Build();
ISchedulerFactory sf = new StdSchedulerFactory();
IScheduler sc = sf.GetScheduler();
sc.ScheduleJob(firstJob, firstTrigger);
sc.ScheduleJob(secondJob, secondTrigger);
sc.Start();
I've wrote this code for testing purposes, just to see if the tasks'll run properly. Turns out neither one of the scheduled tasks after I run my application...
P.S. I call the method at Global.asax class so that's not the issue...
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
JobScheduler.StartCompetitorResearchUpdate(); // this is the method for the above code
}
What am I doing wrong here?
in
.StartAt(DateTimeOffset.UtcNow).WithCronSchedule("0 0 0/4 * * ?").ForJob("Job2").Build();
Test with
.StartAt(DateTimeOffset.UtcNow).WithCronSchedule("0 0 0/4 * * ?").ForJob(secondJob).Build();
Does anyone know is it possible to schedule a job in Quartz.net with variable interval?
For example, I have an interval 10 to 20 and I need to schedule a job, which would be executed every n seconds, where n is random number between 10 and 20 refreshed after each job execution.
Thanks.
Build your trigger for first fire in this way:
var triggerKey = new TriggerKey("simpleTrigger", "simpleTriggerGroup");
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity(triggerKey)
.StartNow()
.Build();
Then, in the Execute method of your job class modify the trigger:
public void Execute(IJobExecutionContext context)
{
Console.WriteLine(string.Format("{0} hello this is a test", DateTime.Now.ToString("r")));
var newInterval = new Random().Next(20, 30);
// retrieve the trigger
var oldTrigger = context.Scheduler.GetTrigger(new TriggerKey("simpleTrigger", "simpleTriggerGroup"));
// obtain a builder that would produce the trigger
var tb = oldTrigger.GetTriggerBuilder();
// update the schedule associated with the builder, and build the new trigger
var newTrigger = tb.StartAt(DateTime.Now.AddSeconds(newInterval)).Build();
context.Scheduler.RescheduleJob(oldTrigger.Key, newTrigger);
Console.WriteLine("Trigger fired... changed interval to {0}", newInterval);
}
The job will be executed in different intervals.