Beispiel #1
0
 /// <summary>
 /// 调用任务API,触发任务调度
 /// </summary>
 private async Task CallTaskApi(int jobID)
 {
     await Task.Run(() =>
     {
         TaskJobInfo taskJob = _yunYingRepository.GetModel <TaskJobInfo>(new { JobID = jobID });
         if (taskJob == null)
         {
             _logger.LogWarning($"Can not find the taskjob -> JobID[{jobID}]");
             return;
         }
         if (string.IsNullOrEmpty(taskJob.ApiUrl))
         {
             _logger.LogWarning($"the call apiUrl of taskjob is null-> JobID[{jobID}]");
             return;
         }
         _logger.LogInformation($"start CallTaskApi -> ApiUrl[{taskJob.ApiUrl}]");
         try
         {
             //call api url
             using (var httpClient = new HttpClient())
             {
                 var result = httpClient.GetStringAsync(taskJob.ApiUrl).Result;
                 _logger.LogInformation($"CallTaskApi success->JobID[{jobID}],Result[{result}]");
                 taskJob.LastExecuteTime = DateTime.Now;
                 _yunYingRepository.Update(taskJob);
             }
         }
         catch (Exception ex)
         {
             _logger.LogError(ex, $"CallTaskApi failed -> ApiUrl[{taskJob.ApiUrl}]");
         }
     });
 }
Beispiel #2
0
        public async Task <ActionResult> Add(TaskJobInfo info)
        {
            AjaxResult ajaxResult = new AjaxResult();

            if (info.JobID == 0)
            {
                info.LastExecuteTime = DateTime.Now;
                var jobId = _yunYingRepository.Insert(info);
                if (jobId > 0)
                {
                    info.JobID = (int)jobId;
                    await _quartzManager.CreateJob(info);

                    ajaxResult.IsOk = true;
                    ajaxResult.Msg  = "添加成功";
                }
                else
                {
                    ajaxResult.IsOk = false;
                    ajaxResult.Msg  = "添加失败";
                }
            }
            else
            {
                var    updateInfo = _yunYingRepository.GetModel <TaskJobInfo>(new { JobId = info.JobID });
                string oldJobName = updateInfo.JobName;
                updateInfo.JobName        = info.JobName;
                updateInfo.JobDesc        = info.JobDesc;
                updateInfo.JobClassName   = info.JobClassName;
                updateInfo.CronExpression = info.CronExpression;
                updateInfo.ApiUrl         = Convert.ToString(info.ApiUrl);
                if (_yunYingRepository.Update(updateInfo) > 0)
                {
                    await _quartzManager.DeleteJob(oldJobName);

                    await _quartzManager.CreateJob(updateInfo);

                    ajaxResult.IsOk = true;
                    ajaxResult.Msg  = "更新成功";
                }
                else
                {
                    ajaxResult.IsOk = false;
                    ajaxResult.Msg  = "更新失败";
                }
            }
            return(Json(ajaxResult));
        }