示例#1
0
        /// <summary>
        /// 保存任务
        /// </summary>
        /// <param name="value">任务</param>
        /// <returns>保存结果</returns>
        public static JsonBaseModel <string> SaveTask(TaskUtil value)
        {
            JsonBaseModel <string> result = new JsonBaseModel <string>();

            result.HasError = true;
            if (value == null)
            {
                result.Message = "参数空异常";
                return(result);
            }

            #region "校验"
            if (string.IsNullOrEmpty(value.TaskName))
            {
                result.Message = "任务名称不能为空";
                return(result);
            }
            if (string.IsNullOrEmpty(value.Assembly))
            {
                result.Message = "程序集名称不能为空";
                return(result);
            }
            if (string.IsNullOrEmpty(value.CronExpressionString))
            {
                result.Message = "Cron表达式不能为空";
                return(result);
            }
            if (!QuartzHelper.ValidExpression(value.CronExpressionString))
            {
                result.Message = "Cron表达式格式不正确";
                return(result);
            }
            if (string.IsNullOrEmpty(value.CronRemark))
            {
                result.Message = "表达式说明不能为空";
                return(result);
            }
            if (string.IsNullOrEmpty(value.Class))
            {
                result.Message = "类名不能为空";
                return(result);
            }
            #endregion

            JsonBaseModel <DateTime> cronResult = null;
            try
            {
                //新增
                if (value.TaskID == Guid.Empty)
                {
                    value.TaskID = Guid.NewGuid();
                    //任务状态处理

                    cronResult = GetTaskeLastRunTime(value.CronExpressionString);
                    if (cronResult.HasError)
                    {
                        result.Message = cronResult.Message;
                        return(result);
                    }
                    else
                    {
                        value.LastRunTime = cronResult.Result;
                    }
                    //添加新任务
                    QuartzHelper.ScheduleJob(value);

                    SQLHelper.ExecuteNonQuery(InsertSQL, value);
                }
                else
                {
                    value.ModifyOn = DateTime.Now;
                    TaskUtil srcTask = GetById(value.TaskID.ToString());

                    //表达式改变了重新计算下次运行时间
                    if (!value.CronExpressionString.Equals(srcTask.CronExpressionString, StringComparison.OrdinalIgnoreCase))
                    {
                        cronResult = GetTaskeLastRunTime(value.CronExpressionString);
                        if (cronResult.HasError)
                        {
                            result.Message = cronResult.Message;
                            return(result);
                        }
                        else
                        {
                            value.LastRunTime = cronResult.Result;
                        }

                        //更新任务
                        QuartzHelper.ScheduleJob(value, true);
                    }
                    else
                    {
                        value.LastRunTime = srcTask.LastRunTime;
                    }

                    SQLHelper.ExecuteNonQuery(UpdateSQL, value);
                }

                result.HasError = false;
                result.Result   = value.TaskID.ToString();
            }
            catch (Exception ex)
            {
                result.HasError = true;
                result.Message  = ex.Message;
            }
            return(result);
        }
示例#2
0
 /// <summary>
 /// 获取所有启用的任务
 /// </summary>
 /// <returns>所有启用的任务</returns>
 public static List <TaskUtil> ReadConfig()
 {
     return(SQLHelper.ToList <TaskUtil>("SELECT * FROM p_Task"));
 }
示例#3
0
 /// <summary>
 /// 删除指定id任务
 /// </summary>
 /// <param name="TaskID">任务id</param>
 public static void DeleteById(string TaskID)
 {
     QuartzHelper.DeleteJob(TaskID);
     SQLHelper.ExecuteNonQuery("DELETE FROM p_Task WHERE TaskID=@TaskID", new { TaskID = TaskID });
 }
示例#4
0
 /// <summary>
 /// 更新任务最近运行时间
 /// </summary>
 /// <param name="TaskID">任务id</param>
 public static void UpdateRecentRunTime(string TaskID, DateTime LastRunTime)
 {
     SQLHelper.ExecuteNonQuery("UPDATE p_Task SET RecentRunTime=GETDATE(),LastRunTime=@LastRunTime WHERE TaskID=@TaskID", new { TaskID = TaskID, LastRunTime = LastRunTime });
 }
示例#5
0
 /// <summary>
 /// 获取指定id任务数据
 /// </summary>
 /// <param name="TaskID">任务id</param>
 /// <returns>任务数据</returns>
 public static TaskUtil GetById(string TaskID)
 {
     return(SQLHelper.Single <TaskUtil>("SELECT * FROM p_Task WHERE TaskID=@TaskID", new { TaskID = TaskID }));
 }