在运行时在 .Net MVC 中创建和调度作业
Create and schedule jobs at runtime in .Net MVC
我想创建一个 Report Scheduler 以根据用户定义的时间自动发送带有附件报告的电子邮件。
用户可以指定报告的时间和发生次数,一旦保存,调度程序需要运行根据定义的设置在特定的time/day上,而且每个用户都可以独立定义自己的设置.
我参考了下面的答案
/// <summary>
/// Create a new trigger based on a existing trigger.
/// </summary>
/// <param name="oldTrigger">the existing trigger</param>
/// <param name="startNow">indicates if the trigger should start immediately after schedule</param>
/// <param name="interval">the interval for the new trigger</param>
/// <returns>Returns the new trigger</returns>
public ITrigger CreateTrigger(ITrigger oldTrigger, bool startNow = false, TimeSpan? interval = null)
{
var builder = oldTrigger.GetTriggerBuilder();
if (startNow)
builder = builder.StartNow();
if (interval.HasValue)
builder = builder.WithSimpleSchedule(s => s
.WithInterval(interval.Value)
.RepeatForever());
var newTrigger = builder.Build();
var simpleTrigger = newTrigger as ISimpleTrigger;
if(simpleTrigger != null)
{
var trigger = oldTrigger as ISimpleTrigger;
if (trigger != null)
simpleTrigger.TimesTriggered = trigger.TimesTriggered;
}
return newTrigger;
}
如何使用
In this example, the Scheduler is already running.
// get the trigger
var trigger = Scheduler.GetTrigger(new TriggerKey("trigger1", "group1"));
// get your settings and set the interval
var reminderSettings = GetReminderSettings();
var intervalBtwnReminders = 0;
if (reminderSettings?.RemindersGap != null)
{
intervalBtwnReminders = (int)(reminderSettings.RemindersGap);
}
// reschedule the job with a new trigger and start it immediately.
// if you don't want that it starts now, pass 'false' for the 'startNow' parameter
Scheduler.RescheduleJob(trigger.Key, CreateTrigger(trigger, true, TimeSpan.FromMinutes(intervalBtwnReminders)));
问题:
上面的代码显示了重新安排作业,但我想在每次用户保存时创建新的调度程序。
如果两个用户定义相同的时间然后 Quartz.Net 对两者都有效怎么办?
是否有更好的方法可用或此方法是否存在任何性能问题?
Hangfire解决了我上面所有的问题
我想创建一个 Report Scheduler 以根据用户定义的时间自动发送带有附件报告的电子邮件。
用户可以指定报告的时间和发生次数,一旦保存,调度程序需要运行根据定义的设置在特定的time/day上,而且每个用户都可以独立定义自己的设置.
我参考了下面的答案
/// <summary>
/// Create a new trigger based on a existing trigger.
/// </summary>
/// <param name="oldTrigger">the existing trigger</param>
/// <param name="startNow">indicates if the trigger should start immediately after schedule</param>
/// <param name="interval">the interval for the new trigger</param>
/// <returns>Returns the new trigger</returns>
public ITrigger CreateTrigger(ITrigger oldTrigger, bool startNow = false, TimeSpan? interval = null)
{
var builder = oldTrigger.GetTriggerBuilder();
if (startNow)
builder = builder.StartNow();
if (interval.HasValue)
builder = builder.WithSimpleSchedule(s => s
.WithInterval(interval.Value)
.RepeatForever());
var newTrigger = builder.Build();
var simpleTrigger = newTrigger as ISimpleTrigger;
if(simpleTrigger != null)
{
var trigger = oldTrigger as ISimpleTrigger;
if (trigger != null)
simpleTrigger.TimesTriggered = trigger.TimesTriggered;
}
return newTrigger;
}
如何使用
In this example, the Scheduler is already running.
// get the trigger
var trigger = Scheduler.GetTrigger(new TriggerKey("trigger1", "group1"));
// get your settings and set the interval
var reminderSettings = GetReminderSettings();
var intervalBtwnReminders = 0;
if (reminderSettings?.RemindersGap != null)
{
intervalBtwnReminders = (int)(reminderSettings.RemindersGap);
}
// reschedule the job with a new trigger and start it immediately.
// if you don't want that it starts now, pass 'false' for the 'startNow' parameter
Scheduler.RescheduleJob(trigger.Key, CreateTrigger(trigger, true, TimeSpan.FromMinutes(intervalBtwnReminders)));
问题:
上面的代码显示了重新安排作业,但我想在每次用户保存时创建新的调度程序。
如果两个用户定义相同的时间然后 Quartz.Net 对两者都有效怎么办?
是否有更好的方法可用或此方法是否存在任何性能问题?
Hangfire解决了我上面所有的问题