// 添加Job调度(如果job已经存在,抛出异常) public async Task ScheduleJob(JobConfig jobConfig) { if (await _scheduler.CheckExists(new JobKey(GetJobName(jobConfig), GetJobGroupName(jobConfig)))) // job是否已经存在 { throw new SpriteException("job 已经存在"); } // 判断假期 if (jobConfig.HolidayCalendarId.HasValue) { if (!(await _scheduler.GetCalendarNames()).Any(r => r.Contains(jobConfig.HolidayCalendarId.ToString()))) { await _serviceProvider.DoDapperServiceAsync(DefaultDbConfig, async (unitOfWork) => { IHolidayCalendarRepository holidayCalendarRepository = ConnectionFactory.GetConnectionProvider(DefaultDbConfig.ConnectionType).GetRepository <IHolidayCalendarRepository>(unitOfWork); var holidayCalendar = await holidayCalendarRepository.GetAsync(jobConfig.HolidayCalendarId.Value); if (holidayCalendar == null) { throw new SpriteException("未找到假期配置信息"); } await AddCalendar(holidayCalendar); }); } } IJobDetail job = CreateJobDetail(jobConfig); ITrigger trigger = CreateJobTriggerAsync(jobConfig, null); await _scheduler.ScheduleJob(job, trigger); }
private IJobDetail CreateJobDetail(JobConfig jobConfig) { JobBuilder jobBuilder; switch (jobConfig.JobExecType) { case EJobExecType.Api: jobBuilder = JobBuilder.Create <CallApiJob>(); break; case EJobExecType.Reflect: jobBuilder = JobBuilder.Create <CallReflectJob>(); break; default: jobBuilder = JobBuilder.Create <CallReflectJob>(); break; } jobBuilder.WithIdentity(GetJobName(jobConfig), GetJobGroupName(jobConfig)); if (!string.IsNullOrEmpty(jobConfig.Description)) { jobBuilder.WithDescription(jobConfig.Description); } jobBuilder.UsingJobData("Params", jobConfig.Params); jobBuilder.UsingJobData("ExecLocation", jobConfig.ExecLocation); IJobDetail job = jobBuilder.Build(); return(job); }
public async Task ResumeJob(JobConfig jobConfig) { await DoSchedulerMethod((jobKey) => { return(_scheduler.ResumeJob(jobKey)); }, jobConfig); }
public async Task TriggerJob(JobConfig jobConfig) { await DoSchedulerMethod((jobKey) => { return(_scheduler.TriggerJob(jobKey)); }, jobConfig); }
private void SetSimpleRepeatCount(JobConfig jobConfig, SimpleScheduleBuilder simpleScheduleBuilder) { if (jobConfig.ExecCount.HasValue) { simpleScheduleBuilder.WithRepeatCount(jobConfig.ExecCount.Value); } else { simpleScheduleBuilder.RepeatForever(); } }
private string GetJobGroupName(JobConfig jobConfig) { var jobGroupName = jobConfig.JobGroup; if (string.IsNullOrEmpty(jobConfig.JobGroup)) { jobGroupName = "Default"; } return(jobGroupName); }
private string GetJobName(JobConfig jobConfig, bool isJobName = true) { if (isJobName) { return($"{jobConfig.Id}_{jobConfig.JobName}_job"); } else { return($"{jobConfig.Id}_{jobConfig.JobName}_trigger"); } }
public async Task UnScheduleJob(JobConfig jobConfig) { if (await _scheduler.CheckExists(new JobKey(GetJobName(jobConfig), GetJobGroupName(jobConfig)))) // job是否已经存在,删除 { await _scheduler.DeleteJob(new JobKey(GetJobName(jobConfig), GetJobGroupName(jobConfig))); } else { throw new SpriteException("job 不存在"); } }
public async Task DoSchedulerMethod(Func <JobKey, Task> func, JobConfig jobConfig) { var jobKey = new JobKey(GetJobName(jobConfig), GetJobGroupName(jobConfig)); if (await _scheduler.CheckExists(jobKey)) // job是否已经存在,删除 { await func(jobKey); } else { throw new SpriteException("job 不存在"); } }
public async Task AddJob(JobConfig jobConfig) { jobConfig.Id = Guid.NewGuid(); await _serviceProvider.DoDapperServiceAsync(DefaultDbConfig, async (unitOfWork) => { IJobConfigRepository jobConfigRepository = ConnectionFactory.GetConnectionProvider(DefaultDbConfig.ConnectionType).GetRepository <IJobConfigRepository>(unitOfWork); await jobConfigRepository.InsertAsync(jobConfig); if (jobConfig.IsActive) { await _jobManager.ScheduleJob(jobConfig); } }); Logger.LogInformation($"AddJob:{JsonConvert.SerializeObject(jobConfig)}"); }
public async Task UpdateJob(JobConfig jobConfig) { await _serviceProvider.DoDapperServiceAsync(DefaultDbConfig, async (unitOfWork) => { IJobConfigRepository jobConfigRepository = ConnectionFactory.GetConnectionProvider(DefaultDbConfig.ConnectionType).GetRepository <IJobConfigRepository>(unitOfWork); var dbJobConfig = await jobConfigRepository.GetAsync(jobConfig.Id); if (dbJobConfig == null) { throw new SpriteException("未找到Job配置信息"); } await _jobManager.UnScheduleJob(dbJobConfig); await jobConfigRepository.UpdateAsync(jobConfig); await _jobManager.ScheduleJob(jobConfig); }); Logger.LogInformation($"UpdateJob:{JsonConvert.SerializeObject(jobConfig)}"); }
private ITrigger CreateJobTriggerAsync(JobConfig jobConfig, List <HolidayCalendar> holidayCalendars) { var nowTime = DateTime.Now; var triggerBuilder = TriggerBuilder.Create(); triggerBuilder.WithIdentity(GetJobName(jobConfig, false), GetJobGroupName(jobConfig)); // 设置开始时间 if (jobConfig.StartTime.HasValue) { triggerBuilder.StartAt(jobConfig.StartTime.Value); } else { triggerBuilder.StartNow(); } // 设置结束时间 if (jobConfig.EndTime.HasValue) { if (jobConfig.EndTime.Value < nowTime.AddSeconds(10)) { Logger.LogWarning($"{GetJobName(jobConfig)}小于启动时间,Job未执行调度"); return(null); } triggerBuilder.EndAt(jobConfig.EndTime.Value); } // 设置假期 if (jobConfig.HolidayCalendarId.HasValue) { if (holidayCalendars == null) { triggerBuilder.ModifiedByCalendar($"{jobConfig.HolidayCalendarId.Value}_calendar"); } else { var holidayCalendar = holidayCalendars.FirstOrDefault(r => r.Id == jobConfig.HolidayCalendarId.Value); if (holidayCalendar == null) { Logger.LogWarning($"{GetJobName(jobConfig)}设置了假期,但未找到假期数据"); } else { triggerBuilder.ModifiedByCalendar($"{jobConfig.HolidayCalendarId.Value}_calendar"); } } } // 优先级 if (jobConfig.Priority.HasValue) { triggerBuilder.WithPriority(jobConfig.Priority.Value); } switch (jobConfig.TriggerType) { case ETriggerType.Simple: if (!jobConfig.SimpleIntervalUnit.HasValue || !jobConfig.SimpleIntervalValue.HasValue) { Logger.LogWarning($"{GetJobName(jobConfig)}的简单作业,未设置执行参数"); throw new Exception($"{GetJobName(jobConfig)}的简单作业,未设置执行参数"); } switch (jobConfig.SimpleIntervalUnit.Value) { case EIntervalUnit.Second: triggerBuilder.WithSimpleSchedule(x => { x.WithIntervalInSeconds(jobConfig.SimpleIntervalValue.Value); SetSimpleRepeatCount(jobConfig, x); }); break; case EIntervalUnit.Minute: triggerBuilder.WithSimpleSchedule(x => { x.WithIntervalInMinutes(jobConfig.SimpleIntervalValue.Value); }); break; case EIntervalUnit.Hour: triggerBuilder.WithSimpleSchedule(x => { x.WithIntervalInHours(jobConfig.SimpleIntervalValue.Value); SetSimpleRepeatCount(jobConfig, x); }); break; } break; case ETriggerType.Cron: triggerBuilder.WithCronSchedule(jobConfig.CronConfig); break; case ETriggerType.At: break; } return(triggerBuilder.Build()); }