开发者

Running Quartz.net job which fires 3 times in a row--should only be once

开发者 https://www.devze.com 2023-02-16 16:33 出处:网络
I\'ve got the following code to schedule reboot jobs using Quartz.The code sets up a trigger for each day of the week (that is selected by the user).So the job should be able to run a maximum of once

I've got the following code to schedule reboot jobs using Quartz. The code sets up a trigger for each day of the week (that is selected by the user). So the job should be able to run a maximum of once per day. However, when I test it out, it runs thre开发者_如何转开发e times in a row instead of just once. Any ideas?

private void ScheduleJob(Server server)
{
    bool jobScheduled = false;
    JobDetail job = new JobDetail(server.ServerName, JOB_GROUP, typeof(RebootJob));
    job.JobDataMap.Add("Server", server);
    // Create a trigger for each day of the week on which this schedule falls
    for(int i=0; i < 7; i++)
    {
        DayOfWeek dayOfWeek = (DayOfWeek)i;
        DateTime? jobStartTime = server.GetNextScheduledRebootDateAndTime();
        if(server.isScheduledOnDayOfWeek(dayOfWeek) && jobStartTime.HasValue)
        {
            SimpleTrigger trigger = new SimpleTrigger(
                String.Format("{0}.{1}", server.ServerName, dayOfWeek.ToString()),
                JOB_GROUP,
                jobStartTime.Value.ToUniversalTime(),
                null,
                SimpleTrigger.RepeatIndefinitely,
                TimeSpan.FromDays(7 * (double)server.RebootFrequency)
            );
            if(!jobScheduled)
            {
                ManagementService.Instance.Scheduler.ScheduleJob(job, trigger);
                jobScheduled = true;
                // Add server to list of servers with a reboot schedule
                m_RebootableServers.Add(server.ServerName, server);
            }
            else // A job has already been schedule, just append triggers to it
            {
                trigger.JobName = job.Name;
                trigger.JobGroup = job.Group;
                ManagementService.Instance.Scheduler.ScheduleJob(trigger);
            }
        }
    }
}


It looks like you are setting up each trigger to fire at the same time and have the same interval.

Your "for" loop iterating through "i" does not account for the fact that "server.GetNextScheduledRebootDateAndTime()" always returns the same "next" reboot time. I would guess that you are testing a server that is configured to reboot on 3 days in a week and all of the triggers are being setup to start at the same time with a 7 day repeat interval.

If you are set on using a SimpleTrigger then you need to shift the start time of the trigger with the day of week that you are wanting to schedule the reboot on.

0

精彩评论

暂无评论...
验证码 换一张
取 消