Example #1
0
 /// <summary>
 /// 根据JobId获取Job编辑信息
 /// </summary>
 /// <param name="JobId"></param>
 /// <returns></returns>
 public QuartzEditModel EditQuartz(string JobId)
 {
     try
     {
         QuartzEditModel result = new QuartzEditModel();
         JobInfo         job    = _sql.Search <JobInfo>(JobId);
         if (job == null)
         {
             throw new Exception("当前Job丢失!");
         }
         result.JobInfoId = JobId;
         result.JobGroup  = job.JobGroup;
         result.JobName   = job.JobName;
         result.JobDesc   = job.JobDesc;
         result.Cron      = job.Cron;
         result.CronDesc  = job.CronDesc;
         result.JobClass  = job.JobClass;
         return(result);
     }
     catch (Exception ex)
     {
         _log.Error(ex);
         throw ex;
     }
 }
Example #2
0
 /// <summary>
 /// 创建Quartz
 /// </summary>
 /// <param name="model"></param>
 /// <returns></returns>
 public string CreateOrUpdateQuartz(QuartzEditModel model)
 {
     try
     {
         if (string.IsNullOrWhiteSpace(model.JobGroup))
         {
             throw new Exception("Job分组不能为空!");
         }
         if (string.IsNullOrWhiteSpace(model.JobName))
         {
             throw new Exception("Job名不能为空!");
         }
         if (string.IsNullOrWhiteSpace(model.Cron))
         {
             throw new Exception("Cron表达式不能为空不能为空!");
         }
         if (string.IsNullOrWhiteSpace(model.JobClass))
         {
             throw new Exception("Job类名不能为空!");
         }
         _sql.OpenDb();
         if (string.IsNullOrEmpty(model.JobInfoId))
         {
             DataTable dt = _sql.Query("SELECT JOB_CLASS_NAME FROM QRTZ_JOB_DETAILS WITH(NOLOCK) WHERE JOB_GROUP = @group AND JOB_NAME = @name", new Dictionary <string, object> {
                 { "@group", model.JobGroup }, { "@name", model.JobName }
             });
             if (dt != null && dt.Rows.Count > 0)
             {
                 throw new Exception("当前Job已存在,请更改组名或Job名!");
             }
             quartz.CreateJob(_config.Job_Assembly, model);
             JobInfo job = new JobInfo();
             model.FillTableWithModel <JobInfo>(job);
             job.JobStatus = Constants.JobStatusRunning;
             _sql.Create(job);
         }
         else
         {
             JobInfo job = _sql.Search <JobInfo>(model.JobInfoId);
             model.FillTableWithModel <JobInfo>(job);
             _sql.Update(job);
         }
         return(Constants.SaveSuccessMssg);
     }
     catch (Exception ex)
     {
         _log.Error(ex);
         throw ex;
     }
     finally
     {
         _sql.CloseDb();
     }
 }
Example #3
0
 /// <summary>
 /// 处理Job
 /// </summary>
 /// <param name="jobId"></param>
 /// <param name="operate"></param>
 /// <returns></returns>
 public string HandleJobAsync(string jobId, string operate)
 {
     try
     {
         JobInfo         job   = _sql.Search <JobInfo>(jobId);
         QuartzEditModel model = new QuartzEditModel();
         model.JobGroup = job.JobGroup;
         model.JobName  = job.JobName;
         model.JobDesc  = job.JobDesc;
         model.Cron     = job.Cron;
         model.CronDesc = job.CronDesc;
         model.JobClass = job.JobClass;
         DataTable dt = _sql.Query(@"SELECT t.TRIGGER_STATE 
                                     FROM QRTZ_TRIGGERS t WITH(NOLOCK)
                                     WHERE t.JOB_GROUP = @group AND t.JOB_NAME = @name", new Dictionary <string, object> {
             { "@group", job.JobGroup }, { "@name", job.JobName }
         });
         if ((dt == null || dt.Rows.Count == 0) && operate != Constants.JobStart)
         {
             throw new Exception("当前处理的JOB不存在!");
         }
         if (operate == Constants.JobPause)
         {
             if (Cast.ConToString(dt.Rows[0]["TRIGGER_STATE"]) != Constants.TriggerStateWaitting)
             {
                 throw new Exception("当前Job状态无法暂停!");
             }
             job.JobStatus = Constants.JobStatusPause;
         }
         else if (operate == Constants.JobResume)
         {
             if (Cast.ConToString(dt.Rows[0]["TRIGGER_STATE"]) != Constants.TriggerStatePaused)
             {
                 throw new Exception("当前Job状态无法恢复!");
             }
             job.JobStatus = Constants.JobStatusRunning;
         }
         else if (operate == Constants.JobExcute)
         {
             if (Cast.ConToString(dt.Rows[0]["TRIGGER_STATE"]) != Constants.TriggerStatePaused)
             {
                 throw new Exception("请暂停后再执行Job!");
             }
         }
         else if (operate == Constants.JobStop)
         {
             job.JobStatus = Constants.JobStatusStop;
         }
         else if (operate == Constants.JobStart)
         {
             job.JobStatus = Constants.JobStatusRunning;
         }
         quartz.HandleJobAsync(operate, _config.Job_Assembly, model);
         _sql.OpenDb();
         _sql.Update(job);
         return(job.JobStatus);
     }
     catch (Exception ex)
     {
         _log.Error(ex);
         throw new Exception(ex.Message);
     }
     finally
     {
         _sql.CloseDb();
     }
 }
Example #4
0
 public string CreateOrUpdateQuartz(QuartzEditModel model)
 {
     return(new QuartzCommand(UserIdentity).CreateOrUpdateQuartz(model));
 }