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();
Related
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);
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();
I'm trying to schedule some jobs using Quartz.NET but I cannot get it working. I tried the following code but nothing happened when the specified time was met.
public void Test()
{
ISchedulerFactory schedulerFactory = new StdSchedulerFactory();
IScheduler scheduler = schedulerFactory.GetScheduler();
IJobDetail jobDetail = JobBuilder.Create<SatellitePaymentGenerationJob>()
.WithIdentity("TestJob")
.Build();
ITrigger trigger = TriggerBuilder.Create()
.ForJob(jobDetail)
.WithCronSchedule("0 26 18 * * ?")
.WithIdentity("TestTrigger")
.StartNow()
.Build();
scheduler.ScheduleJob(jobDetail, trigger);
scheduler.Start();
}
UPDATE:
I also tried the following just to make sure it's not the Cron expression that is causing the problem. Did not work for me either...
IJobDetail jobDetail = JobBuilder.Create<SatellitePaymentGenerationJob>()
.WithIdentity("TestJob", "TestGroup")
.Build();
ITrigger trigger = TriggerBuilder.Create()
.ForJob(jobDetail)
.WithSimpleSchedule(x=> x.RepeatForever().WithIntervalInSeconds(10).WithMisfireHandlingInstructionFireNow())
.StartAt(new DateTimeOffset(DateTime.UtcNow.AddSeconds(10)))
.WithIdentity("TestTrigger", "TestGroup")
.Build();
scheduler.ScheduleJob(jobDetail, trigger);
scheduler.Start();
Console.WriteLine(DateTime.UtcNow.ToLongTimeString());
Console.WriteLine(trigger.GetNextFireTimeUtc());
Note that trigger.GetNextFireTimeUtc() returns a valid time value, but the job never gets triggered.
Where did I go wrong?
Everything is ok with your sample code, maybe the Execute method from SatellitePaymentGenerationJob implementation is wrong, or the job is executed but not in the expected time. In the current shape it will be fired at 18:26 every day, is that what you want?
Compare with my code (working):
class Program
{
static void Main(string[] args)
{
Test();
}
public static void Test()
{
ISchedulerFactory schedulerFactory = new StdSchedulerFactory();
IScheduler scheduler = schedulerFactory.GetScheduler();
IJobDetail jobDetail = JobBuilder.Create<SatellitePaymentGenerationJob>()
.WithIdentity("TestJob")
.Build();
ITrigger trigger = TriggerBuilder.Create()
.ForJob(jobDetail)
.WithCronSchedule("0 45 20 * * ?")
.WithIdentity("TestTrigger")
.StartNow()
.Build();
scheduler.ScheduleJob(jobDetail, trigger);
scheduler.Start();
}
}
internal class SatellitePaymentGenerationJob : IJob
{
public void Execute(IJobExecutionContext context)
{
Console.WriteLine("test");
}
}
I am using Quartz to schedule job in my c# .net application. I am storing all data in database. My code is :
ISchedulerFactory schedFact = new StdSchedulerFactory(properties);
_scheduler = schedFact.GetScheduler();
_scheduler.Start();
job = JobBuilder.Create<JobTask>()
.WithIdentity("job1", "group1")
.Build();
trigger = TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.WithSchedule(
CronScheduleBuilder.CronSchedule("0 0/5 * * * ?"))
.Build();
_scheduler.ScheduleJob(job, trigger);
Now I would like to give user function so user can disable(unschedule) job. I have look in quartz tutorial but I can't find the way to do it in c#.
You can call following method in IScheduler
//
// Summary:
// Remove the indicated Quartz.Trigger from the scheduler.
bool UnscheduleJob(string triggerName, string groupName);
Your Job class can implement IInterruptableJob interface and implement
public void Interrupt()
{
JobKey jobKey = new JobKey(JobName, MyService.GroupName);
Scheduler.Interrupt(jobKey);
}