FinanceTaskJob.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using Newtonsoft.Json;
  2. using Quartz;
  3. using RestSharp;
  4. using SMP.Tool.TimingTask.Core;
  5. using SMP.Tool.TimingTask.Models;
  6. using System;
  7. using System.Collections.Concurrent;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace SMP.Tool.TimingTask.Jobs
  13. {
  14. /// <summary>
  15. /// 轮询任务接口
  16. /// </summary>
  17. public class QueryWaitReadJob : IJob
  18. {
  19. public async Task Execute(IJobExecutionContext context)
  20. {
  21. int threadQty = 5;//每次最大取数50 线程5
  22. try
  23. {
  24. RestClient client = new RestClient(JobConfig.QueryWaitReadJobApi);
  25. RestRequest request = new RestRequest();
  26. var result = await client.ExecuteGetAsync<ApiJsonModel<IEnumerable<Finance_TimeTask>>>(request);
  27. if (result.Data != null && result.Data.data.Count() > 0)
  28. {
  29. ConcurrentQueue<Finance_TimeTask> queue = new ConcurrentQueue<Finance_TimeTask>(result.Data.data);
  30. List<Task> tasks = new List<Task>();
  31. TaskFactory tf = new TaskFactory();
  32. for (int i = 0; i < threadQty; i++)
  33. {
  34. tasks.Add(tf.StartNew(() =>
  35. {
  36. while (!queue.IsEmpty)
  37. {
  38. try
  39. {
  40. if (queue.TryDequeue(out var item))
  41. {
  42. //调用执行
  43. RestClient client = new RestClient(JobConfig.ExecuteTaskJobApi + $"?taskId={item.Id}&startTime={item.StartTime}&endTime={item.EndTime}");
  44. RestRequest request = new RestRequest();
  45. var result = client.ExecuteGetAsync(request).Result;
  46. //if (result.StatusCode == System.Net.HttpStatusCode.InternalServerError)
  47. //{
  48. //}
  49. //else
  50. //{
  51. //}
  52. }
  53. }
  54. catch (Exception ex)
  55. {
  56. }
  57. }
  58. }));
  59. }
  60. Task.WaitAll(tasks.ToArray());
  61. }
  62. Console.WriteLine($"QueryWaitReadJob: 调用成功!");
  63. }
  64. catch (Exception ex)
  65. {
  66. Console.WriteLine($"QueryWaitReadJob:调用异常!" + ex.Message);
  67. }
  68. }
  69. }
  70. /// <summary>
  71. /// 定时创建任务
  72. /// </summary>
  73. public class CreatFinanceTaskJob : IJob
  74. {
  75. public async Task Execute(IJobExecutionContext context)
  76. {
  77. try
  78. {
  79. var time = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
  80. Finance_TimeTask model = new Finance_TimeTask()
  81. {
  82. StartTime = time,
  83. EndTime = time.AddDays(1),
  84. };
  85. RestClient client = new RestClient(JobConfig.CreateApi);
  86. var request = new RestRequest();
  87. request.AddBody(model);
  88. var result = await client.ExecutePostAsync(request);
  89. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  90. {
  91. Console.WriteLine($"CreatFinanceTaskJob: 调用成功!");
  92. }
  93. else
  94. {
  95. Console.WriteLine($"CreatFinanceTaskJob: 调用失败!");
  96. }
  97. }
  98. catch (Exception ex)
  99. {
  100. Console.WriteLine($"CreatFinanceTaskJob: 调用异常!" + ex.Message);
  101. }
  102. }
  103. }
  104. }