/// <summary> /// 添加作业 /// </summary> /// <param name="taskOptions"></param> /// <param name="schedulerFactory"></param> /// <param name="init">是否初始化,否=需要重新生成配置文件,是=不重新生成配置文件</param> /// <returns></returns> public static async Task <object> AddJob(this TaskOptions taskOptions, ISchedulerFactory schedulerFactory, bool init = false) { try { //元组用于验证CRON表达式是否正确 (bool, string)validExpression = taskOptions.Interval.IsValidExpression(); if (!validExpression.Item1) { return new { status = false, msg = validExpression.Item2 } } ; //元组用于验证作业是否已经存在 (bool, object)result = taskOptions.Exists(init); if (!result.Item1) { return(result.Item2); } if (!init) { _taskList.Add(taskOptions); FileQuartz.WriteJobConfig(_taskList); } IJobDetail job = JobBuilder.Create <HttpResultful>() .WithIdentity(taskOptions.TaskName, taskOptions.GroupName) .Build(); ITrigger trigger = TriggerBuilder.Create() .WithIdentity(taskOptions.TaskName, taskOptions.GroupName) .StartNow().WithDescription(taskOptions.Describe) .WithCronSchedule(taskOptions.Interval) .Build(); IScheduler scheduler = await schedulerFactory.GetScheduler(); await scheduler.ScheduleJob(job, trigger); if (taskOptions.Status == (int)TriggerState.Normal) { await scheduler.Start(); } else { await schedulerFactory.Pause(taskOptions); //TODO:更新日志 FileQuartz.WriteStartLog($"作业:{taskOptions.TaskName},分组:{taskOptions.GroupName},新建时未启动原因,状态为:{taskOptions.Status}"); } //TODO:更新动作日志 if (!init) { FileQuartz.WriteJobAction(JobAction.新增, taskOptions.TaskName, taskOptions.GroupName); } } catch (Exception ex) { return(new { status = false, msg = ex.Message }); } return(new { status = true }); }
/// <summary> /// 添加作业 /// </summary> /// <param name="taskOptions"></param> /// <param name="schedulerFactory"></param> /// <param name="init">是否初始化,false持久化,true不持久化</param> /// <returns></returns> public static async Task<object> AddJob(this TaskOptions taskOptions, ISchedulerFactory schedulerFactory, bool init = false) { try { (bool, string) validExpression = taskOptions.interval.IsValidExpression(); if (!validExpression.Item1) return new { status = false, msg = validExpression.Item2 }; (bool, object) result = taskOptions.Exists(init); if (!result.Item1) return result.Item2; if (!init) { TaskRepository.AddTask(taskOptions); } IJobDetail job = JobBuilder.Create<HttpResultful>() .WithIdentity(taskOptions.name, taskOptions.group_name) .Build(); ITrigger trigger = TriggerBuilder.Create() .WithIdentity(taskOptions.name, taskOptions.group_name) .StartNow().WithDescription(taskOptions.describe) .WithCronSchedule(taskOptions.interval) .Build(); IScheduler scheduler = await schedulerFactory.GetScheduler(); await scheduler.ScheduleJob(job, trigger); //如果任务处于正常状态 if (taskOptions.status == (int)TriggerState.Normal) { await scheduler.Start(); } else { //暂定任务 await schedulerFactory.Pause(taskOptions); } } catch (Exception ex) { return new { status = false, msg = ex.Message }; } return new { status = true }; }
/// <summary> /// 添加作业 /// </summary> /// <param name="taskOptions"></param> /// <param name="schedulerFactory"></param> /// <param name="init">是否初始化,否=需要重新生成配置文件,是=不重新生成配置文件</param> /// <returns></returns> public static async Task <object> AddJob(this TaskOptions taskOptions, ISchedulerFactory schedulerFactory, bool init = false, IJobFactory jobFactory = null) { try { (bool, string)validExpression = taskOptions.Interval.IsValidExpression(); if (!validExpression.Item1) { return new { status = false, msg = validExpression.Item2 } } ; (bool, object)result = taskOptions.Exists(init); if (!result.Item1) { return(result.Item2); } if (!init) { _taskList.Add(taskOptions); FileQuartz.WriteJobConfig(_taskList); } IJobDetail job = JobBuilder.Create <HttpResultfulJob>() .WithIdentity(taskOptions.TaskName, taskOptions.GroupName) .Build(); ITrigger trigger = TriggerBuilder.Create() .WithIdentity(taskOptions.TaskName, taskOptions.GroupName) .StartNow() .WithDescription(taskOptions.Describe) .WithCronSchedule(taskOptions.Interval) .Build(); IScheduler scheduler = await schedulerFactory.GetScheduler(); if (jobFactory == null) { try { jobFactory = HttpContext.Current.RequestServices.GetService <IJobFactory>(); } catch (Exception ex) { Console.WriteLine($"创建任务[{taskOptions.TaskName}]异常,{ex.Message}"); } } if (jobFactory != null) { scheduler.JobFactory = jobFactory; } await scheduler.ScheduleJob(job, trigger); if (taskOptions.Status == (int)TriggerState.Normal) { await scheduler.Start(); } else { await schedulerFactory.Pause(taskOptions); FileQuartz.WriteStartLog($"作业:{taskOptions.TaskName},分组:{taskOptions.GroupName},新建时未启动原因,状态为:{taskOptions.Status}"); } if (!init) { FileQuartz.WriteJobAction(JobAction.新增, taskOptions.TaskName, taskOptions.GroupName); } } catch (Exception ex) { return(new { status = false, msg = ex.Message }); } return(new { status = true }); }