Quartz.NET Rescheduling job with new trigger set
Asked Answered
A

3

7

I have a Quartz job which is scheduled with a collection of triggers and it has 3 to 5 minutes of execution time. But at any moment in the future(it may be a week later or a couple of minutes later) I may need to reschedule it with a new trigger set. There will be some additions or deletions on trigger set.

How can I reschedule the job with new trigger set? The trick here is, I want to be sure that no instance of the job is alive at that moment, so I can reschedule my job reliably. Thanks for any help...

Arbitrary answered 2/5, 2013 at 9:19 Comment(0)
N
4

As I remember it, you can do:

List<JobExecutionContext> context = scheduler.GetCurrentlyExecutingJobs()

Iterate the list and call GetJobInstance() (or something similar) to find the job the context was created for, then check if it's the job you're interested in. If not, you can reschedule using the same trigger. Try something like this:

Trigger trigger = Global.scheduler.GetTrigger("testTrigger","triggerGroup");

trigger.set(); 

Global.scheduler.RescheduleJob(trigger.JobName, trigger.JobGroup, trigger);
Nephelometer answered 2/5, 2013 at 18:14 Comment(3)
Thanks for your answer. It is what I looked for. GetCurrentlyExecutingJobs() handles the job greatly.Arbitrary
should be IList<IJobExecutionContext> context = scheduler.GetCurrentlyExecutingJobs();Postfree
It worked for me when I used trigger name and group insted of job name and job group.Agc
R
1

I have re-scheduled ActiveDirectoryJob as following:

public class ActiveDirectoryJob : IJob
{
    public void Execute(IJobExecutionContext context)
    {
        var staffService = new StaffService();

        staffService.Synchronize();

        int intervalTime=2;
        ITrigger rsiTrigger = TriggerBuilder.Create()
        .WithIdentity(context.Trigger.Key.Name)
        .StartAt(DateTime.Now.AddHours(intervalTime))
        .WithSimpleSchedule(x => x
        .WithIntervalInHours(intervalTime)
        .RepeatForever())
        .Build();


        context.Scheduler.RescheduleJob(new TriggerKey(context.Trigger.Key.Name, ((AbstractTrigger)context.Trigger).Group), rsiTrigger);

    }
}

PS:

I am using Quartz v2.6.1

Revile answered 15/1, 2018 at 12:22 Comment(0)
D
0

Yes you can reschedule or assign new trigger to the existing scheduler or job, if you had Quartz package verion above ver.3. (If you does have it update throgh nuget package maneger).

For Example: At first I had a scheduler declared within a class FireBaseJob like:

        IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler().Result;
        public static void InitializeScheduler()
        {
            //Getting Defauls Scheduler Job
           
            //Setting trigger time interval, ie. 3min here
            int min = 3;
            scheduler.Start();

            IJobDetail jobDetail = JobBuilder.Create<FirebaseScheduler>().Build();
            ITrigger trigger = TriggerBuilder.Create().WithIdentity("FireTrigger1", "FireJob1")
                .StartNow().WithSimpleSchedule(s => s.WithIntervalInMinutes(min).RepeatForever()).Build();
            
            scheduler.ScheduleJob(jobDetail, trigger);
        }

You can write api,function or within a runing job also you can reschedule a job (for me running job is in the class FirebaseSceduler). I created function to do it.

        public ActionResult RescheduleJob()
        {

            try
            {
                var triggerBykeyGroup = FirebaseJob.scheduler.GetTrigger(new Quartz.TriggerKey("FireTrigger1", "FireJob1"));

                int nextIntervalMinute = 5; // i am setting new trigger minute to 5 mins
                TimeSpan newmin = new TimeSpan(0, nextIntervalMinute, 0);

                //Updating trigger time interval in minutes
                ((Quartz.Impl.Triggers.SimpleTriggerImpl)triggerBykeyGroup.Result).RepeatInterval = newmin;

                //Getting updated trigger result
                var iTrigger = triggerBykeyGroup.Result;
                TriggerKey triggerkey = iTrigger.Key;

                //Starting paused job
                FirebaseJob.scheduler.Start();

                //rescheduling Job with updated trigger
                FirebaseJob.scheduler.RescheduleJob(triggerkey, iTrigger);
            }
            catch(Exception ex)
            {

            }
            return Json(new { success = true; });
        }

After executing this function Trigger will run in the interval of 5min. Thank You.

Denysedenzil answered 23/8, 2020 at 12:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.