public async Task <IActionResult> CreateOrUpdate(string group = "", string name = "") { JobCreateOrUpdateRequest model = new JobCreateOrUpdateRequest { Group = group, TransferType = (int)TransferTypeEnum.SharedFolder, UserName = @"administrator", Password = "******", SourceRootPath = @"\\10.48.68.205", SourceFilePattern = @"^(?<fpath>(\\\\)([\s\.\-\w]+\\)*)(?<fname>[\w]+.[\w]+)", DestinationRootPath = @"D:\", DestinationFilePattern = "${fname}", TriggerType = (int)TriggerTypeEnum.Simple, Interval = 60, IntervalType = (int)IntervalTypeEnum.Minute, IsUpdate = false }; if (!string.IsNullOrWhiteSpace(group) && !string.IsNullOrWhiteSpace(name)) { /**********不为空,说明是更新任务**********/ JobKey key = new JobKey(name.Trim(), group.Trim()); IJobDetail job = await _scheduler.GetJobDetail(key); model = new JobCreateOrUpdateRequest { Name = job.Key.Name, Group = job.Key.Group, TransferType = job.JobDataMap.GetTransferType(), UserName = job.JobDataMap.GetUserName(), Password = job.JobDataMap.GetPassword(), SourceRootPath = job.JobDataMap.GetSourceRootPath(), SourceFilePattern = job.JobDataMap.GetSourceFilePattern(), DestinationRootPath = job.JobDataMap.GetDestinationRootPath(), DestinationFilePattern = job.JobDataMap.GetDestinationFilePattern(), DeleteOnCopied = job.JobDataMap.GetDeleteOnCopied(), StartTime = job.JobDataMap.GetStartTime(), EndTime = job.JobDataMap.GetEndTime(), TriggerType = job.JobDataMap.GetTriggerType(), Interval = job.JobDataMap.GetInterval(), IntervalType = job.JobDataMap.GetIntervalType(), RepeatCount = job.JobDataMap.GetRepeatCount(), Cron = job.JobDataMap.GetCron(), Description = job.Description, IsUpdate = true }; } model.Properties.Add(PropertiesKeys.Key_1, TransferTypeEnum.SharedFolder.ToSelectList()); model.Properties.Add(PropertiesKeys.Key_2, TriggerTypeEnum.Simple.ToSelectList()); model.Properties.Add(PropertiesKeys.Key_3, IntervalTypeEnum.Second.ToSelectList()); return(View(model)); }
public async Task <IActionResult> CreateOrUpdate(string group = "", string name = "") { JobCreateOrUpdateRequest model = new JobCreateOrUpdateRequest { Group = group, HttpMethod = (int)HttpMethodEnum.Get, TriggerType = (int)TriggerTypeEnum.Simple, Interval = 60, IsUpdate = false }; if (!string.IsNullOrWhiteSpace(group) && !string.IsNullOrWhiteSpace(name)) { /**********不为空,说明是更新任务**********/ JobKey key = new JobKey(name.Trim(), group.Trim()); IJobDetail job = await _scheduler.GetJobDetail(key); model = new JobCreateOrUpdateRequest { Name = job.Key.Name, Group = job.Key.Group, HttpMethod = job.JobDataMap.GetHttpMethod(), RequestUrl = job.JobDataMap.GetRequestUrl(), StartTime = job.JobDataMap.GetStartTime(), EndTime = job.JobDataMap.GetEndTime(), TriggerType = job.JobDataMap.GetTriggerType(), Interval = job.JobDataMap.GetInterval(), IntervalType = job.JobDataMap.GetIntervalType(), RepeatCount = job.JobDataMap.GetRepeatCount(), Cron = job.JobDataMap.GetCron(), RequestBody = job.JobDataMap.GetRequestBody(), Description = job.Description, IsUpdate = true }; } model.Properties.Add(PropertiesKeys.Key_1, HttpMethodEnum.Get.ToSelectList()); model.Properties.Add(PropertiesKeys.Key_2, TriggerTypeEnum.Simple.ToSelectList()); model.Properties.Add(PropertiesKeys.Key_3, IntervalTypeEnum.Second.ToSelectList()); return(View(model)); }
public async Task CreateOrUpdate(JobCreateOrUpdateRequest request) { request.Name = request.Name.Trim(); request.Group = request.Group.Trim(); JobKey key = new JobKey(request.Name, request.Group); if (await _scheduler.CheckExists(key)) { if (!request.IsUpdate) { throw new Exception("已存在相同名称的任务"); // 新增时,存在相同任务,不创建 } else { await _scheduler.DeleteJob(key); // 更新时,先删除,再创建 } } ; /******Data*****/ JobDataMap dataMap = new JobDataMap(); dataMap.Put(DataKeys.HttpMethod, request.HttpMethod); dataMap.Put(DataKeys.RequestUrl, request.RequestUrl); dataMap.Put(DataKeys.TriggerType, request.TriggerType); dataMap.Put(DataKeys.RepeatCount, request.RepeatCount); dataMap.Put(DataKeys.Interval, request.Interval); dataMap.Put(DataKeys.IntervalType, request.IntervalType); dataMap.Put(DataKeys.Cron, request.Cron); dataMap.Put(DataKeys.RequestBody, request.RequestBody); dataMap.Put(DataKeys.CreateTime, DateTime.Now.ToString()); dataMap.Put(DataKeys.StartTime, request.StartTime.ToString()); dataMap.Put(DataKeys.EndTime, request.EndTime.HasValue ? request.EndTime.Value.ToString() : string.Empty); /******Job*****/ IJobDetail job = JobBuilder.Create <HttpJob>() .StoreDurably(true) // 是否持久化, 无关联触发器时是否移除,false:移除 .RequestRecovery() // 重启后是否恢复任务 .WithDescription(request.Description ?? string.Empty) .WithIdentity(request.Name, request.Group) .UsingJobData(dataMap) .Build(); /******Trigger*****/ TriggerBuilder builder = TriggerBuilder.Create() .WithIdentity(request.Name, request.Group) .StartAt(request.StartTime.Value) .ForJob(job); if (request.EndTime.HasValue) { builder.EndAt(request.EndTime.Value); } if (request.TriggerType == (int)TriggerTypeEnum.Simple) { builder.WithSimpleSchedule(simple => { if (request.IntervalType == (int)IntervalTypeEnum.Second) { simple.WithIntervalInSeconds(request.Interval); } if (request.IntervalType == (int)IntervalTypeEnum.Minute) { simple.WithIntervalInMinutes(request.Interval); } if (request.IntervalType == (int)IntervalTypeEnum.Hour) { simple.WithIntervalInHours(request.Interval); } if (request.IntervalType == (int)IntervalTypeEnum.Day) { simple.WithIntervalInHours(request.Interval * 24); } if (request.RepeatCount > 0) { simple.WithRepeatCount(request.RepeatCount); } else { simple.RepeatForever(); } simple.WithMisfireHandlingInstructionFireNow(); // 如果延迟执行了 }); } else { builder.WithCronSchedule(request.Cron, cron => { cron.WithMisfireHandlingInstructionFireAndProceed(); }); } ITrigger trigger = builder.Build(); await _scheduler.ScheduleJob(job, trigger); // 加入调度,并持久化 FlushCache(); }