防止石英在加载后立即运行作业
本文关键字:运行 作业 加载 石英 | 更新日期: 2023-09-27 18:32:50
我有一个石英作业,计划在特定时间运行,但是一旦加载作业,它就会运行一次,而不考虑时间,然后在计划的时间再次运行。
如何限制它仅在计划时间运行而不是在加载后立即运行?
public void Initialze()
{
_schedulerFactory = new StdSchedulerFactory();
_scheduler = _schedulerFactory.GetScheduler();
_scheduler.Start();
}
public StatusObject AddJobToQuartzScheduler(T_BIT_Scheduler item)
{
return AddJobToQuartzScheduler(new QuartzModel { BeginDateTime = item.BeginDateTime, IdeaId = item.IdeaId, IsActive = item.IsActive, SchedulerExpression = item.SchedulerExpression, SchedulerID = item.SchedulerID, SchedulerName = item.SchedulerName, SchedulerType = item.SchedulerType });
}
public StatusObject AddJobToQuartzScheduler(QuartzModel quartzData, bool AddToDB = false)
{
IGlobalFunction globalFunction = new GlobalFunction();
try
{
int schedulerId = quartzData.SchedulerID;
if (AddToDB)
{
using (var dbContext = new DBEntities())
{
IQuartzRepository quartzRepository = new QuartzRepository(dbContext);
schedulerId = quartzRepository.Add(new T_BIT_Scheduler { BeginDateTime = quartzData.BeginDateTime, IdeaId = quartzData.IdeaId, IsActive = quartzData.IsActive, SchedulerExpression = quartzData.SchedulerExpression, SchedulerName = quartzData.SchedulerName, SchedulerType = quartzData.SchedulerType }).SchedulerID;
}
}
CronExpression cron = new CronExpression(quartzData.SchedulerExpression);
if (cron.GetNextValidTimeAfter(DateTime.Now) == null)
{
globalFunction.FailureObject("Invalid Time");
}
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity(quartzData.SchedulerName + "Trigger", "BrightIdeaGroup")
.StartAt(quartzData.BeginDateTime)
.WithCronSchedule(cron.CronExpressionString)
.Build();
//task.TaskName+"Job" is added to give unique job name
IJobDetail job = JobBuilder.Create<QuartzJob>()
.WithIdentity(schedulerId + "Job", "ScheduledJobGroup")
.UsingJobData("IdeaID", quartzData.IdeaId)
.UsingJobData("Type", quartzData.SchedulerType)
.Build();
// Tell quartz to schedule the job using our trigger
_scheduler.ScheduleJob(job, trigger);
return globalFunction.SuccessObject("Successfully Added the job");
}
catch (Exception e)
{
return globalFunction.FailureObject(e.Message);
}
// log if a job failed to get add
}
您可以使用 objSchedular.StartDelayed(new TimeSpan(Trigger Interval));它将在计划的时间触发第一个作业执行,而不是在加载后立即触发......如果它正在工作,请还原。