GetNextFireTime of my existing work

I tried the Quartz.com documentation and searched for a pair for a couple for several hours ... but could not find any good article on how to get the next job (which should be launched in the future).

I use the CronTrigger expression to send jobs using VB.net (winforms). work assignments work fine ... however, I would like my users to see when it will be later. I store CronExpression in my database, can I use this expression to display the next fire date / time for my end users? or if there is any other way, please advise (just try).

thanks

Edit

The following code returns the next task will fire after 12 minutes instead of 20 minutes

Dim exp As CronExpression = New CronExpression("0 0/20 * * * ?") Dim nextFire As String = exp.GetNextValidTimeAfter(DateTime.Now) MsgBox(nextFire) 
+4
source share
2 answers

You can create a new jobkey

 JobKey jobKey = new JobKey(jobName, groupName); 

and use the key to get detailed information about the task:

 var detail = scheduler.GetJobDetail(jobKey); 

this is a simple function that does what you are looking for:

  private DateTime getNextFireTimeForJob(IScheduler scheduler, string jobName, string groupName = "") { JobKey jobKey = new JobKey(jobName, groupName); DateTime nextFireTime = DateTime.MinValue; bool isJobExisting = Scheduler.CheckExists(jobKey); if (isJobExisting) { var detail = scheduler.GetJobDetail(jobKey); var triggers = scheduler.GetTriggersOfJob(jobKey); if (triggers.Count > 0) { var nextFireTimeUtc = triggers[0].GetNextFireTimeUtc(); nextFireTime = TimeZone.CurrentTimeZone.ToLocalTime(nextFireTimeUtc.Value.DateTime); } } return (nextFireTime); } 

It only works if you have one trigger per task.
If your work has more than one trigger, you can skip them through:

 foreach (ITrigger trigger in triggers) { Console.WriteLine(jobKey.Name); Console.WriteLine(detail.Description); Console.WriteLine(trigger.Key.Name); Console.WriteLine(trigger.Key.Group); Console.WriteLine(trigger.GetType().Name); Console.WriteLine(scheduler.GetTriggerState(trigger.Key)); DateTimeOffset? nextFireTime = trigger.GetNextFireTimeUtc(); if (nextFireTime.HasValue) { Console.WriteLine(TimeZone.CurrentTimeZone.ToLocalTime(nextFireTime.Value.DateTime).ToString()); } } 

or using Linq ( System.Linq ):

  var myTrigger = triggers.Where(f => f.Key.Name == "[trigger name]").SingleOrDefault(); 
+5
source

If you already know cronExpression, you can call GetNextValidTimeAfter, for example

 CronExpression exp = new CronExpression("0 0 0/1 1/1 * ? *"); var nextFire = exp.GetNextValidTimeAfter(DateTime.Now); Console.WriteLine(nextFire); 

and if you want more response time, then

 for(int i=0 ; i< 9; i++) { if (nextFire.HasValue) { nextFire = exp.GetNextValidTimeAfter(nextFire.Value); Console.WriteLine(nextFire); } } 

If you are looking for a more general way to display the next fire time for existing jobs, then check the answer to this question, which works even if you don’t know t using cron expressions. This is for Quartz Version 2

Quartz version 1, the way to get information about the job and the trigger is something like the method below.

  public void GetListOfJobs(IScheduler scheduler) { var query = (from groupName in scheduler.JobGroupNames from jobName in scheduler.GetJobNames(groupName) let triggers = scheduler.GetTriggersOfJob(jobName, groupName) select new { groupName, jobName, triggerInfo = (from trigger in triggers select trigger) } ); foreach (var r in query) { if (r.triggerInfo.Count() == 0) { var details = String.Format("No triggers found for : Group {0} Job {1}", r.groupName, r.jobName); Console.WriteLine(details); } foreach (var t in r.triggerInfo) { var details = String.Format("{0,-50} {9} Next Due {1,30:r} Last Run {2,30:r} Group {3,-30}, Trigger {4,-50} {5,-50} Scheduled from {6:r} Until {7,30:r} {8,30:r} ", t.JobName, t.GetNextFireTimeUtc().ToLocalTime(), t.GetPreviousFireTimeUtc().ToLocalTime(), t.JobGroup, t.Name, t.Description, t.StartTimeUtc.ToLocalTime(), t.EndTimeUtc.ToLocalTime(), t.FinalFireTimeUtc.ToLocalTime(), ((t.GetNextFireTimeUtc() > DateTime.UtcNow) ? "Active " : "InActive") ); Console.WriteLine(details); } } 

}

+4
source

All Articles