public void Execute(IJobExecutionContext context) { try { ///获取所有执行中的任务 List <TaskModel> listTask = TaskHelper.GetAllTaskList().Where(e => e.IsDelete == 0).ToList <TaskModel>(); //开始对比当前配置文件和上一次配置文件之间的改变 //2.新增的任务(TaskID在原集合不存在) var addJobList = (from p in listTask where !(from q in QuartzHelper.CurrentTaskList select q.TaskID).Contains(p.TaskID) select p).ToList(); foreach (var taskUtil in addJobList) { try { QuartzHelper.ScheduleJob(taskUtil); //添加新增的任务 QuartzHelper.CurrentTaskList.Add(taskUtil); } catch (Exception e) { LogHelper.WriteLog("自动增加任务失败,错误:" + e.Message + e.StackTrace); } } } catch (Exception ex) { JobExecutionException e2 = new JobExecutionException(ex); //1.立即重新执行任务 e2.RefireImmediately = true; } }
public TaskModule() : base("Task") { //任务列表 Get["/Grid"] = r => { return(View["Grid"]); }; //任务编辑界面 Get["/Edit"] = r => { return(View["Edit"]); }; #region "取数接口API" //立即运行一次任务 Get["/Run/{Id}"] = r => { //取出单条记录数据 string TaskId = r.Id; ApiResult <string> result = new ApiResult <string>(); try { TaskHelper.RunById(TaskId); } catch (Exception ex) { result.HasError = true; result.Message = ex.Message; } return(Response.AsJson(result)); }; Get["/GetById/{Id}"] = r => { ApiResult <TaskUtil> result = new ApiResult <TaskUtil>(); try { //取出单条记录数据 string TaskId = r.Id; result.Result = TaskHelper.GetById(TaskId); } catch (Exception ex) { result.HasError = true; result.Message = ex.Message; } return(Response.AsJson(result)); }; //列表查询接口 Post["/PostQuery"] = r => { QueryCondition condition = this.Bind <QueryCondition>(); return(Response.AsJson(TaskHelper.Query(condition))); }; //重新加载任务接口 Post["/Reload"] = r => { ApiResult <string> result = new ApiResult <string>(); try { List <string> listTaskId = this.Bind <List <string> >(); foreach (string TaskId in listTaskId) { var taskUtil = TaskHelper.GetById(TaskId); QuartzHelper.ScheduleJob(taskUtil, true); } } catch (Exception ex) { result.HasError = true; result.Message = ex.Message; } return(Response.AsJson(result)); }; //保存数据 Post["/"] = r => { TaskUtil TaskUtil = this.Bind <TaskUtil>(); return(Response.AsJson(TaskHelper.SaveTask(TaskUtil))); }; //更新数据 Put["/"] = r => { TaskUtil TaskUtil = this.Bind <TaskUtil>(); return(Response.AsJson(TaskHelper.SaveTask(TaskUtil))); }; //删除任务接口 Delete["/{Id}"] = r => { ApiResult <string> result = new ApiResult <string>(); try { string TaskId = r.Id; TaskHelper.DeleteById(TaskId); } catch (Exception ex) { result.HasError = true; result.Message = ex.Message; } return(Response.AsJson(result)); }; //更新任务运行状态 Put["/{Id}/{Status:int}"] = r => { ApiResult <string> result = new ApiResult <string>(); try { TaskStatus Status = Enum.ToObject(typeof(TaskStatus), r.Status); string TaskId = r.Id; TaskHelper.UpdateTaskStatus(TaskId, Status); } catch (Exception ex) { result.HasError = true; result.Message = ex.Message; } return(Response.AsJson(result)); }; #endregion }
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; } }