From c4b1f2d3c63bf111ead6d772c78df0118ca75b89 Mon Sep 17 00:00:00 2001 From: wintel Date: Thu, 21 Dec 2023 22:02:16 +0800 Subject: [PATCH] =?UTF-8?q?fix=20issue=20#I8NXEX=20=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E4=BD=BF=E7=94=A8=E5=A4=96=E9=83=A8=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E7=B1=BB=E5=9E=8B=E9=85=8D=E7=BD=AE=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- OpenAuth.App/Extensions/OpenJobExt.cs | 24 +++++++++----- OpenAuth.App/Jobs/HttpPostJob.cs | 48 +++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 OpenAuth.App/Jobs/HttpPostJob.cs diff --git a/OpenAuth.App/Extensions/OpenJobExt.cs b/OpenAuth.App/Extensions/OpenJobExt.cs index 032f07c6..9be97c00 100644 --- a/OpenAuth.App/Extensions/OpenJobExt.cs +++ b/OpenAuth.App/Extensions/OpenJobExt.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using Infrastructure; +using OpenAuth.App.Jobs; using OpenAuth.Repository.Domain; using Quartz; @@ -18,14 +19,21 @@ namespace OpenAuth.App.Extensions /// 一个Quartz Scheduler public static void Start(this OpenJob job, IScheduler scheduler) { - var jobBuilderType = typeof(JobBuilder); - var method = jobBuilderType.GetMethods().FirstOrDefault( - x => x.Name.Equals("Create", StringComparison.OrdinalIgnoreCase) && - x.IsGenericMethod && x.GetParameters().Length == 0) - ?.MakeGenericMethod(Type.GetType(job.JobCall)); - - var jobBuilder = (JobBuilder) method.Invoke(null, null); - + JobBuilder jobBuilder = null; + if (job.JobType == 1) + { + jobBuilder = JobBuilder.Create(); + } + else + { + var jobBuilderType = typeof(JobBuilder); + var method = jobBuilderType.GetMethods().FirstOrDefault( + x => x.Name.Equals("Create", StringComparison.OrdinalIgnoreCase) && + x.IsGenericMethod && x.GetParameters().Length == 0) + ?.MakeGenericMethod(Type.GetType(job.JobCall)); + jobBuilder = (JobBuilder) method.Invoke(null, null); + } + IJobDetail jobDetail = jobBuilder.WithIdentity(job.Id).Build(); jobDetail.JobDataMap[Define.JOBMAPKEY] = job.Id; //传递job信息 ITrigger trigger = TriggerBuilder.Create() diff --git a/OpenAuth.App/Jobs/HttpPostJob.cs b/OpenAuth.App/Jobs/HttpPostJob.cs new file mode 100644 index 00000000..2510e4dd --- /dev/null +++ b/OpenAuth.App/Jobs/HttpPostJob.cs @@ -0,0 +1,48 @@ +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using Infrastructure; +using Microsoft.AspNetCore.Authentication.JwtBearer; +using OpenAuth.Repository.Domain; +using Quartz; + +namespace OpenAuth.App.Jobs +{ + public class HttpPostJob : IJob + { + private SysLogApp _sysLogApp; + private OpenJobApp _openJobApp; + private IHttpClientFactory _httpClientFactory; + + public HttpPostJob(SysLogApp sysLogApp, OpenJobApp openJobApp, IHttpClientFactory httpClientFactory) + { + _sysLogApp = sysLogApp; + _openJobApp = openJobApp; + _httpClientFactory = httpClientFactory; + } + + public async Task Execute(IJobExecutionContext context) + { + var jobId = context.MergedJobDataMap.GetString(Define.JOBMAPKEY); + var job = _openJobApp.Get(jobId); + if (job.JobType == 1) + { + using (HttpContent httpContent = new StringContent(job.JobCallParams, Encoding.UTF8)) + { + httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); + var result = await _httpClientFactory.CreateClient().PostAsync(job.JobCall, httpContent); + _sysLogApp.Add(new SysLog + { + CreateName = "Quartz", + CreateId = "Quartz", + TypeName = "定时任务", + TypeId = "AUTOJOB", + Content = $"定时任务自动请求{job.JobCall}返回结果:{result.Content.ReadAsStringAsync().Result}" + }); + } + } + //todo:这里可以加入自己的自动任务逻辑 + _openJobApp.RecordRun(jobId); + } + } +} \ No newline at end of file