views:

37

answers:

1

Hi all, I'm using Quartz.NET for a system I am creating, where jobs are run via a Windows Service and can be added and editted (inlcuding trigger details) in an ASP.NET page.

I'm having some issues though - I need to be able to modify a triggers details, such as the type of simpletrigger or the cronexpression, and I've been told that I need to unschedule the job then reschedule it. Problem is, unscheduling the job then rescheduling is totally removing the trigger from the job for some reason. Here is my code for when the Submit button is clicked on the edit page:

 Trigger[] triggers = sched.GetTriggersOfJob(hdnID.Value, hdnGroupID.Value);

        if (ddlScheduleType.SelectedIndex == 1)
        {
            sched.UnscheduleJob(triggers[0].Name, triggers[0].Group);
            foreach (SimpleTrigger trigger in triggers.OfType<SimpleTrigger>())
            {
                if (ddlInterval.SelectedIndex == 0)
                {
                    trigger.RepeatInterval = TimeSpan.Parse("00:00:01");
                }
                else if (ddlInterval.SelectedIndex == 1)
                {
                    trigger.RepeatInterval = TimeSpan.Parse("00:01:00");
                }
                else if (ddlInterval.SelectedIndex == 2)
                {
                    trigger.RepeatInterval = TimeSpan.Parse("01:00:00");
                }


               sched.RescheduleJob(trigger.Name, trigger.Group, trigger);


            }
        }

        else
        {
            foreach (CronTrigger trigger in triggers.OfType<CronTrigger>())
            {
                trigger.CronExpressionString = txtCron.Text;
                sched.UnscheduleJob(triggers[0].Name, triggers[0].Group);
                sched.RescheduleJob(trigger.Name, trigger.Group, trigger);

            }


        }

        sched.AddJob(jobDetail, true);

Any ideas why this isn't working? Thanks

NEW:

 if (ddlScheduleType.SelectedIndex == 1)

        {
            foreach (SimpleTrigger trigger in triggers.OfType<SimpleTrigger>())
            {
                Trigger newtrigger = new SimpleTrigger();


                if (ddlInterval.SelectedIndex == 0)
                {
                    newtrigger = TriggerUtils.MakeSecondlyTrigger();
                }
                else if (ddlInterval.SelectedIndex == 1)
                {
                    newtrigger = TriggerUtils.MakeMinutelyTrigger();
                }
                else if (ddlInterval.SelectedIndex == 2)
                {
                    newtrigger = TriggerUtils.MakeHourlyTrigger();
                }

                newtrigger.Name = triggers[0].Name;
                newtrigger.Group = triggers[0].Group;
                newtrigger.JobName = triggers[0].JobName;
                newtrigger.JobGroup = triggers[0].JobGroup;

                sched.RescheduleJob(trigger.Name, trigger.Group, newtrigger);


            }
        }

        else
        {
            foreach (CronTrigger trigger in triggers.OfType<CronTrigger>())
            {
                CronTrigger newtrigger2 = new CronTrigger();
                newtrigger2.Name = trigger.Name;
                newtrigger2.Group = trigger.Group;
                newtrigger2.JobName = trigger.JobName;
                newtrigger2.JobGroup = trigger.JobGroup;

                newtrigger2.CronExpressionString = txtCron.Text;
                sched.RescheduleJob(trigger.Name, trigger.Group, newtrigger2);

            }


        }
A: 
  • I doubt UnscheduleJob is necessary. Reschedule should delete the old trigger, so try without it
  • Your else code doesn't make sense:

        foreach (CronTrigger trigger in triggers.OfType<CronTrigger>())
        {
            trigger.CronExpressionString = txtCron.Text;
            sched.UnscheduleJob(triggers[0].Name, triggers[0].Group);
            sched.RescheduleJob(trigger.Name, trigger.Group, trigger);
        }
    

    Do you mean perhaps

        foreach (CronTrigger trigger in triggers.OfType<CronTrigger>())
        {
            trigger.CronExpressionString = txtCron.Text;
            sched.UnscheduleJob(trigger.Name, trigger.Group); //I would remove this 
            sched.RescheduleJob(trigger.Name, trigger.Group, trigger);
        }
    
  • Lastly, I would try rescheduling with a new trigger rather than with the old one modified

ohadsc
Cheers buddy, that works great - but if I want to change a SimpleTrigger to a CronTrigger it doesn't seem to work, it just keeps the old one
Chris
Can you post the code you're using to replace a SimpleTrigger with a CronTrigger ?
ohadsc
Just posted the new code :)
Chris
Your new code doesn't change SimpleTriggers to CronTriggers. In one case it changes SimpleTrigger to SimpleTrigger. In the other case it changes CronTriggers to other CronTriggers.
ohadsc
Hmm, do I need to be able to delete the other trigger? How would I do this? Sorry for sounding dumb, my brains got in gear lately!
Chris
For example, when you write `foreach (SimpleTrigger trigger in triggers.OfType<SimpleTrigger>` you only loop on the SimpleTriggers. If you want to change all the triggers, try `foreach (Trigger trigger in triggers)` (notice I dropped the OfType() call)
ohadsc