29 using System.Reflection;
30 using System.Threading;
33 namespace OpenSim.Framework.Monitoring
52 public static class WorkManager
54 private static readonly ILog m_log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
56 public static JobEngine JobEngine {
get;
private set; }
60 JobEngine =
new JobEngine(
"Non-blocking non-critical job engine",
"JOB ENGINE");
62 StatsManager.RegisterStat(
65 "Number of jobs waiting for processing.",
72 stat => stat.Value = JobEngine.JobsWaiting,
75 MainConsole.Instance.Commands.AddCommand(
79 "debug jobengine <start|stop|status|log>",
80 "Start, stop, get status or set logging level of the job engine.",
81 "If stopped then all outstanding jobs are processed immediately.",
82 HandleControlCommand);
95 public static Thread StartThread(
96 ThreadStart start,
string name, ThreadPriority priority,
bool isBackground,
bool alarmIfTimeout,
bool log =
true)
98 return StartThread(start, name, priority, isBackground, alarmIfTimeout, null, Watchdog.DEFAULT_WATCHDOG_TIMEOUT_MS, log);
117 public static Thread StartThread(
118 ThreadStart start,
string name, ThreadPriority priority,
bool isBackground,
119 bool alarmIfTimeout, Func<string> alarmMethod,
int timeout,
bool log =
true)
122 thread.Priority = priority;
123 thread.IsBackground = isBackground;
125 Watchdog.ThreadWatchdogInfo twi
126 =
new Watchdog.ThreadWatchdogInfo(thread, timeout, name)
127 { AlarmIfTimeout = alarmIfTimeout, AlarmMethod = alarmMethod };
129 Watchdog.AddThread(twi, name, log:log);
144 public static void RunInThread(WaitCallback callback,
object obj,
string name,
bool log =
false)
148 Culture.SetCurrentCulture();
153 ThreadStart ts =
new ThreadStart(delegate()
157 Culture.SetCurrentCulture();
159 Watchdog.RemoveThread(log:
false);
163 m_log.Error(string.Format(
"[WATCHDOG]: Exception in thread {0}.", name), e);
167 StartThread(ts, name, ThreadPriority.Normal,
true,
false, log:log);
179 public static void RunInThreadPool(System.Threading.WaitCallback callback,
object obj,
string name)
181 Util.FireAndForget(callback, obj, name);
212 public static void RunJob(
213 string jobType, WaitCallback callback,
object obj,
string name,
214 bool canRunInThisThread =
false,
bool mustNotTimeout =
false,
219 Culture.SetCurrentCulture();
224 if (JobEngine.IsRunning)
225 JobEngine.QueueJob(name, () => callback(obj));
226 else if (canRunInThisThread)
228 else if (mustNotTimeout)
229 RunInThread(callback, obj, name, log);
231 Util.FireAndForget(callback, obj, name);
234 private static void HandleControlCommand(
string module,
string[] args)
241 MainConsole.Instance.Output(
"Usage: debug jobengine <stop|start|status|log>");
245 string subCommand = args[2];
247 if (subCommand ==
"stop")
250 MainConsole.Instance.OutputFormat(
"Stopped job engine.");
252 else if (subCommand ==
"start")
255 MainConsole.Instance.OutputFormat(
"Started job engine.");
257 else if (subCommand ==
"status")
259 MainConsole.Instance.OutputFormat(
"Job engine running: {0}", JobEngine.IsRunning);
261 JobEngine.Job job = JobEngine.CurrentJob;
262 MainConsole.Instance.OutputFormat(
"Current job {0}", job != null ? job.Name :
"none");
264 MainConsole.Instance.OutputFormat(
265 "Jobs waiting: {0}", JobEngine.IsRunning ? JobEngine.JobsWaiting.ToString() :
"n/a");
266 MainConsole.Instance.OutputFormat(
"Log Level: {0}", JobEngine.LogLevel);
268 else if (subCommand ==
"log")
272 MainConsole.Instance.Output(
"Usage: debug jobengine log <level>");
277 int logLevel = int.Parse(args[3]);
280 JobEngine.LogLevel = logLevel;
281 MainConsole.Instance.OutputFormat(
"Set debug log level to {0}", JobEngine.LogLevel);
286 MainConsole.Instance.OutputFormat(
"Unrecognized job engine subcommand {0}", subCommand);
FireAndForgetMethod
The method used by Util.FireAndForget for asynchronously firing events
StatVerbosity
Verbosity of stat.
MeasuresOfInterest
Measures of interest for this stat.