Task.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using System;
  2. using System.Threading;
  3. using Newtonsoft.Json;
  4. using Sers.Core.Module.Api;
  5. using Vit.Core.Module.Log;
  6. using Vit.Core.Util.ComponentModel.Data;
  7. using Vit.Core.Util.Net;
  8. using Vit.Core.Util.Threading;
  9. using Vit.Extensions;
  10. namespace App.Robot.Station.Logical.Model
  11. {
  12. /// <summary>
  13. /// RepeatTaskHelp
  14. /// </summary>
  15. public class Task
  16. {
  17. public string name => config.name;
  18. public int id;
  19. [JsonIgnore]
  20. RepeatTaskHelp tasks = new RepeatTaskHelp();
  21. public int RunningThreadCount => tasks.RunningThreadCount;
  22. public bool IsRunning => tasks.IsRunning;
  23. public int targetCount => config.threadCount * config.loopCountPerThread;
  24. public int sumCount = 0;
  25. public int sumFailCount = 0;
  26. public int curCount=0;
  27. public int failCount=0;
  28. public TaskConfig config;
  29. void StepUp(bool success)
  30. {
  31. Interlocked.Increment(ref curCount);
  32. Interlocked.Increment(ref sumCount);
  33. if (!success)
  34. {
  35. Interlocked.Increment(ref sumFailCount);
  36. Interlocked.Increment(ref failCount);
  37. }
  38. }
  39. public Task(TaskConfig config)
  40. {
  41. this.config = config;
  42. tasks.threadCount = config.threadCount;
  43. tasks.repeatCountPerThread = config.loopCountPerThread;
  44. if (config.apiRoute.StartsWith("http"))
  45. {
  46. if (config.httpUseHttpUtil)
  47. {
  48. httpClient = new HttpClient();
  49. httpClient_ReqParam = new HttpRequest
  50. {
  51. url = config.apiRoute,
  52. body = config.apiArg,
  53. httpMethod = config.httpMethod
  54. };
  55. tasks.action = ActionHttpClient;
  56. }
  57. else
  58. {
  59. httpUtil = new HttpUtil();
  60. httpUtil_Request = new RequestParam
  61. {
  62. url = config.apiRoute,
  63. body = config.apiArg,
  64. Method = config.httpMethod
  65. };
  66. tasks.action = ActionHttpUtil;
  67. }
  68. }
  69. else
  70. {
  71. tasks.action = ActionApiClient;
  72. }
  73. }
  74. #region ActionHttpClient
  75. HttpClient httpClient;
  76. HttpRequest httpClient_ReqParam;
  77. void ActionHttpClient()
  78. {
  79. bool success = false;
  80. try
  81. {
  82. var ret = httpClient.Send<ApiReturn>(httpClient_ReqParam)?.data;
  83. if (ret.success)
  84. {
  85. success = true;
  86. }
  87. else
  88. {
  89. if (config.logError)
  90. Logger.Info("失败:ret:" + ret.Serialize());
  91. }
  92. }
  93. catch (Exception ex)
  94. {
  95. Interlocked.Increment(ref failCount);
  96. Logger.Error(ex);
  97. }
  98. StepUp(success);
  99. Thread.Sleep(config.interval);
  100. }
  101. #endregion
  102. #region ActionHttpUtil
  103. HttpUtil httpUtil;
  104. RequestParam httpUtil_Request;
  105. void ActionHttpUtil()
  106. {
  107. bool success = false;
  108. try
  109. {
  110. var ret = httpUtil.Ajax<ApiReturn>(httpUtil_Request);
  111. if (ret.success)
  112. {
  113. success = true;
  114. }
  115. else
  116. {
  117. if (config.logError)
  118. Logger.Info("失败:ret:" + ret.Serialize());
  119. }
  120. }
  121. catch (Exception ex)
  122. {
  123. Interlocked.Increment(ref failCount);
  124. Logger.Error(ex);
  125. }
  126. StepUp(success);
  127. Thread.Sleep(config.interval);
  128. }
  129. #endregion
  130. void ActionApiClient()
  131. {
  132. bool success = false;
  133. try
  134. {
  135. var ret = ApiClient.CallRemoteApi<ApiReturn>(config.apiRoute, config.apiArg, config.httpMethod);
  136. if (ret!=null && ret.success)
  137. {
  138. success = true;
  139. }
  140. else
  141. {
  142. if(config.logError)
  143. Logger.Info("失败:ret:" + ret.Serialize());
  144. }
  145. }
  146. catch (Exception ex)
  147. {
  148. Interlocked.Increment(ref failCount);
  149. Logger.Error(ex);
  150. }
  151. StepUp(success);
  152. if(config.interval>0)
  153. Thread.Sleep(config.interval);
  154. }
  155. public void Start()
  156. {
  157. tasks.threadName = "Robot-"+ config.name;
  158. curCount = 0;
  159. failCount = 0;
  160. tasks.Start();
  161. }
  162. public void Stop()
  163. {
  164. tasks.Stop();
  165. }
  166. }
  167. }