QuotaReductionJob.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using Quartz;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using XYY.Tool.QuotaReduction;
  10. namespace XYY.Tool.TimingTask.jobs
  11. {
  12. public class QuotaReductionJob : IJob
  13. {
  14. public Task Execute(IJobExecutionContext context)
  15. {
  16. int count = 0; bool isSucess = false;string msg = string.Empty;
  17. DateTime startTime =Convert.ToDateTime(DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"));
  18. DateTime endTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
  19. do{
  20. try
  21. {
  22. RestSharp.RestClient client = new RestSharp.RestClient(JobConfig.QuotaReductionJobApi+ $"?startTime={startTime}&endTime={endTime}");
  23. RestSharp.RestRequest request = new RestSharp.RestRequest(RestSharp.Method.POST);
  24. request.AddHeader("Authorization", "token 57AEDE5487260443D6BF2182D31286B1");
  25. var response = client.Execute(request);
  26. if (response.StatusCode == System.Net.HttpStatusCode.OK)
  27. {
  28. isSucess = true;
  29. }
  30. else
  31. {
  32. count++;
  33. }
  34. }
  35. catch (Exception ex)
  36. {
  37. count++;
  38. }
  39. //重复赋值,取最后一次错误
  40. msg = $"授信数据还原:" + JobConfig.QuotaReductionJobApi + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + " "
  41. + $"{((count == 0 || count == 1) ? "首次请求" : "请求次数:" + count)},请求结果为{isSucess}";
  42. Console.WriteLine(msg);
  43. //睡眠再继续
  44. if (count <= JobConfig.QuotaReductionJobTryTime && !isSucess) { Thread.Sleep(JobConfig.QuotaReductionJobTryTimeInterval); }
  45. }
  46. while (count <= JobConfig.QuotaReductionJobTryTime&& !isSucess) ;
  47. //失败请求,发送钉钉消息
  48. if (!isSucess) { dingApiRequest.sendDingMessageToChat("QuotaReductionJob", msg); }
  49. return null;
  50. }
  51. }
  52. }