Beispiel #1
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();
     }
 }