PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/Aurora/Services/GenericServices/Scheduler/Scheduler.cs

https://bitbucket.org/VirtualReality/async-sim-testing
C# | 219 lines | 150 code | 27 blank | 42 comment | 16 complexity | 3cf0c3447708b1528cbd1372244c62e9 MD5 | raw file
  1. /*
  2. * Copyright (c) Contributors, http://aurora-sim.org/, 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 Aurora-Sim 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 Aurora.Framework;
  28. using Aurora.Framework.ConsoleFramework;
  29. using Aurora.Framework.Modules;
  30. using Aurora.Framework.Services;
  31. using Aurora.Framework.Services.ClassHelpers.Other;
  32. using Aurora.Framework.Utilities;
  33. using Nini.Config;
  34. using System;
  35. using System.Collections.Generic;
  36. using System.Linq;
  37. using System.Timers;
  38. namespace Aurora.Services
  39. {
  40. public class Scheduler : ConnectorBase, IScheduleService, IService
  41. {
  42. public AuroraEventManager EventManager = new AuroraEventManager();
  43. private ISchedulerDataPlugin m_database;
  44. private bool m_enabled = false;
  45. private readonly Timer scheduleTimer = new Timer();
  46. #region Implementation of IService
  47. /// <summary>
  48. /// Set up and register the module
  49. /// </summary>
  50. /// <param name="config">Config file</param>
  51. /// <param name="registry">Place to register the modules into</param>
  52. public void Initialize(IConfigSource config, IRegistryCore registry)
  53. {
  54. registry.RegisterModuleInterface<IScheduleService>(this);
  55. base.Init(registry, "Scheduler");
  56. }
  57. /// <summary>
  58. /// Load other IService modules now that this is set up
  59. /// </summary>
  60. /// <param name="config">Config file</param>
  61. /// <param name="registry">Place to register and retrieve module interfaces</param>
  62. public void Start(IConfigSource config, IRegistryCore registry)
  63. {
  64. }
  65. /// <summary>
  66. /// All modules have started up and it is ready to run
  67. /// </summary>
  68. public void FinishedStartup()
  69. {
  70. if (!m_doRemoteCalls)
  71. {
  72. m_database = Framework.Utilities.DataManager.RequestPlugin<ISchedulerDataPlugin>();
  73. if (m_database != null)
  74. m_enabled = true;
  75. if (m_enabled)
  76. {
  77. // don't want to start to soon
  78. scheduleTimer.Interval = 60000;
  79. scheduleTimer.Elapsed += t_Elapsed;
  80. scheduleTimer.Start();
  81. }
  82. }
  83. }
  84. #endregion
  85. #region Implementation of IScheduleService
  86. public bool Register(SchedulerItem I, OnGenericEventHandler handler)
  87. {
  88. if (m_doRemoteCalls) return false;
  89. EventManager.RegisterEventHandler(I.FireFunction, handler);
  90. return true;
  91. }
  92. public bool Register(string fName, OnGenericEventHandler handler)
  93. {
  94. if (m_doRemoteCalls) return false;
  95. EventManager.RegisterEventHandler(fName, handler);
  96. return true;
  97. }
  98. [CanBeReflected(ThreatLevel = ThreatLevel.High, RenamedMethod = "SchedulerSave")]
  99. public string Save(SchedulerItem I)
  100. {
  101. if (m_doRemoteCalls)
  102. return (string) DoRemote(I);
  103. return m_database.SchedulerSave(I);
  104. }
  105. [CanBeReflected(ThreatLevel = ThreatLevel.High, RenamedMethod = "SchedulerRemove")]
  106. public void Remove(string id)
  107. {
  108. if (m_doRemoteCalls)
  109. {
  110. DoRemote(id);
  111. return;
  112. }
  113. m_database.SchedulerRemove(id);
  114. }
  115. [CanBeReflected(ThreatLevel = ThreatLevel.Low, RenamedMethod = "SchedulerExist")]
  116. public bool Exist(string scdID)
  117. {
  118. if (m_doRemoteCalls)
  119. return (bool) DoRemote(scdID);
  120. return m_database.SchedulerExist(scdID);
  121. }
  122. [CanBeReflected(ThreatLevel = ThreatLevel.Low, RenamedMethod = "SchedulerGet")]
  123. public SchedulerItem Get(string ID)
  124. {
  125. if (m_doRemoteCalls)
  126. return (SchedulerItem) DoRemote(ID);
  127. return m_database.Get(ID);
  128. }
  129. [CanBeReflected(ThreatLevel = ThreatLevel.Low, RenamedMethod = "SchedulerGet")]
  130. public SchedulerItem Get(string scheduleFor, string fireFunction)
  131. {
  132. if (m_doRemoteCalls)
  133. return (SchedulerItem) DoRemote(scheduleFor, fireFunction);
  134. return m_database.Get(scheduleFor, fireFunction);
  135. }
  136. #endregion
  137. #region Timer
  138. private void t_Elapsed(object sender, ElapsedEventArgs e)
  139. {
  140. scheduleTimer.Enabled = false;
  141. try
  142. {
  143. List<SchedulerItem> CurrentSchedule = m_database.ToRun();
  144. foreach (SchedulerItem I in CurrentSchedule)
  145. {
  146. FireEvent(I);
  147. }
  148. }
  149. catch (Exception ee)
  150. {
  151. MainConsole.Instance.ErrorFormat("[Scheduler] t_Elapsed Error {0}", ee.ToString());
  152. }
  153. finally
  154. {
  155. scheduleTimer.Enabled = true;
  156. }
  157. }
  158. private void FireEvent(SchedulerItem I)
  159. {
  160. try
  161. {
  162. // save chagnes before it fires in case its chagned during the fire
  163. I = m_database.SaveHistory(I);
  164. if (I.RunOnce) I.Enabled = false;
  165. if (I.Enabled) I.CalculateNextRunTime(I.TimeToRun);
  166. if (!I.HisotryKeep)
  167. m_database.HistoryDeleteOld(I);
  168. m_database.SchedulerSave(I);
  169. // now fire
  170. List<Object> reciept = EventManager.FireGenericEventHandler(I.FireFunction, I.FireParams);
  171. if (!I.HistoryReciept)
  172. I = m_database.SaveHistoryComplete(I);
  173. else
  174. {
  175. foreach (string results in reciept.Cast<string>().Where(results => results != ""))
  176. {
  177. m_database.SaveHistoryCompleteReciept(I.HistoryLastID, results);
  178. }
  179. }
  180. }
  181. catch (Exception e)
  182. {
  183. MainConsole.Instance.ErrorFormat("[Scheduler] FireEvent Error {0}: {1}", I.id, e.ToString());
  184. }
  185. }
  186. public void MarkComplete(string history_id, string reciept)
  187. {
  188. m_database.SaveHistoryCompleteReciept(history_id, reciept);
  189. }
  190. #endregion
  191. }
  192. }