ExecutorBase.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. namespace WingServerCommon.Utilities.Executors
  6. {
  7. public abstract class ExecutorBase<T> where T : class
  8. {
  9. private readonly TimeSpan _executeDelayTime;
  10. protected readonly object WorkersLocker = new object();
  11. private volatile bool _closed;
  12. private readonly ExecutingStatus _executingStatus = new ExecutingStatus();
  13. private readonly ManualResetEvent _delayManualResetEvent = new ManualResetEvent(false);
  14. /// <summary>
  15. /// Gets the name of this executor.
  16. /// </summary>
  17. public string Name { get; }
  18. /// <summary>
  19. /// Gets all workers count.
  20. /// </summary>
  21. public int WorkerCount => GetWorkerCount();
  22. /// <summary>
  23. /// Indicate this executor is closed
  24. /// </summary>
  25. public bool IsClosed => _closed;
  26. protected ExecutorBase(string name)
  27. {
  28. _executeDelayTime = TimeSpan.MinValue;
  29. Name = name;
  30. }
  31. protected ExecutorBase(string name, TimeSpan executeDelayTime) : this(name)
  32. {
  33. _executeDelayTime = executeDelayTime;
  34. }
  35. /// <summary>
  36. /// Gets current worker count in queue.
  37. /// </summary>
  38. /// <returns></returns>
  39. protected abstract int GetWorkerCount();
  40. /// <summary>
  41. /// Add one worker into the queue.
  42. /// </summary>
  43. /// <param name="worker"></param>
  44. protected abstract void AddWorker(ExecuteWorker<T> worker);
  45. /// <summary>
  46. /// Take one worker from the queue.
  47. /// </summary>
  48. /// <returns></returns>
  49. protected abstract ExecuteWorker<T> TakeWorker();
  50. /// <summary>
  51. /// Gets all workers.
  52. /// </summary>
  53. /// <returns></returns>
  54. protected abstract IEnumerable<ExecuteWorker<T>> GetWorkers();
  55. /// <summary>
  56. /// Clear all workers which not stated.
  57. /// </summary>
  58. protected abstract void CancelWorkers();
  59. /// <summary>
  60. /// Add one execute func into the queue.
  61. /// </summary>
  62. /// <param name="executeMethod"></param>
  63. /// <param name="parameter"></param>
  64. public void Add(Func<T, bool> executeMethod, T parameter)
  65. {
  66. lock (WorkersLocker)
  67. {
  68. if (_closed)
  69. {
  70. return;
  71. }
  72. var worker = new ExecuteWorker<T>(executeMethod, parameter);
  73. AddWorker(worker);
  74. if (!_executingStatus.Executing)
  75. {
  76. _executingStatus.Reset();
  77. DoExecute();
  78. }
  79. }
  80. }
  81. /// <summary>
  82. /// The final method to run the worker.
  83. /// </summary>
  84. private void DoExecute()
  85. {
  86. //Logic mush run in another thread.
  87. Task.Run(() =>
  88. {
  89. ExecuteWorker<T> worker = null;
  90. lock (WorkersLocker)
  91. {
  92. if (WorkerCount > 0)
  93. {
  94. worker = TakeWorker();
  95. }
  96. }
  97. var executeResult = worker?.Run() ?? true;
  98. if (_closed)
  99. {
  100. _executingStatus.Set();
  101. }
  102. lock (WorkersLocker)
  103. {
  104. var continueExecute = WorkerCount > 0 && !_closed && executeResult;
  105. if (continueExecute)
  106. {
  107. if (_executeDelayTime != TimeSpan.MinValue)
  108. {
  109. _delayManualResetEvent.WaitOne(_executeDelayTime);
  110. }
  111. DoExecute();
  112. }
  113. else
  114. {
  115. _executingStatus.Set();
  116. }
  117. }
  118. });
  119. }
  120. /// <summary>
  121. /// Close the executor and execute all workers.
  122. /// </summary>
  123. public void Close()
  124. {
  125. StopExecuting();
  126. lock (WorkersLocker)
  127. {
  128. var workers = GetWorkers();
  129. foreach (var worker in workers)
  130. {
  131. worker?.Run();
  132. }
  133. }
  134. }
  135. /// <summary>
  136. /// Dispose current executor.
  137. /// </summary>
  138. public void Dispose(int timeoutSeconds = 10)
  139. {
  140. _closed = true;
  141. lock (WorkersLocker)
  142. {
  143. CancelWorkers();
  144. _executingStatus.Wait(timeoutSeconds);
  145. }
  146. }
  147. /// <summary>
  148. /// Cancel all workers.
  149. /// </summary>
  150. public void Cancel()
  151. {
  152. lock (WorkersLocker)
  153. {
  154. CancelWorkers();
  155. }
  156. StopExecuting();
  157. }
  158. /// <summary>
  159. /// Stop executing.
  160. /// </summary>
  161. protected void StopExecuting()
  162. {
  163. _closed = true;
  164. lock (WorkersLocker)
  165. {
  166. if (WorkerCount == 0)
  167. {
  168. _closed = false;
  169. return;
  170. }
  171. _executingStatus.Wait();
  172. }
  173. _closed = false;
  174. }
  175. public bool GetExecutingStatus()
  176. {
  177. return _executingStatus.Executing;
  178. }
  179. }
  180. }