shgx_tz_mom/ZR.Tasks/TaskScheduler/Job_HttpRequest.cs

62 lines
2.2 KiB
C#
Raw Normal View History

2022-04-03 13:00:30 +08:00
using Infrastructure;
using Infrastructure.Attribute;
using Quartz;
using Quartz.Impl;
using Quartz.Impl.Triggers;
using SqlSugar.IOC;
2022-05-10 11:18:12 +08:00
using System;
2022-04-03 13:00:30 +08:00
using System.Threading.Tasks;
using ZR.Model.System;
2022-04-03 13:00:30 +08:00
namespace ZR.Tasks.TaskScheduler
{
2022-05-10 11:18:12 +08:00
/// <summary>
/// 定时任务http请求
/// </summary>
2022-04-03 13:00:30 +08:00
[AppService(ServiceType = typeof(Job_HttpRequest), ServiceLifetime = LifeTime.Scoped)]
internal class Job_HttpRequest : JobBase, IJob
{
//private readonly ISysTasksQzService tasksQzService;
private readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
2022-04-03 13:00:30 +08:00
//public Job_HttpRequest(ISysTasksQzService tasksQzService)
//{
// this.tasksQzService = tasksQzService;
//}
2022-04-03 13:00:30 +08:00
public async Task Execute(IJobExecutionContext context)
{
await ExecuteJob(context, async () => await Run(context));
}
public async Task Run(IJobExecutionContext context)
{
AbstractTrigger trigger = (context as JobExecutionContextImpl).Trigger as AbstractTrigger;
//var info = await tasksQzService.CopyNew().GetByIdAsync(trigger.JobName);
var info = await DbScoped.SugarScope.CopyNew().Queryable<SysTasks>().FirstAsync(f => f.ID == trigger.JobName);
2022-12-05 12:03:28 +08:00
if (info == null)
2022-05-10 11:18:12 +08:00
{
2022-12-05 12:03:28 +08:00
throw new CustomException($"任务{trigger?.JobName}网络请求执行失败,任务不存在");
}
string result;
if (info.RequestMethod != null && info.RequestMethod.Equals("POST", StringComparison.OrdinalIgnoreCase))
{
result = await HttpHelper.HttpPostAsync(info.ApiUrl, info.JobParams);
}
else
{
2022-12-05 12:03:28 +08:00
var url = info.ApiUrl;
if (url.IndexOf("?") > -1)
{
url += "&" + info.JobParams;
}
else
{
url += "?" + info.JobParams;
}
result = await HttpHelper.HttpGetAsync(url);
2022-05-10 11:18:12 +08:00
}
2022-12-05 12:03:28 +08:00
logger.Info($"任务【{info.Name}】网络请求执行结果=" + result);
2022-04-03 13:00:30 +08:00
}
}
}