/// <summary> /// 创建JobKey /// </summary> /// <param name="config">工作配置</param> /// <returns></returns> static Quartz.JobKey JobKeyBuilder(IJobConfig config) { return JobKeyBuilder(config.Name); }
/// <summary> /// 创建触发器 /// </summary> /// <param name="config">工作配置</param> /// <returns></returns> static Quartz.ITrigger TriggerBuilder(IJobConfig config) { return new Quartz.Impl.Triggers.CronTriggerImpl(config.Name, null, config.Name, null, config.StartTime, config.EndTime, config.CronExpression); }
/// <summary> /// 调度任务 /// </summary> /// <param name="jobDetail">工作内容</param> /// <param name="trigger">触发器</param> static Urge.Job.JobResult Begin(Quartz.JobBuilder jobBuilder, IJobConfig jobConfig) { var result = new Urge.Job.JobResult() { Status = false }; if(string.IsNullOrWhiteSpace(jobConfig.Name)) { result.Message = "任务名称不能为空!"; return result; } if (jobConfig.StartTime > jobConfig.EndTime) { result.Message = "任务开始时间不能大于结束时间!"; return result; } var job = jobBuilder.WithIdentity(JobKeyBuilder(jobConfig)).Build(); if (jobConfig.JobData != null) { foreach (var data in jobConfig.JobData) { job.JobDataMap[data.Key] = data.Value; } } if (!SchedulerBuilder.CheckExists(JobKeyBuilder(jobConfig))) { SchedulerBuilder.ScheduleJob(job, TriggerBuilder(jobConfig)); Start(); result.Status = true; result.Message = "任务调度成功!"; } else { result.Message = "任务调度失败,请确保任务名称唯一!"; } return result; }