public bool PauseJob(Customer_JobInfo jobInfo) { var jobKey = TaskSchedulerHelper.CreateJobKey(jobInfo.JobName, jobInfo.JobGroupName); //var triggerKey = TaskSchedulerHelper.CreateTriggerKey(jobInfo.TriggerName, jobInfo.TriggerGroupName); _scheduler.PauseJob(jobKey).Wait(); return(true); }
public bool RunJob(Customer_JobInfo jobInfo) { var jobKey = TaskSchedulerHelper.CreateJobKey(jobInfo.JobName, jobInfo.JobGroupName); var triggerKey = TaskSchedulerHelper.CreateTriggerKey(jobInfo.TriggerName, jobInfo.TriggerGroupName); var flag = _scheduler.CheckExists(jobKey).Result; if (flag) { //////存在job,先删除 _scheduler.PauseTrigger(triggerKey).GetAwaiter().GetResult(); _scheduler.UnscheduleJob(triggerKey).GetAwaiter().GetResult(); _scheduler.DeleteJob(jobKey).GetAwaiter().GetResult(); Console.WriteLine("当前job已经存在,无需调度:{0}", jobKey.ToString()); } if (!string.IsNullOrWhiteSpace(jobInfo.DLLName)) { var jobdata = new JobDataMap() { new KeyValuePair <string, object>(TaskSchedulerHelper.JobDataMapKeyJobId, jobInfo.JobId), new KeyValuePair <string, object>("JobArgs", jobInfo.JobArgs), new KeyValuePair <string, object>("RequestUrl", jobInfo.RequestUrl) }; var type = ReflectHelper.GetType(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) { _scheduler.Start().GetAwaiter().GetResult(); } _scheduler.ScheduleJob(jobDetail, trigger).GetAwaiter().GetResult();; } return(true); }