public JsonResult DeleteJob(List <JobDeleteViewModel> idList) { var result = new ResponseResult(); try { foreach (var item in idList) { QuartzHelper.DeleteJob(item.JobName, item.GroupName).GetAwaiter(); } result.success = true; } catch (Exception ex) { result.success = false; result.message = ex.Message; } return(Json(result)); }
public void Execute(IJobExecutionContext context) { try { TaskLog.ConfigLogInfo.WriteLogE("Job修改任务开始,当前系统时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); ///获取所有执行中的任务 List <TaskUtil> listTask = TaskHelper.ReadConfig().Where(e => e.IsExcute).ToList <TaskUtil>(); //开始对比当前配置文件和上一次配置文件之间的改变 //1.修改的任务 var UpdateJobList = (from p in listTask from q in TaskHelper.CurrentTaskList where p.TaskID == q.TaskID && (p.TaskParam != q.TaskParam || p.Assembly != q.Assembly || p.Class != q.Class || p.CronExpressionString != q.CronExpressionString ) select new { NewTaskUtil = p, OriginTaskUtil = q }).ToList(); foreach (var item in UpdateJobList) { try { QuartzHelper.ScheduleJob(item.NewTaskUtil); //修改原有的任务 int index = TaskHelper.CurrentTaskList.IndexOf(item.OriginTaskUtil); TaskHelper.CurrentTaskList[index] = item.NewTaskUtil; } catch (Exception e) { TaskLog.ConfigLogError.WriteLogE(string.Format("任务“{0}”配置信息更新失败!", item.NewTaskUtil.TaskName), e); } } //2.新增的任务(TaskID在原集合不存在) var AddJobList = (from p in listTask where !(from q in TaskHelper.CurrentTaskList select q.TaskID).Contains(p.TaskID) select p).ToList(); foreach (var taskUtil in AddJobList) { try { QuartzHelper.ScheduleJob(taskUtil); //添加新增的任务 TaskHelper.CurrentTaskList.Add(taskUtil); } catch (Exception e) { TaskLog.ConfigLogError.WriteLogE(string.Format("任务“{0}”新增失败!", taskUtil.TaskName), e); } } //3.删除的任务 var DeleteJobList = (from p in TaskHelper.CurrentTaskList where !(from q in listTask select q.TaskID).Contains(p.TaskID) select p).ToList(); foreach (var taskUtil in DeleteJobList) { try { QuartzHelper.DeleteJob(taskUtil.TaskID); //添加新增的任务 TaskHelper.CurrentTaskList.Remove(taskUtil); } catch (Exception e) { TaskLog.ConfigLogError.WriteLogE(string.Format("任务“{0}”删除失败!", taskUtil.TaskName), e); } } if (UpdateJobList.Count > 0 || AddJobList.Count > 0 || DeleteJobList.Count > 0) { TaskLog.ConfigLogInfo.WriteLogE("Job修改任务执行完成后,系统当前的所有任务信息:" + JsonConvert.SerializeObject(TaskHelper.CurrentTaskList)); } else { TaskLog.ConfigLogInfo.WriteLogE("当前没有修改的任务"); } } catch (Exception ex) { JobExecutionException e2 = new JobExecutionException(ex); TaskLog.ConfigLogError.WriteLogE("Job修改任务异常", ex); //1.立即重新执行任务 e2.RefireImmediately = true; } }
public void Execute(IJobExecutionContext context) { try { //获取所有数据库或者配置文件中的任务 List <TaskModel> listTask = TaskHelper.GetAllTaskList().Where(e => e.IsDelete == 0).ToList <TaskModel>(); //开始对比当前配置文件和上一次配置文件之间的改变 // 1. 停用或启用 //1.修改的任务 var StatusJobList = (from p in listTask from q in QuartzHelper.CurrentTaskList where p.TaskID == q.TaskID && p.Status != q.Status select p).ToList(); foreach (var item in StatusJobList) { try { if (item.Status == TaskStatus.RUN) { QuartzHelper.ResumeJob(item.TaskID.ToString()); } else { QuartzHelper.PauseJob(item.TaskID.ToString()); } } catch (Exception e) { LogHelper.WriteLog("自动暂停任务失败,错误:" + e.Message + e.StackTrace); } } //2.修改的任务 var UpdateJobList = (from p in listTask from q in QuartzHelper.CurrentTaskList where p.TaskID == q.TaskID && (p.TaskParam != q.TaskParam || p.AssemblyName != q.AssemblyName || p.ClassName != q.ClassName || p.CronExpressionString != q.CronExpressionString ) select new { NewTaskModel = p, OriginTaskUtil = q }).ToList(); foreach (var item in UpdateJobList) { try { QuartzHelper.ScheduleJob(item.NewTaskModel, true); //修改原有的任务 int index = QuartzHelper.CurrentTaskList.IndexOf(item.OriginTaskUtil); QuartzHelper.CurrentTaskList[index] = item.NewTaskModel; } catch (Exception e) { LogHelper.WriteLog("自动修改任务失败,错误:" + e.Message + e.StackTrace); } } //3.新增的任务(TaskID在原集合不存在) var AddJobList = (from p in listTask where !(from q in QuartzHelper.CurrentTaskList select q.TaskID).Contains(p.TaskID) select p).ToList(); foreach (var taskModel in AddJobList) { try { QuartzHelper.ScheduleJob(taskModel); //添加新增的任务 QuartzHelper.CurrentTaskList.Add(taskModel); } catch (Exception e) { LogHelper.WriteLog("自动增加任务失败,错误:" + e.Message + e.StackTrace); } } //4.删除的任务 var DeleteJobList = (from p in QuartzHelper.CurrentTaskList where !(from q in listTask select q.TaskID).Contains(p.TaskID) select p).ToList(); foreach (var taskModel in DeleteJobList) { try { QuartzHelper.DeleteJob(taskModel.TaskID.ToString()); //移除删除的任务 QuartzHelper.CurrentTaskList.Remove(taskModel); } catch (Exception e) { LogHelper.WriteLog("自动删除任务失败,错误:" + e.Message + e.StackTrace); } } if (UpdateJobList.Count > 0 || AddJobList.Count > 0 || DeleteJobList.Count > 0) { LogHelper.WriteLog("Job修改任务执行完成后,系统当前的所有任务信息:" + JsonHelper.ToJson(QuartzHelper.CurrentTaskList)); } else { LogHelper.WriteLog("当前没有修改的任务"); } } catch (Exception ex) { JobExecutionException e2 = new JobExecutionException(ex); //1.立即重新执行任务 e2.RefireImmediately = true; } }