fix issue #I8NXEX 定时任务使用外部接口类型配置执行报错

This commit is contained in:
wintel 2023-12-21 22:02:16 +08:00
parent 0305ad8c2e
commit c4b1f2d3c6
2 changed files with 64 additions and 8 deletions

View File

@ -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
/// <param name="scheduler">一个Quartz Scheduler</param>
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<HttpPostJob>();
}
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()

View File

@ -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);
}
}
}