Exemple #1
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 });
 }
Exemple #2
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);
        }
Exemple #3
0
 public static void RunById(string TaskID)
 {
     QuartzHelper.RunOnceTask(TaskID);
 }