作业重新安排不起作用

本文关键字:不起作用 新安排 作业 | 更新日期: 2023-09-27 18:29:15

我想更改其中一个作业的触发器。因此,我要做的是首先查看我的作业是否有与之相关的触发器。如果有,我将用用户选择的新触发器Reschedule。但当我查看我的AdoJobStore时,新的触发器不在那里,并且在重新安排后作业将停止运行。

我已经将整个代码放在try-catch块中,但我没有接收到任何Exceptions,但这可能是因为我应该捕获JobExecutionExceptions,而不仅仅是Exceptions?

var jobKey = new JobKey(jobName, jobGroup);
IScheduler sched = scheduler.GetScheduler();
IList<ITrigger> triggers = sched.GetTriggersOfJob(jobKey);
if (triggers.Count != 0)
{
    ITrigger existingTrigger = triggers[0];
    sched.UnscheduleJob(existingTrigger.Key);
    ITrigger newTrigger = BuildTrigger(triggerName, triggerGroup);
    IJobDetail job = sched.GetJobDetail(jobKey);
    DialogResult dr = MsgBox.Show(string.Format("Confirm campaign '{0}' with the schedule '{1}'?", lblCampaignName.Text, txtbxMessage.Text), "Confirm Schedule", MsgBox.Buttons.YesNo, MsgBox.Icon.Question);
    if (dr == DialogResult.Yes)
    {
        sched.RescheduleJob(existingTrigger.Key, newTrigger);
        int updateResult = UpdateCampaignSchedule();
        if (updateResult == 1)
            MsgBox.Show("Campaign schedule successfully updated!", "Edit schedule", MsgBox.Buttons.OK, MsgBox.Icon.Info);
        else
            MsgBox.Show("Unable to update campaign schedule in the database.", "Edit schedule", MsgBox.Buttons.OK, MsgBox.Icon.Error);
    }
}
else
{
..
}

作业重新安排不起作用

这是因为调用了sched.UnscheduleJob(existingTrigger.Key。这将从作业存储中删除触发器。因此,当调用sched.RescheduleJob(existingTrigger.Key, newTrigger)时,石英无法找到旧触发器来用新触发器替换它(如果您查看返回值,它将为null),也不会抛出异常,除非您的新触发器无效。

删除sched.UnscheduleJob(existingTrigger.Key, newTrigger,它应该可以工作。