RestartModule.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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.Linq;
  29. using System.Reflection;
  30. using System.Timers;
  31. using System.IO;
  32. using System.Diagnostics;
  33. using System.Threading;
  34. using System.Collections.Generic;
  35. using log4net;
  36. using Nini.Config;
  37. using OpenMetaverse;
  38. using OpenSim.Framework;
  39. using OpenSim.Framework.Console;
  40. using OpenSim.Region.Framework.Interfaces;
  41. using OpenSim.Region.Framework.Scenes;
  42. using Timer=System.Timers.Timer;
  43. using Mono.Addins;
  44. namespace OpenSim.Region.CoreModules.World.Region
  45. {
  46. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "RestartModule")]
  47. public class RestartModule : INonSharedRegionModule, IRestartModule
  48. {
  49. private static readonly ILog m_log =
  50. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  51. protected Scene m_Scene;
  52. protected Timer m_CountdownTimer = null;
  53. protected DateTime m_RestartBegin;
  54. protected List<int> m_Alerts;
  55. protected string m_Message;
  56. protected UUID m_Initiator;
  57. protected bool m_Notice = false;
  58. protected IDialogModule m_DialogModule = null;
  59. protected string m_MarkerPath = String.Empty;
  60. private int[] m_CurrentAlerts = null;
  61. protected bool m_shortCircuitDelays = false;
  62. protected bool m_rebootAll = false;
  63. public void Initialise(IConfigSource config)
  64. {
  65. IConfig restartConfig = config.Configs["RestartModule"];
  66. if (restartConfig != null)
  67. {
  68. m_MarkerPath = restartConfig.GetString("MarkerPath", String.Empty);
  69. }
  70. IConfig startupConfig = config.Configs["Startup"];
  71. m_shortCircuitDelays = startupConfig.GetBoolean("SkipDelayOnEmptyRegion", false);
  72. m_rebootAll = startupConfig.GetBoolean("InworldRestartShutsDown", false);
  73. }
  74. public void AddRegion(Scene scene)
  75. {
  76. if (m_MarkerPath != String.Empty)
  77. File.Delete(Path.Combine(m_MarkerPath,
  78. scene.RegionInfo.RegionID.ToString()));
  79. m_Scene = scene;
  80. scene.RegisterModuleInterface<IRestartModule>(this);
  81. MainConsole.Instance.Commands.AddCommand("Regions",
  82. false, "region restart bluebox",
  83. "region restart bluebox <message> <delta seconds>+",
  84. "Schedule a region restart",
  85. "Schedule a region restart after a given number of seconds. If one delta is given then the region is restarted in delta seconds time. A time to restart is sent to users in the region as a dismissable bluebox notice. If multiple deltas are given then a notice is sent when we reach each delta.",
  86. HandleRegionRestart);
  87. MainConsole.Instance.Commands.AddCommand("Regions",
  88. false, "region restart notice",
  89. "region restart notice <message> <delta seconds>+",
  90. "Schedule a region restart",
  91. "Schedule a region restart after a given number of seconds. If one delta is given then the region is restarted in delta seconds time. A time to restart is sent to users in the region as a transient notice. If multiple deltas are given then a notice is sent when we reach each delta.",
  92. HandleRegionRestart);
  93. MainConsole.Instance.Commands.AddCommand("Regions",
  94. false, "region restart abort",
  95. "region restart abort [<message>]",
  96. "Abort a region restart", HandleRegionRestart);
  97. }
  98. public void RegionLoaded(Scene scene)
  99. {
  100. m_DialogModule = m_Scene.RequestModuleInterface<IDialogModule>();
  101. }
  102. public void RemoveRegion(Scene scene)
  103. {
  104. }
  105. public void Close()
  106. {
  107. }
  108. public string Name
  109. {
  110. get { return "RestartModule"; }
  111. }
  112. public Type ReplaceableInterface
  113. {
  114. get { return typeof(IRestartModule); }
  115. }
  116. public TimeSpan TimeUntilRestart
  117. {
  118. get { return DateTime.Now - m_RestartBegin; }
  119. }
  120. public void ScheduleRestart(UUID initiator, string message, int[] alerts, bool notice)
  121. {
  122. if (m_CountdownTimer != null)
  123. {
  124. m_CountdownTimer.Stop();
  125. m_CountdownTimer = null;
  126. }
  127. if (alerts == null)
  128. {
  129. CreateMarkerFile();
  130. m_Scene.RestartNow();
  131. return;
  132. }
  133. m_Message = message;
  134. m_Initiator = initiator;
  135. m_Notice = notice;
  136. m_CurrentAlerts = alerts;
  137. m_Alerts = new List<int>(alerts);
  138. m_Alerts.Sort();
  139. m_Alerts.Reverse();
  140. if (m_Alerts[0] == 0)
  141. {
  142. CreateMarkerFile();
  143. m_Scene.RestartNow();
  144. return;
  145. }
  146. int nextInterval = DoOneNotice(true);
  147. SetTimer(nextInterval);
  148. }
  149. public int DoOneNotice(bool sendOut)
  150. {
  151. if (m_Alerts.Count == 0 || m_Alerts[0] == 0)
  152. {
  153. CreateMarkerFile();
  154. m_Scene.RestartNow();
  155. return 0;
  156. }
  157. int nextAlert = 0;
  158. while (m_Alerts.Count > 1)
  159. {
  160. if (m_Alerts[1] == m_Alerts[0])
  161. {
  162. m_Alerts.RemoveAt(0);
  163. continue;
  164. }
  165. nextAlert = m_Alerts[1];
  166. break;
  167. }
  168. int currentAlert = m_Alerts[0];
  169. m_Alerts.RemoveAt(0);
  170. if (sendOut)
  171. {
  172. int minutes = currentAlert / 60;
  173. string currentAlertString = String.Empty;
  174. if (minutes > 0)
  175. {
  176. if (minutes == 1)
  177. currentAlertString += "1 minute";
  178. else
  179. currentAlertString += String.Format("{0} minutes", minutes);
  180. if ((currentAlert % 60) != 0)
  181. currentAlertString += " and ";
  182. }
  183. if ((currentAlert % 60) != 0)
  184. {
  185. int seconds = currentAlert % 60;
  186. if (seconds == 1)
  187. currentAlertString += "1 second";
  188. else
  189. currentAlertString += String.Format("{0} seconds", seconds);
  190. }
  191. string msg = String.Format(m_Message, currentAlertString);
  192. if (m_DialogModule != null && msg != String.Empty)
  193. {
  194. if (m_Notice)
  195. m_DialogModule.SendGeneralAlert(msg);
  196. else
  197. m_DialogModule.SendNotificationToUsersInRegion(m_Initiator, "System", msg);
  198. }
  199. }
  200. return currentAlert - nextAlert;
  201. }
  202. public void SetTimer(int intervalSeconds)
  203. {
  204. if (intervalSeconds > 0)
  205. {
  206. m_CountdownTimer = new Timer();
  207. m_CountdownTimer.AutoReset = false;
  208. m_CountdownTimer.Interval = intervalSeconds * 1000;
  209. m_CountdownTimer.Elapsed += OnTimer;
  210. m_CountdownTimer.Start();
  211. }
  212. else if (m_CountdownTimer != null)
  213. {
  214. m_CountdownTimer.Stop();
  215. m_CountdownTimer = null;
  216. }
  217. else
  218. {
  219. m_log.WarnFormat(
  220. "[RESTART MODULE]: Tried to set restart timer to {0} in {1}, which is not a valid interval",
  221. intervalSeconds, m_Scene.Name);
  222. }
  223. }
  224. private void OnTimer(object source, ElapsedEventArgs e)
  225. {
  226. int nextInterval = DoOneNotice(true);
  227. if (m_shortCircuitDelays)
  228. {
  229. if (CountAgents() == 0)
  230. {
  231. m_Scene.RestartNow();
  232. return;
  233. }
  234. }
  235. SetTimer(nextInterval);
  236. }
  237. public void DelayRestart(int seconds, string message)
  238. {
  239. if (m_CountdownTimer == null)
  240. return;
  241. m_CountdownTimer.Stop();
  242. m_CountdownTimer = null;
  243. m_Alerts = new List<int>(m_CurrentAlerts);
  244. m_Alerts.Add(seconds);
  245. m_Alerts.Sort();
  246. m_Alerts.Reverse();
  247. int nextInterval = DoOneNotice(false);
  248. SetTimer(nextInterval);
  249. }
  250. public void AbortRestart(string message)
  251. {
  252. if (m_CountdownTimer != null)
  253. {
  254. m_CountdownTimer.Stop();
  255. m_CountdownTimer = null;
  256. if (m_DialogModule != null && message != String.Empty)
  257. m_DialogModule.SendNotificationToUsersInRegion(UUID.Zero, "System", message);
  258. //m_DialogModule.SendGeneralAlert(message);
  259. }
  260. if (m_MarkerPath != String.Empty)
  261. File.Delete(Path.Combine(m_MarkerPath,
  262. m_Scene.RegionInfo.RegionID.ToString()));
  263. }
  264. private void HandleRegionRestart(string module, string[] args)
  265. {
  266. if (!(MainConsole.Instance.ConsoleScene is Scene))
  267. return;
  268. if (MainConsole.Instance.ConsoleScene != m_Scene)
  269. return;
  270. if (args.Length < 5)
  271. {
  272. if (args.Length > 2)
  273. {
  274. if (args[2] == "abort")
  275. {
  276. string msg = String.Empty;
  277. if (args.Length > 3)
  278. msg = args[3];
  279. AbortRestart(msg);
  280. MainConsole.Instance.Output("Region restart aborted");
  281. return;
  282. }
  283. }
  284. MainConsole.Instance.Output("Error: restart region <mode> <name> <delta seconds>+");
  285. return;
  286. }
  287. bool notice = false;
  288. if (args[2] == "notice")
  289. notice = true;
  290. List<int> times = new List<int>();
  291. for (int i = 4 ; i < args.Length ; i++)
  292. times.Add(Convert.ToInt32(args[i]));
  293. MainConsole.Instance.Output(
  294. "Region {0} scheduled for restart in {1} seconds", null, m_Scene.Name, times.Sum());
  295. ScheduleRestart(UUID.Zero, args[3], times.ToArray(), notice);
  296. }
  297. protected void CreateMarkerFile()
  298. {
  299. if (m_MarkerPath == String.Empty)
  300. return;
  301. string path = Path.Combine(m_MarkerPath, m_Scene.RegionInfo.RegionID.ToString());
  302. try
  303. {
  304. string pidstring = System.Diagnostics.Process.GetCurrentProcess().Id.ToString();
  305. FileStream fs = File.Create(path);
  306. System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
  307. Byte[] buf = enc.GetBytes(pidstring);
  308. fs.Write(buf, 0, buf.Length);
  309. fs.Close();
  310. }
  311. catch (Exception)
  312. {
  313. }
  314. }
  315. int CountAgents()
  316. {
  317. m_log.Info("[RESTART MODULE]: Counting affected avatars");
  318. int agents = 0;
  319. if (m_rebootAll)
  320. {
  321. foreach (Scene s in SceneManager.Instance.Scenes)
  322. {
  323. foreach (ScenePresence sp in s.GetScenePresences())
  324. {
  325. if (!sp.IsChildAgent && !sp.IsNPC)
  326. agents++;
  327. }
  328. }
  329. }
  330. else
  331. {
  332. foreach (ScenePresence sp in m_Scene.GetScenePresences())
  333. {
  334. if (!sp.IsChildAgent && !sp.IsNPC)
  335. agents++;
  336. }
  337. }
  338. m_log.InfoFormat("[RESTART MODULE]: Avatars in region: {0}", agents);
  339. return agents;
  340. }
  341. }
  342. }