/// <summary> /// 重启任务 /// </summary> /// <param name="jobInfo"></param> /// <returns></returns> public bool ResumeJob(Customer_JobInfo jobInfo) { var jobKey = MangertKey.CreateJobKey(jobInfo.JobName, jobInfo.JobGroupName); var triggerKey = MangertKey.CreateTriggerKey(jobInfo.TriggerName, jobInfo.TriggerGroupName); Scheduler.ResumeJob(jobKey).Wait(); return(true); }
/// <summary> /// 修改任务周期 /// </summary> /// <param name="jobInfo"></param> /// <returns></returns> public bool ModifyJobCron(Customer_JobInfo jobInfo) { var scheduleBuilder = CronScheduleBuilder.CronSchedule(jobInfo.Cron); var triggerKey = MangertKey.CreateTriggerKey(jobInfo.TriggerName, jobInfo.TriggerGroupName); var trigger = TriggerBuilder.Create().StartAt(DateTimeOffset.Now.AddYears(-1)).WithIdentity(triggerKey).WithSchedule(scheduleBuilder.WithMisfireHandlingInstructionDoNothing()).Build(); Scheduler.RescheduleJob(triggerKey, trigger); return(true); }
public bool DeleteJob(Customer_JobInfo jobInfo) { var jobKey = MangertKey.CreateJobKey(jobInfo.JobName, jobInfo.JobGroupName); var triggerKey = MangertKey.CreateTriggerKey(jobInfo.TriggerName, jobInfo.TriggerGroupName); Scheduler.PauseTrigger(triggerKey); Scheduler.UnscheduleJob(triggerKey); Scheduler.DeleteJob(jobKey); return(true); }
/// <summary> /// 运行job /// </summary> /// <param name="jobInfo"></param> /// <returns></returns> public async Task <bool> RunJob(Customer_JobInfo jobInfo) { var jobKey = MangertKey.CreateJobKey(jobInfo.JobName, jobInfo.JobGroupName); var triggerKey = MangertKey.CreateTriggerKey(jobInfo.TriggerName, jobInfo.TriggerGroupName); var flag = Scheduler.CheckExists(jobKey).Result; if (flag) { //////存在job,先删除 await Scheduler.PauseTrigger(triggerKey); await Scheduler.UnscheduleJob(triggerKey); await Scheduler.DeleteJob(jobKey); Console.WriteLine("当前job已经存在,无需调度:{0}", jobKey.ToString()); } if (!string.IsNullOrWhiteSpace(jobInfo.DLLName)) { var jobdata = new JobDataMap() { new KeyValuePair <string, object>(MangertKey.JobDataMapKeyJobId, jobInfo.JobId), new KeyValuePair <string, object>("JobArgs", jobInfo.JobArgs), new KeyValuePair <string, object>("RequestUrl", jobInfo.RequestUrl) }; var type = GetClassInfo(jobInfo.DLLName, jobInfo.FullJobName); IJobDetail jobDetail = JobBuilder.Create(type) .WithIdentity(jobKey) .UsingJobData(jobdata) .RequestRecovery(false) .StoreDurably() .WithDescription("使用quartz进行持久化存储") .Build(); ////两种不同的写法结果不同 //IJobDetail jobDetail = JobBuilder.Create<TestJob2>() // .WithIdentity(jobKey) // .UsingJobData(jobdata) // .RequestRecovery(false) // //.StoreDurably() // .WithDescription("使用quartz进行持久化存储") // .Build(); CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.CronSchedule(jobInfo.Cron); ITrigger trigger = TriggerBuilder.Create() //.StartAt(DateTimeOffset.Now.AddYears(-1)) .StartNow() .WithIdentity(jobInfo.TriggerName, jobInfo.TriggerGroupName) .ForJob(jobKey) //.WithSimpleSchedule(x => x.WithIntervalInSeconds(2).RepeatForever()) //.WithSchedule(cronScheduleBuilder.WithMisfireHandlingInstructionDoNothing()) .WithCronSchedule(jobInfo.Cron) .Build(); if (!Scheduler.IsStarted) { await Scheduler.Start(); } await Scheduler.ScheduleJob(jobDetail, trigger); } return(true); }