JobEngine.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Concurrent;
  29. using System.Reflection;
  30. using System.Threading;
  31. using log4net;
  32. using OpenSim.Framework;
  33. namespace OpenSim.Framework.Monitoring
  34. {
  35. public class JobEngine
  36. {
  37. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  38. public int LogLevel { get; set; }
  39. private object JobLock = new object();
  40. public string Name { get; private set; }
  41. public string LoggingName { get; private set; }
  42. /// <summary>
  43. /// Is this engine running?
  44. /// </summary>
  45. public bool IsRunning { get; private set; }
  46. /// <summary>
  47. /// The current job that the engine is running.
  48. /// </summary>
  49. /// <remarks>
  50. /// Will be null if no job is currently running.
  51. /// </remarks>
  52. private Job m_currentJob;
  53. public Job CurrentJob { get { return m_currentJob;} }
  54. /// <summary>
  55. /// Number of jobs waiting to be processed.
  56. /// </summary>
  57. public int JobsWaiting { get { return m_jobQueue.Count; } }
  58. /// <summary>
  59. /// The timeout in milliseconds to wait for at least one event to be written when the recorder is stopping.
  60. /// </summary>
  61. public int RequestProcessTimeoutOnStop { get; set; }
  62. /// <summary>
  63. /// Controls whether we need to warn in the log about exceeding the max queue size.
  64. /// </summary>
  65. /// <remarks>
  66. /// This is flipped to false once queue max has been exceeded and back to true when it falls below max, in
  67. /// order to avoid spamming the log with lots of warnings.
  68. /// </remarks>
  69. private bool m_warnOverMaxQueue = true;
  70. private BlockingCollection<Job> m_jobQueue = new BlockingCollection<Job>(5000);
  71. private CancellationTokenSource m_cancelSource;
  72. private int m_timeout = -1;
  73. private bool m_threadRunnig = false;
  74. public JobEngine(string name, string loggingName, int timeout = -1)
  75. {
  76. Name = name;
  77. LoggingName = loggingName;
  78. m_timeout = timeout;
  79. RequestProcessTimeoutOnStop = 5000;
  80. }
  81. public void Start()
  82. {
  83. lock (JobLock)
  84. {
  85. if (IsRunning)
  86. return;
  87. IsRunning = true;
  88. m_cancelSource = new CancellationTokenSource();
  89. WorkManager.RunInThreadPool(ProcessRequests, null, Name, false);
  90. m_threadRunnig = true;
  91. }
  92. }
  93. public void Stop()
  94. {
  95. lock (JobLock)
  96. {
  97. try
  98. {
  99. if (!IsRunning)
  100. return;
  101. m_log.DebugFormat("[JobEngine] Stopping {0}", Name);
  102. IsRunning = false;
  103. if(m_threadRunnig)
  104. {
  105. m_cancelSource.Cancel();
  106. m_threadRunnig = false;
  107. }
  108. }
  109. finally
  110. {
  111. if(m_cancelSource != null)
  112. {
  113. m_cancelSource.Dispose();
  114. m_cancelSource = null;
  115. }
  116. if (m_jobQueue != null)
  117. {
  118. m_jobQueue.Dispose();
  119. m_jobQueue = null;
  120. }
  121. }
  122. }
  123. }
  124. /// <summary>
  125. /// Make a job.
  126. /// </summary>
  127. /// <remarks>
  128. /// We provide this method to replace the constructor so that we can later pool job objects if necessary to
  129. /// reduce memory churn. Normally one would directly call QueueJob() with parameters anyway.
  130. /// </remarks>
  131. /// <returns></returns>
  132. /// <param name="name">Name.</param>
  133. /// <param name="action">Action.</param>
  134. /// <param name="commonId">Common identifier.</param>
  135. public static Job MakeJob(string name, Action action, string commonId = null)
  136. {
  137. return Job.MakeJob(name, action, commonId);
  138. }
  139. /// <summary>
  140. /// Remove the next job queued for processing.
  141. /// </summary>
  142. /// <remarks>
  143. /// Returns null if there is no next job.
  144. /// Will not remove a job currently being performed.
  145. /// </remarks>
  146. public Job RemoveNextJob()
  147. {
  148. Job nextJob;
  149. m_jobQueue.TryTake(out nextJob);
  150. return nextJob;
  151. }
  152. /// <summary>
  153. /// Queue the job for processing.
  154. /// </summary>
  155. /// <returns><c>true</c>, if job was queued, <c>false</c> otherwise.</returns>
  156. /// <param name="name">Name of job. This appears on the console and in logging.</param>
  157. /// <param name="action">Action to perform.</param>
  158. /// <param name="commonId">
  159. /// Common identifier for a set of jobs. This is allows a set of jobs to be removed
  160. /// if required (e.g. all jobs for a given agent. Optional.
  161. /// </param>
  162. public bool QueueJob(string name, Action action, string commonId = null)
  163. {
  164. return QueueJob(MakeJob(name, action, commonId));
  165. }
  166. /// <summary>
  167. /// Queue the job for processing.
  168. /// </summary>
  169. /// <returns><c>true</c>, if job was queued, <c>false</c> otherwise.</returns>
  170. /// <param name="job">The job</param>
  171. /// </param>
  172. public bool QueueJob(Job job)
  173. {
  174. lock(JobLock)
  175. {
  176. if(!IsRunning)
  177. return false;
  178. if(!m_threadRunnig)
  179. {
  180. WorkManager.RunInThreadPool(ProcessRequests, null, Name, false);
  181. m_threadRunnig = true;
  182. }
  183. }
  184. if (m_jobQueue.Count < m_jobQueue.BoundedCapacity)
  185. {
  186. m_jobQueue.Add(job);
  187. if (!m_warnOverMaxQueue)
  188. m_warnOverMaxQueue = true;
  189. return true;
  190. }
  191. else
  192. {
  193. if (m_warnOverMaxQueue)
  194. {
  195. m_log.WarnFormat(
  196. "[{0}]: Job queue at maximum capacity, not recording job from {1} in {2}",
  197. LoggingName, job.Name, Name);
  198. m_warnOverMaxQueue = false;
  199. }
  200. return false;
  201. }
  202. }
  203. private void ProcessRequests(Object o)
  204. {
  205. while(IsRunning)
  206. {
  207. try
  208. {
  209. if(!m_jobQueue.TryTake(out m_currentJob, m_timeout, m_cancelSource.Token))
  210. {
  211. lock(JobLock)
  212. m_threadRunnig = false;
  213. break;
  214. }
  215. }
  216. catch (OperationCanceledException)
  217. {
  218. m_log.DebugFormat("[JobEngine] {0} Canceled ignoring {1} jobs in queue",
  219. Name, m_jobQueue.Count);
  220. break;
  221. }
  222. catch
  223. {
  224. break;
  225. }
  226. if(LogLevel >= 1)
  227. m_log.DebugFormat("[{0}]: Processing job {1}",LoggingName,m_currentJob.Name);
  228. try
  229. {
  230. m_currentJob.Action();
  231. }
  232. catch(Exception e)
  233. {
  234. m_log.Error(
  235. string.Format(
  236. "[{0}]: Job {1} failed, continuing. Exception ",LoggingName,m_currentJob.Name),e);
  237. }
  238. if(LogLevel >= 1)
  239. m_log.DebugFormat("[{0}]: Processed job {1}",LoggingName,m_currentJob.Name);
  240. m_currentJob.Action = null;
  241. m_currentJob = null;
  242. }
  243. }
  244. public class Job
  245. {
  246. /// <summary>
  247. /// Name of the job.
  248. /// </summary>
  249. /// <remarks>
  250. /// This appears on console and debug output.
  251. /// </remarks>
  252. public string Name { get; private set; }
  253. /// <summary>
  254. /// Common ID for this job.
  255. /// </summary>
  256. /// <remarks>
  257. /// This allows all jobs with a certain common ID (e.g. a client UUID) to be removed en-masse if required.
  258. /// Can be null if this is not required.
  259. /// </remarks>
  260. public string CommonId { get; private set; }
  261. /// <summary>
  262. /// Action to perform when this job is processed.
  263. /// </summary>
  264. public Action Action { get; set; }
  265. private Job(string name, string commonId, Action action)
  266. {
  267. Name = name;
  268. CommonId = commonId;
  269. Action = action;
  270. }
  271. /// <summary>
  272. /// Make a job. It needs to be separately queued.
  273. /// </summary>
  274. /// <remarks>
  275. /// We provide this method to replace the constructor so that we can pool job objects if necessary to
  276. /// to reduce memory churn. Normally one would directly call JobEngine.QueueJob() with parameters anyway.
  277. /// </remarks>
  278. /// <returns></returns>
  279. /// <param name="name">Name.</param>
  280. /// <param name="action">Action.</param>
  281. /// <param name="commonId">Common identifier.</param>
  282. public static Job MakeJob(string name, Action action, string commonId = null)
  283. {
  284. return new Job(name, commonId, action);
  285. }
  286. }
  287. }
  288. }