using SafeMobileLib; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.IO.Pipes; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace AppWatchDog { class Program { private static bool isRunning = false; private static int smallNrOfWorkersCount = 0; private static bool isOnlySimulating = false; static void Main(string[] args) { if (args.Length > 0) isOnlySimulating = true; isRunning = true; WatchDogWorker(); while (true) { Thread.Sleep(10); } } private static void WatchDogWorker() { int count = 0; // wait 10 seconds before starting to check for AppServer while(count++ < 100) Thread.Sleep(100); count = 0; while (isRunning) { Thread.Sleep(100); if (count++ > 30 && isRunning) { using (NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", "appServerPipe", PipeDirection.In)) { try { // Connect to the pipe or wait until the pipe is available. Console.Write("Attempting to connect to pipe..."); pipeClient.Connect(1000); Console.WriteLine("Connected to pipe."); Console.WriteLine("There are currently {0} pipe server instances open.", pipeClient.NumberOfServerInstances); using (StreamReader sr = new StreamReader(pipeClient)) { // Display the read text to the console string temp; while ((temp = sr.ReadLine()) != null) { Console.WriteLine("Received from server: {0}", temp); String[] split = temp.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries); int numberOfWorkersQueueCount = 0; if (split.Length > 0) Int32.TryParse(split[0], out numberOfWorkersQueueCount); if (numberOfWorkersQueueCount < 200) smallNrOfWorkersCount++; if (smallNrOfWorkersCount >= 5) { Utils.WriteLine("WatchDog detected slow GPS queue polling : " + smallNrOfWorkersCount, ConsoleColor.Yellow); Utils.WriteEventLog("AppWatchDog", "WatchDog detected slow GPS queue polling : " + smallNrOfWorkersCount, EventLogEntryType.Warning, 406); RestartAppServer(); } } } } catch (Exception ex) { Utils.WriteLine( "WatchDog unable to connect to namedpipe", ConsoleColor.Yellow); Utils.WriteEventLog("AppWatchDog", "WatchDog unable to connect to namedpipe", EventLogEntryType.Error, 405); RestartAppServer(); } } count = 0; } } } private static void RestartAppServer() { String serviceLocation = System.Reflection.Assembly.GetExecutingAssembly().Location; String serviceFolder = serviceLocation.Substring(0, serviceLocation.LastIndexOf('\\')); if (!isOnlySimulating) { ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = serviceFolder + "\\" + "AppServer.exe"; startInfo.Arguments = "-c"; Process.Start(startInfo); } //Start(); } } }