Example #1
0
        public void Execute(IJobExecutionContext context)
        {
            //获得requestUrl和requestType
            JobDataMap dataMap = context.JobDetail.JobDataMap;
            string     jobJson = dataMap.GetString("jobJson");

            writeLog($"jobJson:{ jobJson}");
            var result = JsonConvert.DeserializeObject <SchedulerRequest>(jobJson);

            try
            {
                writeLog($"jobJson:{ result.RequestUrl}", "1");
                var        resultMsg = string.Empty;
                HttpClient client    = new HttpClient();
                //GET请求
                if (result.RequestType == (int)JobEnum.RequestType.Get)
                {
                    resultMsg = client.GetAsync(result.RequestUrl).Result.Content.ReadAsStringAsync().Result;
                    writeLog($"GET请求resultMsg:{ resultMsg}");
                }
                //POST请求
                else if (result.RequestType == (int)JobEnum.RequestType.Post)
                {
                    SchedulerRequest re = new SchedulerRequest();
                    var content         = JsonConvert.SerializeObject(re);
                    resultMsg = client.PostAsync(result.RequestUrl, new StringContent(content)).Result.Content.ReadAsStringAsync().Result;
                    writeLog($"POST请求resultMsg:{ resultMsg}");
                }
                //保存结果到数据库
                if (resultMsg.ToUpper().Contains("SUCCESS"))
                {
                    result.LogStatus = (int)JobEnum.LogStatus.SuccessInfo;
                }
                else
                {
                    result.LogStatus = (int)JobEnum.LogStatus.FailedInfo;
                }
                result.ReturnMsg = resultMsg.Length > 800 ? resultMsg.Substring(0, 800) : resultMsg;
                SchedulerConfigDB.InsertLog(result);
            }
            catch (Exception ex)
            {
                //设置将自动去除这个任务的触发器,所以这个任务不会再执行
                //JobExecutionException ex_job = new JobExecutionException(ex);
                //ex_job.UnscheduleAllTriggers = true;
                //保存到数据库
                result.ReturnMsg = "IJob-Exception: " + (ex.ToString().Length > 450 ? ex.ToString().Substring(0, 450) : ex.ToString());
                result.LogStatus = (int)JobEnum.LogStatus.ExceptInfo;
                SchedulerConfigDB.InsertLog(result);
            }
        }
Example #2
0
      private bool ExecuteWebApi(SchedulerConfigModel list)
      {
          try
          {
              var             flag      = true;
              SchedulerHelper scheduler = new SchedulerHelper();

              SchedulerRequest request = new SchedulerRequest();
              request.JobName          = list.JobName;
              request.JobGroup         = list.JobGroup;
              request.TriggerName      = list.JobName + "Trigger";
              request.TriggerGroupName = list.JobGroup + "Trigger";
              request.CronTab          = list.CronTab;
              request.StartTime        = list.StartTime;
              if (list.EndTime != null)
              {
                  request.EndTime = list.EndTime;
              }
              else
              {
                  request.EndTime = null;
              }
              request.RequestType = list.RequestType;
              request.RequestUrl  = list.RequestUrl;

              var json = JsonConvert.SerializeObject(request);

              DateTimeOffset?end = null;
              if (request.EndTime != null)
              {
                  end = DateTime.SpecifyKind(Convert.ToDateTime(request.EndTime), DateTimeKind.Local);
                  if (request.EndTime < DateTime.Now)
                  {
                      flag = false;
                  }
              }
              if (flag)
              {
                  IJobDetail jobDetail = JobBuilder.Create <FileJob>()
                                         .WithIdentity(request.JobName, request.JobGroup)
                                         .UsingJobData("jobJson", json)
                                         .Build();

                  CronTriggerImpl tigger = (CronTriggerImpl)TriggerBuilder.Create()
                                           .WithIdentity(request.TriggerName, request.TriggerGroupName)
                                           .WithCronSchedule(request.CronTab)
                                           .ForJob(request.JobName, request.JobGroup)
                                           .StartNow()
                                           .EndAt(end)
                                           .Build();

                  DateTimeOffset dt = scheduler.scheduleJob(jobDetail, tigger);
              }
              else
              {
              }

              return(true);
          }
          catch (Exception ex)
          {
              //插入日志
              SchedulerRequest request = new SchedulerRequest();
              request.JobGroup    = list.JobGroup;
              request.JobName     = list.JobName;
              request.RequestType = list.RequestType;
              request.RequestUrl  = list.RequestUrl;
              request.ReturnMsg   = "Job-Exception: " + (ex.ToString().Length > 450 ? ex.ToString().Substring(0, 450) : ex.ToString());
              request.LogStatus   = (int)JobEnum.LogStatus.ExceptInfo;
              SchedulerConfigDB.InsertLog(request);
              //数据状态更新为0,防止重复执行
              SchedulerConfigDB.UpdateStatus(list.ID);
              return(false);
          }
      }