今天遇到一个问题,调度器在启动时无法立刻开始执行任务,代码如下:
var trigger = TriggerBuilder.Create()
.StartNow() //此处无效
.WithCronSchedule("0 0/30 10-20 * * ?")
.Build();
F12 可以看到 StartNow() 注释如下:
//
// 摘要:
// Set the time the Trigger should start at to the current moment - the trigger
// may or may not fire at this time - depending upon the schedule configured
// for the Trigger.
//
// 返回结果:
// the updated TriggerBuilder
public TriggerBuilder StartNow();
在 stockoverflow 上查找结果如下
由上述得知,当使用 Cron 表达式时,StartNow 方法不会起任何效果,Cron 有其自己的执行时间。目前看来 StartNow 应该只适用于 SimpleTrigger 触发器。
解决方法:
1.如上图,增加一个仅有 StartNow 的触发器来立刻触发 Job
2.可在 scheduler.Start() 启动前手动调用执行 Job 一次