using log4net; using Microsoft.AspNetCore.SignalR; using Quartz; using Quartz.Impl; using Quartz.Impl.Triggers; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Net.Http; using System.Threading.Tasks; using VueWebCoreApi.Extensions; using VueWebCoreApi.SignalR; using VueWebCoreApi.Tools; namespace VueWebCoreApi.Quartz { public class HttpResultfulJob : IJob { private ILog log = LogManager.GetLogger(Startup.repository.Name, typeof(ChatHub)); private readonly IHubContext _hubContext; readonly IHttpClientFactory httpClientFactory; /// /// 2020.05.31增加构造方法 /// /// /// public HttpResultfulJob(IServiceProvider serviceProvider, IHttpClientFactory httpClientFactory, IHubContext hubContext) { this.httpClientFactory = httpClientFactory; _hubContext = hubContext; //serviceProvider.GetService() } public async Task Execute(IJobExecutionContext context) { DateTime dateTime = DateTime.Now; TaskOptions taskOptions = context.GetTaskOptions(); string httpMessage = ""; AbstractTrigger trigger = (context as JobExecutionContextImpl).Trigger as AbstractTrigger; if (taskOptions == null) { FileHelper.WriteFile(FileQuartz.LogPath + trigger.Group, $"{trigger.Name}.txt", "未到找作业或可能被移除", true); return; } Console.WriteLine($"作业[{taskOptions.TaskName}]开始:{ DateTime.Now.ToString("yyyy-MM-dd HH:mm:sss")}"); if (string.IsNullOrEmpty(taskOptions.ApiUrl) || taskOptions.ApiUrl == "/") { FileHelper.WriteFile(FileQuartz.LogPath + trigger.Group, $"{trigger.Name}.txt", $"{ DateTime.Now.ToString("yyyy-MM-dd HH:mm:sss")}未配置url,", true); return; } try { Dictionary header = new Dictionary(); if (!string.IsNullOrEmpty(taskOptions.AuthKey) && !string.IsNullOrEmpty(taskOptions.AuthValue)) { header.Add(taskOptions.AuthKey.Trim(), taskOptions.AuthValue.Trim()); } httpMessage = await httpClientFactory.HttpSendAsync(taskOptions.RequestType?.ToLower() == "get" ? HttpMethod.Get : HttpMethod.Post, taskOptions.ApiUrl, header); } catch (Exception ex) { httpMessage = ex.Message; } try { string logContent = $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}_{dateTime.ToString("yyyy-MM-dd HH:mm:ss")}_{(string.IsNullOrEmpty(httpMessage) ? "OK" : httpMessage)}\r\n"; FileHelper.WriteFile(FileQuartz.LogPath + taskOptions.GroupName + "\\", $"{taskOptions.TaskName}.txt", logContent, true); //await _hubContext.Clients.All.SendAsync("SendMessage系统通知:"+$"最新消息{DateTime.Now}"); //await _hubContext.Clients.All.SendAll(logContent); //查找系统用户 var sql = @"select usercode as code,username as name from TUser where is_delete='0' and enable='Y' and password='123'"; var data = DapperHelper.selecttable(sql); var departmentIDs = data.AsEnumerable().Select(x => x.Field("code")).ToList();//获取推送人员编码 var FindPublicBaseDic = UserIdsStore.Ids.Where(d => departmentIDs.Contains(d.Value)).Select(x => x.Key).ToList(); //匹配已经登录的推送人员connectionIds if (FindPublicBaseDic.Count > 0) { //推送指定用户 await _hubContext.Clients.Clients(FindPublicBaseDic).SendCustomUserMessage(logContent); } } catch (Exception) { } Console.WriteLine(trigger.FullName + " " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:sss") + " " + httpMessage); return; } } }