1

Is there any way to create a new trigger on a JobDetail record which is in the database, but not in memory? Specific use: I have jobs scheduled to run daily using a cron trigger. Sometimes a particular day's job needs to be re-run with the same parameters. I'd like to create a new simple trigger using the same JobDetail, since that's where the parameters are stored. Reschedule() doesn't work, since it deletes the existing trigger. Is that possible?

4 Answers4

2

Yes, you can.

What I would do is the fetch the job from the database:

var myJob = Scheduler.GetJobDetail(jobName, groupName);

and use the function (which probably you've already used)

Scheduler.ScheduleJob(JobDetail jobDetail, Trigger trigger);

passing your new trigger. You don't have to do much cause the run-time will fetch the new trigger from the DB after a few seconds and will run it.

UPDATE

There are two ways to add a trigger with Quartz.net

1) you can add a job and then the trigger:

Scheduler.AddJob(jobToSchedule, true);
Scheduler.ScheduleJob(triggerToSchedule);

2) you can schedule a trigger adding a job at the same time:

Scheduler.ScheduleJob(jobToSchedule, triggerToSchedule);

If you try to add a job and a trigger this way:

Scheduler.AddJob(jobToSchedule, true);
Scheduler.ScheduleJob(jobToSchedule, triggerToSchedule);

You get an exception which warns you the job already exists.

I would suggest you to clean the DB before making any test cause you might have some pending jobs. You can find a simple routine to clean it here:

        // unschedule jobs
        string[] groups = Scheduler.TriggerGroupNames;
        for (int i = 0; i < groups.Length; i++)
        {
            string[] names = Scheduler.GetTriggerNames(groups[i]);
            for (int j = 0; j < names.Length; j++)
            {
                Scheduler.UnscheduleJob(names[j], groups[i]);
            }
        }

        // delete jobs
        groups = Scheduler.JobGroupNames;
        for (int i = 0; i < groups.Length; i++)
        {
            string[] names = Scheduler.GetJobNames(groups[i]);
            for (int j = 0; j < names.Length; j++)
            {
                Scheduler.DeleteJob(names[j], groups[i]);
            }
        }
LeftyX
  • 35,328
  • 21
  • 132
  • 193
  • 1
    Tried this before. Maybe it works in Quartz, but in Quartz.net, GetJobDetail() works, but ScheduleJob() fails with "Couldn't store job: Unable to store Job with name: 'Job Name 1234' and group: 'Group Name', because one already exists with this identification." – Bruce Antman Mar 31 '11 at 16:56
  • I working with this stuff right now (Quartz.net) after one month of struggling. I'll update my answer to give you some more infos. – LeftyX Apr 01 '11 at 15:40
2

In Quartz.net, I had to do this a little differently:

Trigger trigger = new SimpleTrigger(triggerName, triggerGroup, newDateTime); trigger.JobName = jobName; trigger.GroupName = jobGroup; Scheduler.ScheduleJob(trigger);

That worked.

0

For QuartzNet, Scheduler.GetJobDetail(jobName, groupName) is not available. Instead use :

    ISchedulerFactory schedFact = new StdSchedulerFactory();
    IScheduler sched = schedFact.GetScheduler();
    sched.Start();
    sched.GetJobDetail(new JobKey("job1", "group1"));

Additional reference: GetJobDetail Method

Picrofo Software
  • 5,475
  • 3
  • 23
  • 37
Sairam Krish
  • 10,158
  • 3
  • 55
  • 67
0

If you need to remove all Jobs/Triggers in Quartz.Net 2.0 - this will do it for you:

        var groups = _scheduler.GetJobGroupNames().ToList();

        foreach (var group in groups)
        {
            var jobKeys = _scheduler.GetJobKeys(GroupMatcher<JobKey>.GroupEquals(group)).ToList();

            foreach (var jobKey in jobKeys)
            {
                _scheduler.DeleteJob(jobKey);
            }
        }
znelson
  • 919
  • 1
  • 10
  • 24