diff options
Updated svn properties.
Diffstat (limited to 'OpenSim/Region/ScriptEngine/Common/ScriptEngineBase/MaintenanceThread.cs')
-rw-r--r-- | OpenSim/Region/ScriptEngine/Common/ScriptEngineBase/MaintenanceThread.cs | 326 |
1 files changed, 163 insertions, 163 deletions
diff --git a/OpenSim/Region/ScriptEngine/Common/ScriptEngineBase/MaintenanceThread.cs b/OpenSim/Region/ScriptEngine/Common/ScriptEngineBase/MaintenanceThread.cs index fc2fda9..ae6f001 100644 --- a/OpenSim/Region/ScriptEngine/Common/ScriptEngineBase/MaintenanceThread.cs +++ b/OpenSim/Region/ScriptEngine/Common/ScriptEngineBase/MaintenanceThread.cs | |||
@@ -1,163 +1,163 @@ | |||
1 | using System; | 1 | using System; |
2 | using System.Collections.Generic; | 2 | using System.Collections.Generic; |
3 | using System.Text; | 3 | using System.Text; |
4 | using System.Threading; | 4 | using System.Threading; |
5 | 5 | ||
6 | namespace OpenSim.Region.ScriptEngine.Common.ScriptEngineBase | 6 | namespace OpenSim.Region.ScriptEngine.Common.ScriptEngineBase |
7 | { | 7 | { |
8 | /// <summary> | 8 | /// <summary> |
9 | /// This class does maintenance on script engine. | 9 | /// This class does maintenance on script engine. |
10 | /// </summary> | 10 | /// </summary> |
11 | public class MaintenanceThread : iScriptEngineFunctionModule | 11 | public class MaintenanceThread : iScriptEngineFunctionModule |
12 | { | 12 | { |
13 | public ScriptEngine m_ScriptEngine; | 13 | public ScriptEngine m_ScriptEngine; |
14 | private int MaintenanceLoopms; | 14 | private int MaintenanceLoopms; |
15 | 15 | ||
16 | public MaintenanceThread(ScriptEngine _ScriptEngine) | 16 | public MaintenanceThread(ScriptEngine _ScriptEngine) |
17 | { | 17 | { |
18 | m_ScriptEngine = _ScriptEngine; | 18 | m_ScriptEngine = _ScriptEngine; |
19 | 19 | ||
20 | ReadConfig(); | 20 | ReadConfig(); |
21 | 21 | ||
22 | // Start maintenance thread | 22 | // Start maintenance thread |
23 | StartMaintenanceThread(); | 23 | StartMaintenanceThread(); |
24 | } | 24 | } |
25 | 25 | ||
26 | ~MaintenanceThread() | 26 | ~MaintenanceThread() |
27 | { | 27 | { |
28 | StopMaintenanceThread(); | 28 | StopMaintenanceThread(); |
29 | } | 29 | } |
30 | 30 | ||
31 | public void ReadConfig() | 31 | public void ReadConfig() |
32 | { | 32 | { |
33 | MaintenanceLoopms = m_ScriptEngine.ScriptConfigSource.GetInt("MaintenanceLoopms", 50); | 33 | MaintenanceLoopms = m_ScriptEngine.ScriptConfigSource.GetInt("MaintenanceLoopms", 50); |
34 | } | 34 | } |
35 | 35 | ||
36 | 36 | ||
37 | #region " Maintenance thread " | 37 | #region " Maintenance thread " |
38 | /// <summary> | 38 | /// <summary> |
39 | /// Maintenance thread. Enforcing max execution time for example. | 39 | /// Maintenance thread. Enforcing max execution time for example. |
40 | /// </summary> | 40 | /// </summary> |
41 | public Thread MaintenanceThreadThread; | 41 | public Thread MaintenanceThreadThread; |
42 | 42 | ||
43 | /// <summary> | 43 | /// <summary> |
44 | /// Starts maintenance thread | 44 | /// Starts maintenance thread |
45 | /// </summary> | 45 | /// </summary> |
46 | private void StartMaintenanceThread() | 46 | private void StartMaintenanceThread() |
47 | { | 47 | { |
48 | if (MaintenanceThreadThread == null) | 48 | if (MaintenanceThreadThread == null) |
49 | { | 49 | { |
50 | MaintenanceThreadThread = new Thread(MaintenanceLoop); | 50 | MaintenanceThreadThread = new Thread(MaintenanceLoop); |
51 | MaintenanceThreadThread.Name = "ScriptMaintenanceThread"; | 51 | MaintenanceThreadThread.Name = "ScriptMaintenanceThread"; |
52 | MaintenanceThreadThread.IsBackground = true; | 52 | MaintenanceThreadThread.IsBackground = true; |
53 | MaintenanceThreadThread.Start(); | 53 | MaintenanceThreadThread.Start(); |
54 | } | 54 | } |
55 | } | 55 | } |
56 | 56 | ||
57 | /// <summary> | 57 | /// <summary> |
58 | /// Stops maintenance thread | 58 | /// Stops maintenance thread |
59 | /// </summary> | 59 | /// </summary> |
60 | private void StopMaintenanceThread() | 60 | private void StopMaintenanceThread() |
61 | { | 61 | { |
62 | #if DEBUG | 62 | #if DEBUG |
63 | m_ScriptEngine.Log.Debug(m_ScriptEngine.ScriptEngineName, "StopMaintenanceThread() called"); | 63 | m_ScriptEngine.Log.Debug(m_ScriptEngine.ScriptEngineName, "StopMaintenanceThread() called"); |
64 | #endif | 64 | #endif |
65 | PleaseShutdown = true; | 65 | PleaseShutdown = true; |
66 | Thread.Sleep(100); | 66 | Thread.Sleep(100); |
67 | try | 67 | try |
68 | { | 68 | { |
69 | if (MaintenanceThreadThread != null) | 69 | if (MaintenanceThreadThread != null) |
70 | { | 70 | { |
71 | if (MaintenanceThreadThread.IsAlive) | 71 | if (MaintenanceThreadThread.IsAlive) |
72 | { | 72 | { |
73 | MaintenanceThreadThread.Abort(); | 73 | MaintenanceThreadThread.Abort(); |
74 | } | 74 | } |
75 | } | 75 | } |
76 | } | 76 | } |
77 | catch (Exception ex) | 77 | catch (Exception ex) |
78 | { | 78 | { |
79 | m_ScriptEngine.Log.Error(m_ScriptEngine.ScriptEngineName, "Exception stopping maintenence thread: " + ex.ToString()); | 79 | m_ScriptEngine.Log.Error(m_ScriptEngine.ScriptEngineName, "Exception stopping maintenence thread: " + ex.ToString()); |
80 | } | 80 | } |
81 | 81 | ||
82 | } | 82 | } |
83 | 83 | ||
84 | /// <summary> | 84 | /// <summary> |
85 | /// A thread should run in this loop and check all running scripts | 85 | /// A thread should run in this loop and check all running scripts |
86 | /// </summary> | 86 | /// </summary> |
87 | public void MaintenanceLoop() | 87 | public void MaintenanceLoop() |
88 | { | 88 | { |
89 | if (m_ScriptEngine.m_EventQueueManager.maxFunctionExecutionTimens < MaintenanceLoopms) | 89 | if (m_ScriptEngine.m_EventQueueManager.maxFunctionExecutionTimens < MaintenanceLoopms) |
90 | m_ScriptEngine.Log.Warn(m_ScriptEngine.ScriptEngineName, | 90 | m_ScriptEngine.Log.Warn(m_ScriptEngine.ScriptEngineName, |
91 | "Configuration error: MaxEventExecutionTimeMs is less than MaintenanceLoopms. The Maintenance Loop will only check scripts once per run."); | 91 | "Configuration error: MaxEventExecutionTimeMs is less than MaintenanceLoopms. The Maintenance Loop will only check scripts once per run."); |
92 | 92 | ||
93 | while (true) | 93 | while (true) |
94 | { | 94 | { |
95 | try | 95 | try |
96 | { | 96 | { |
97 | long Last_maxFunctionExecutionTimens = 0; // DateTime.Now.Ticks; | 97 | long Last_maxFunctionExecutionTimens = 0; // DateTime.Now.Ticks; |
98 | long Last_ReReadConfigFilens = DateTime.Now.Ticks; | 98 | long Last_ReReadConfigFilens = DateTime.Now.Ticks; |
99 | while (true) | 99 | while (true) |
100 | { | 100 | { |
101 | System.Threading.Thread.Sleep(MaintenanceLoopms); // Sleep before next pass | 101 | System.Threading.Thread.Sleep(MaintenanceLoopms); // Sleep before next pass |
102 | if (PleaseShutdown) | 102 | if (PleaseShutdown) |
103 | return; | 103 | return; |
104 | 104 | ||
105 | if (m_ScriptEngine != null) | 105 | if (m_ScriptEngine != null) |
106 | { | 106 | { |
107 | // | 107 | // |
108 | // Re-reading config every x seconds | 108 | // Re-reading config every x seconds |
109 | // | 109 | // |
110 | if (m_ScriptEngine.RefreshConfigFileSeconds > 0) | 110 | if (m_ScriptEngine.RefreshConfigFileSeconds > 0) |
111 | { | 111 | { |
112 | // Check if its time to re-read config | 112 | // Check if its time to re-read config |
113 | if (DateTime.Now.Ticks - Last_ReReadConfigFilens > m_ScriptEngine.RefreshConfigFilens) | 113 | if (DateTime.Now.Ticks - Last_ReReadConfigFilens > m_ScriptEngine.RefreshConfigFilens) |
114 | { | 114 | { |
115 | // Its time to re-read config file | 115 | // Its time to re-read config file |
116 | m_ScriptEngine.ReadConfig(); | 116 | m_ScriptEngine.ReadConfig(); |
117 | Last_ReReadConfigFilens = DateTime.Now.Ticks; // Reset time | 117 | Last_ReReadConfigFilens = DateTime.Now.Ticks; // Reset time |
118 | } | 118 | } |
119 | } | 119 | } |
120 | 120 | ||
121 | // | 121 | // |
122 | // Adjust number of running script threads if not correct | 122 | // Adjust number of running script threads if not correct |
123 | // | 123 | // |
124 | if (m_ScriptEngine.m_EventQueueManager != null) | 124 | if (m_ScriptEngine.m_EventQueueManager != null) |
125 | m_ScriptEngine.m_EventQueueManager.AdjustNumberOfScriptThreads(); | 125 | m_ScriptEngine.m_EventQueueManager.AdjustNumberOfScriptThreads(); |
126 | 126 | ||
127 | // | 127 | // |
128 | // Check if any script has exceeded its max execution time | 128 | // Check if any script has exceeded its max execution time |
129 | // | 129 | // |
130 | if (m_ScriptEngine.m_EventQueueManager != null && m_ScriptEngine.m_EventQueueManager.EnforceMaxExecutionTime) | 130 | if (m_ScriptEngine.m_EventQueueManager != null && m_ScriptEngine.m_EventQueueManager.EnforceMaxExecutionTime) |
131 | { | 131 | { |
132 | // We are enforcing execution time | 132 | // We are enforcing execution time |
133 | if (DateTime.Now.Ticks - Last_maxFunctionExecutionTimens > | 133 | if (DateTime.Now.Ticks - Last_maxFunctionExecutionTimens > |
134 | m_ScriptEngine.m_EventQueueManager.maxFunctionExecutionTimens) | 134 | m_ScriptEngine.m_EventQueueManager.maxFunctionExecutionTimens) |
135 | { | 135 | { |
136 | // Its time to check again | 136 | // Its time to check again |
137 | m_ScriptEngine.m_EventQueueManager.CheckScriptMaxExecTime(); // Do check | 137 | m_ScriptEngine.m_EventQueueManager.CheckScriptMaxExecTime(); // Do check |
138 | Last_maxFunctionExecutionTimens = DateTime.Now.Ticks; // Reset time | 138 | Last_maxFunctionExecutionTimens = DateTime.Now.Ticks; // Reset time |
139 | } | 139 | } |
140 | } | 140 | } |
141 | } // m_ScriptEngine != null | 141 | } // m_ScriptEngine != null |
142 | } | 142 | } |
143 | } | 143 | } |
144 | catch (Exception ex) | 144 | catch (Exception ex) |
145 | { | 145 | { |
146 | m_ScriptEngine.Log.Error(m_ScriptEngine.ScriptEngineName, "Exception in MaintenanceLoopThread. Thread will recover after 5 sec throttle. Exception: " + ex.ToString()); | 146 | m_ScriptEngine.Log.Error(m_ScriptEngine.ScriptEngineName, "Exception in MaintenanceLoopThread. Thread will recover after 5 sec throttle. Exception: " + ex.ToString()); |
147 | Thread.Sleep(5000); | 147 | Thread.Sleep(5000); |
148 | } | 148 | } |
149 | } | 149 | } |
150 | } | 150 | } |
151 | #endregion | 151 | #endregion |
152 | /// <summary> | 152 | /// <summary> |
153 | /// If set to true then threads and stuff should try to make a graceful exit | 153 | /// If set to true then threads and stuff should try to make a graceful exit |
154 | /// </summary> | 154 | /// </summary> |
155 | public bool PleaseShutdown | 155 | public bool PleaseShutdown |
156 | { | 156 | { |
157 | get { return _PleaseShutdown; } | 157 | get { return _PleaseShutdown; } |
158 | set { _PleaseShutdown = value; } | 158 | set { _PleaseShutdown = value; } |
159 | } | 159 | } |
160 | private bool _PleaseShutdown = false; | 160 | private bool _PleaseShutdown = false; |
161 | 161 | ||
162 | } | 162 | } |
163 | } | 163 | } |