Job_HttpRequest.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using Infrastructure;
  2. using Infrastructure.Attribute;
  3. using Quartz;
  4. using Quartz.Impl;
  5. using Quartz.Impl.Triggers;
  6. using SqlSugar.IOC;
  7. using System;
  8. using System.Threading.Tasks;
  9. using ZR.Model.System;
  10. namespace ZR.Tasks.TaskScheduler
  11. {
  12. /// <summary>
  13. /// 定时任务http请求
  14. /// </summary>
  15. [AppService(ServiceType = typeof(Job_HttpRequest), ServiceLifetime = LifeTime.Scoped)]
  16. internal class Job_HttpRequest : JobBase, IJob
  17. {
  18. //private readonly ISysTasksQzService tasksQzService;
  19. private readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
  20. //public Job_HttpRequest(ISysTasksQzService tasksQzService)
  21. //{
  22. // this.tasksQzService = tasksQzService;
  23. //}
  24. public async Task Execute(IJobExecutionContext context)
  25. {
  26. await ExecuteJob(context, async () => await Run(context));
  27. }
  28. public async Task Run(IJobExecutionContext context)
  29. {
  30. AbstractTrigger trigger = (context as JobExecutionContextImpl).Trigger as AbstractTrigger;
  31. //var info = await tasksQzService.CopyNew().GetByIdAsync(trigger.JobName);
  32. var info = await DbScoped.SugarScope.CopyNew()
  33. .Queryable<SysTasks>()
  34. .FirstAsync(f => f.ID == trigger.JobName) ?? throw new CustomException($"任务{trigger?.JobName}网络请求执行失败,任务不存在");
  35. string result;
  36. if (info.RequestMethod != null && info.RequestMethod.Equals("POST", StringComparison.OrdinalIgnoreCase))
  37. {
  38. result = await HttpHelper.HttpPostAsync(info.ApiUrl, info.JobParams);
  39. }
  40. else
  41. {
  42. var url = info.ApiUrl;
  43. if (url.IndexOf("?") > -1)
  44. {
  45. url += "&" + info.JobParams;
  46. }
  47. else
  48. {
  49. url += "?" + info.JobParams;
  50. }
  51. result = await HttpHelper.HttpGetAsync(url);
  52. }
  53. logger.Info($"任务【{info.Name}】网络请求执行结果=" + result);
  54. }
  55. }
  56. }