|
| 1 | +using Microsoft.EntityFrameworkCore; |
| 2 | +using Microsoft.Extensions.Configuration; |
| 3 | +using Quartz; |
| 4 | +using Quartz.Impl; |
| 5 | +using Quartz.Impl.Triggers; |
| 6 | +using System; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Linq; |
| 9 | +using System.Net.Http; |
| 10 | +using System.Threading.Tasks; |
| 11 | +using VOL.Core.Configuration; |
| 12 | +using VOL.Core.EFDbContext; |
| 13 | +using VOL.Core.Utilities; |
| 14 | +using VOL.Entity.DomainModels; |
| 15 | + |
| 16 | +namespace VOL.Core.Quartz |
| 17 | +{ |
| 18 | + public class HttpResultfulJob : IJob |
| 19 | + { |
| 20 | + readonly IHttpClientFactory _httpClientFactory; |
| 21 | + |
| 22 | + readonly IServiceProvider _serviceProvider; |
| 23 | + /// <summary> |
| 24 | + /// 2020.05.31增加构造方法 |
| 25 | + /// </summary> |
| 26 | + /// <param name="serviceProvider"></param> |
| 27 | + /// <param name="httpClientFactory"></param> |
| 28 | + public HttpResultfulJob(IServiceProvider serviceProvider, IHttpClientFactory httpClientFactory) |
| 29 | + { |
| 30 | + _httpClientFactory = httpClientFactory; |
| 31 | + _serviceProvider = serviceProvider; |
| 32 | + } |
| 33 | + public async Task Execute(IJobExecutionContext context) |
| 34 | + { |
| 35 | + DateTime dateTime = DateTime.Now; |
| 36 | + Sys_QuartzOptions taskOptions = context.GetTaskOptions(); |
| 37 | + string httpMessage = ""; |
| 38 | + AbstractTrigger trigger = (context as JobExecutionContextImpl).Trigger as AbstractTrigger; |
| 39 | + if (taskOptions == null) |
| 40 | + { |
| 41 | + Console.WriteLine($"未获取到作业"); |
| 42 | + return; |
| 43 | + } |
| 44 | + if (string.IsNullOrEmpty(taskOptions.ApiUrl) || taskOptions.ApiUrl == "/") |
| 45 | + { |
| 46 | + Console.WriteLine($"未配置作业:{taskOptions.TaskName}的url地址"); |
| 47 | + QuartzFileHelper.Error($"未配置作业:{taskOptions.TaskName}的url地址"); |
| 48 | + return; |
| 49 | + } |
| 50 | + string exceptionMsg = null; |
| 51 | + |
| 52 | + try |
| 53 | + { |
| 54 | + using (var dbContext = new VOLContext()) |
| 55 | + { |
| 56 | + var _taskOptions = dbContext.Set<Sys_QuartzOptions>().AsTracking() |
| 57 | + .Where(x => x.Id == taskOptions.Id).FirstOrDefault(); |
| 58 | + |
| 59 | + if (_taskOptions != null) |
| 60 | + { |
| 61 | + _taskOptions.LastRunTime = DateTime.Now; |
| 62 | + dbContext.Update(_taskOptions); |
| 63 | + var entry = dbContext.Entry(_taskOptions); |
| 64 | + entry.State = EntityState.Unchanged; |
| 65 | + entry.Property("LastRunTime").IsModified = true; |
| 66 | + dbContext.SaveChanges(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + Dictionary<string, string> header = new Dictionary<string, string>(); |
| 71 | + if (!string.IsNullOrEmpty(taskOptions.AuthKey) |
| 72 | + && !string.IsNullOrEmpty(taskOptions.AuthValue)) |
| 73 | + { |
| 74 | + header.Add(taskOptions.AuthKey.Trim(), taskOptions.AuthValue.Trim()); |
| 75 | + } |
| 76 | + |
| 77 | + httpMessage = await _httpClientFactory.SendAsync( |
| 78 | + taskOptions.Method?.ToLower() == "get" ? HttpMethod.Get : HttpMethod.Post, |
| 79 | + taskOptions.ApiUrl, |
| 80 | + taskOptions.PostData, |
| 81 | + taskOptions.TimeOut ?? 180, |
| 82 | + header); ; |
| 83 | + } |
| 84 | + catch (Exception ex) |
| 85 | + { |
| 86 | + exceptionMsg = ex.Message + ex.StackTrace; |
| 87 | + } |
| 88 | + finally |
| 89 | + { |
| 90 | + try |
| 91 | + { |
| 92 | + var log = new Sys_QuartzLog |
| 93 | + { |
| 94 | + LogId = Guid.NewGuid(), |
| 95 | + TaskName = taskOptions.TaskName, |
| 96 | + Id = taskOptions.Id, |
| 97 | + CreateDate = dateTime, |
| 98 | + ElapsedTime = Convert.ToInt32((DateTime.Now - dateTime).TotalSeconds), |
| 99 | + ResponseContent = httpMessage, |
| 100 | + ErrorMsg = exceptionMsg, |
| 101 | + StratDate = dateTime, |
| 102 | + Result = exceptionMsg == null ? 1 : 0, |
| 103 | + EndDate = DateTime.Now |
| 104 | + }; |
| 105 | + using (var dbContext = new VOLContext()) |
| 106 | + { |
| 107 | + dbContext.Set<Sys_QuartzLog>().Add(log); |
| 108 | + dbContext.SaveChanges(); |
| 109 | + } |
| 110 | + } |
| 111 | + catch (Exception ex) |
| 112 | + { |
| 113 | + Console.WriteLine($"日志写入异常:{taskOptions.TaskName},{ex.Message}"); |
| 114 | + QuartzFileHelper.Error($"日志写入异常:{typeof(HttpResultfulJob).Name},{taskOptions.TaskName},{ex.Message}"); |
| 115 | + } |
| 116 | + } |
| 117 | + Console.WriteLine(trigger.FullName + " " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:sss") + " " + httpMessage); |
| 118 | + return; |
| 119 | + } |
| 120 | + } |
| 121 | + public class TaskOptions |
| 122 | + { |
| 123 | + public string TaskName { get; set; } |
| 124 | + public string GroupName { get; set; } |
| 125 | + public string Interval { get; set; } |
| 126 | + public string ApiUrl { get; set; } |
| 127 | + public string AuthKey { get; set; } |
| 128 | + public string AuthValue { get; set; } |
| 129 | + public string Describe { get; set; } |
| 130 | + public string RequestType { get; set; } |
| 131 | + public DateTime? LastRunTime { get; set; } |
| 132 | + public int Status { get; set; } |
| 133 | + } |
| 134 | +} |
0 commit comments