示例#1
0
        static int Main(string[] args)
        {
            ///////////
            // Initialize
            bool bRestart;
            bRestart = false;
            bool bQuit = false;
            int max_worker = 0;
            int max_port = 0;
            ArrayList WatchedFolders = new ArrayList();
            App AppClass = new App();
            cApp = AppClass;

            cApp.HOST = Dns.GetHostName();
            IPHostEntry iphe;
            iphe = Dns.GetHostByName(cApp.HOST);
            IPAddress[] ipAddresses = iphe.AddressList;
            cApp.IP = "";
            for (int i = 0; i < ipAddresses.GetUpperBound(0); i++)
            {
                if (i > 0) cApp.IP += ",";
                cApp.IP += ipAddresses[i].ToString();
            }
            //Console.WriteLine("{0},{1}", cApp.IP, cApp.HOST);

            // start queue thread
            Queue QueueClass = new Queue(ref AppClass);
            Thread thQueue = new Thread(new ThreadStart(QueueClass.Main));
            thQueue.Start();

            // start queue processing thread
            QueueWorkerClass = new QueueWorker(ref AppClass);
            Thread thQueueWorker = new Thread(new ThreadStart(QueueWorkerClass.Main));
            thQueueWorker.Start();

            AppClass.settings.sIniFile = "settings.xml";
            AppClass.settings.sLogFile = "logfile.txt";

            FileSystemWatcher IniWatcher = new FileSystemWatcher();
            IniWatcher.Path = ".";
            IniWatcher.Filter = AppClass.settings.sIniFile;
            IniWatcher.Changed += new FileSystemEventHandler(OnChanged);
            IniWatcher.NotifyFilter = System.IO.NotifyFilters.LastWrite;
            IniWatcher.EnableRaisingEvents = true;

            if (!LoadSettings())
            {
                bQuit = true;
                cApp.bRunning = false;
            }

            bool bPidExisted = false;
            if (File.Exists("MediaMachine.pid"))
            {
                bPidExisted = true;
            }
            if (!App.MXLockPid())
            {
                // Can't lock pid file
                string sTempResults = QueueWorkerClass.MXRunScript("alreadyrunning()", "", "Cant lock pid file MediaMachine.pid, so the application must be already running.");
                if (sTempResults != "done")
                {
                    Console.WriteLine(DateTime.Now.ToString("yyMMdd-HH:mm") + " Warning: Problems while executing script 'alreadyrunning()', Error msg = {0}", sTempResults);
                }
                bQuit = true;
                cApp.bRunning = false;
            }

            if (!bQuit)
            {
                ///////////
                // Start console and log writer
                ArrayList MemoryLog = new ArrayList();
                cApp.MemoryLog = MemoryLog;
                SplitWriter OutWriter = new SplitWriter(AppClass.settings.sLogFile, true, ref MemoryLog);
                Console.SetOut(OutWriter);

                //////////////////////////////////////////
                //
                // MAIN LOOP
                //
                //////////////////////////////////////////
                App.ShowBanner();

                // show current settings
                ThreadPool.GetMaxThreads(out max_worker, out max_port);
                AppClass.settings.iMaxThreads = max_worker;
                Console.WriteLine(DateTime.Now.ToString("yyMMdd-HH:mm") + " INFO - Default settings:");
                Console.WriteLine(DateTime.Now.ToString("yyMMdd-HH:mm") + " INFO -   TimeOut: {0} seconds", AppClass.settings.iTimeOut);
                Console.WriteLine(DateTime.Now.ToString("yyMMdd-HH:mm") + " INFO -   WaitTime: {0} seconds", AppClass.settings.iWaitTime);
                if ( AppClass.settings.iScanDelay != 0 )
                    Console.WriteLine(DateTime.Now.ToString("yyMMdd-HH:mm") + " INFO -   ScanDelay: {0} seconds", AppClass.settings.iScanDelay);
                else
                    Console.WriteLine(DateTime.Now.ToString("yyMMdd-HH:mm") + " INFO -   ScanDelay: Using system events");
                Console.WriteLine(DateTime.Now.ToString("yyMMdd-HH:mm") + " INFO -   IgnoreTime: {0} seconds", AppClass.settings.iIgnoreTime);
                Console.WriteLine(DateTime.Now.ToString("yyMMdd-HH:mm") + " INFO - OS System status:");
                Console.WriteLine(DateTime.Now.ToString("yyMMdd-HH:mm") + " INFO -   Maximum Worker threads: {0}", max_worker);
                Console.WriteLine(DateTime.Now.ToString("yyMMdd-HH:mm") + " INFO -   Maximum Aynchronous I/O threads: {0}", max_port);

                // load plugins
                App.LoadPlugins();

                //AutoResetEvent mainEvent = new AutoResetEvent(false);

                // start monitor thread
                Monitor MonitorClass = new Monitor(ref AppClass, ref QueueWorkerClass);
                Thread thMonitor = new Thread(new ThreadStart(MonitorClass.Main));
                thMonitor.Start();

                // add files from job folders
                foreach (string sPath in AppClass.settings.aFoldersPath)
                {
                    App.MXProcessDirectory(sPath, false);
                }

                lock (Console.Out)
                {
                    if (bPidExisted)
                    {
                        // read logfiles last 10 lines into memory
                        FileStream fsin = new FileStream("logfile.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                        TextReader tin = new StreamReader(fsin, true);
                        // read log
                        ArrayList tmpArray = new ArrayList();
                        string sLog = "... Last 10 lines from logfile ...\n\n";
                        while (tin.Peek() != -1)
                        {
                            string sTmp = tin.ReadLine();
                            tmpArray.Add(sTmp);
                        }
                        tin.Close();
                        // get last 10 lines... first try to find place where we have the startup banner
                        int i = 0, k = 0;
                        for (int j = tmpArray.Count-1; j >= 0; j--)
                        {
                            if (tmpArray[j].ToString().StartsWith("==================="))
                            {
                                if (tmpArray[j + 1].ToString().Contains("EVENT"))
                                {
                                    k = j;
                                    if (k < 0) k = 0;
                                    i = j - 12;
                                    if (i<0) i=0;
                                    break;
                                }
                            }
                        }
                        for (int j = i; j < k; j++)
                        {
                            sLog += tmpArray[j] + "\n";
                        }
                        sLog += "... end of log ...\n";
                        string sTempResults = QueueWorkerClass.MXRunScript("unplannedshutdown()", "", sLog);
                        if (sTempResults != "done")
                        {
                            Console.WriteLine(DateTime.Now.ToString("yyMMdd-HH:mm") + " Warning: Problems while executing script 'unplannedshutdown()', Error msg = {0}", sTempResults);
                        }
                    }
                }

                lock (Console.Out)
                {
                    // launch startup script
                    string sLog = "";
                    foreach (string sTmp in MemoryLog)
                    {
                        sLog += sTmp + "\n";
                    }
                    string sTempResults = QueueWorkerClass.MXRunScript("startup()", "", sLog);
                    if (sTempResults != "done")
                    {
                        Console.WriteLine(DateTime.Now.ToString("yyMMdd-HH:mm") + " Warning: Problems while executing script 'startup()', Error msg = {0}", sTempResults);
                    }
                    MemoryLog.Clear();
                }

                for (; ; )
                {
                    string sCmd = Console.ReadLine();
                    switch (sCmd)
                    {
                        case "h":
                        case "help":
                        case "?":
                            lock (Console.Error)
                            {
                                Console.Error.WriteLine(
                                    "\n" +
                                    "  Commands:\n" +
                                    "   h/help/?    display this help\n" +
                                    "   q/quit      quit this program (use ctrl+c to force quit)\n" +
                                    "   l/list      list wathced folder(s)\n" +
                                    "   s/status    show status\n" +
                                    "   c/clear     clear the console\n" +
                                    "   r/restart   restart the application\n"
                                    );
                            }
                            break;
                        case "quit":
                            bQuit = true;
                            break;
                    }
                    if (sCmd == "r" || sCmd == "restart")
                    {
                        bRestart = true;
                        break;
                    }
                    if (sCmd == "s" || sCmd == "status")
                    {
                        lock (cApp.queue.aQueueFileName)
                        {
                            Console.Error.WriteLine("==================================");
                            for (int i = 0; i < cApp.queue.aQueueFileName.Count; i++)
                            {
                                try
                                {
                                    DateTime dFrom, dTo;
                                    TimeSpan tSpan;
                                    dFrom = DateTime.Now;
                                    dTo = (DateTime)cApp.queue.aQueueTimeStamp[i];
                                    tSpan = dFrom.Subtract(dTo);
                                    //cApp.queue.aQueueElapsed[i] = (double)tSpan.TotalSeconds;

                                    Console.Error.WriteLine("#{0} {1} {2} {3}", i, cApp.queue.aQueueStatus[i], tSpan.TotalSeconds.ToString(), cApp.queue.aQueueFileName[i]);
                                }
                                catch
                                {
                                    Console.Error.WriteLine("#{0} - error processing", i);
                                }
                            }
                            Console.Error.WriteLine("----------------------------------");

                        }
                    }
                    if (sCmd == "c" || sCmd == "clear")
                        Console.Clear();
                    if (sCmd == "q") break;
                    if (bQuit) break;
                    Console.Error.Write("{0}                                                                              {1}", '\x0d', '\x0d');
                    Console.Error.Write("{0}{1} > ", '\x0d', DateTime.Now.ToString("yyMMdd-HH:mm"));
                }

                AppClass.bRunning = false;

                // wait for Monitor thread to stop
                for (; ; )
                {
                    if (thMonitor.ThreadState == System.Threading.ThreadState.Stopped)
                        break;
                    Thread.Sleep(50);
                }

                // wait for Queue thread to stop
                for (; ; )
                {
                    if (thQueue.ThreadState == System.Threading.ThreadState.Stopped)
                        break;
                    Thread.Sleep(50);
                }

                // wait for Worker thread(s) to stop
                // ToDo:

                // launch shutdown script
                lock (Console.Out)
                {
                    string sLog = "";
                    foreach (string sTmp in MemoryLog)
                    {
                        sLog += sTmp + "\n";
                    }
                    string sTempResults = QueueWorkerClass.MXRunScript("shutdown()", "", sLog);
                    if (sTempResults != "done")
                    {
                        Console.WriteLine(DateTime.Now.ToString("yyMMdd-HH:mm") + " Warning: Problems while executing script 'shutdown()', Error msg = {0}", sTempResults);
                    }
                }

                App.EndBanner();

                if (!App.MXUnLockPid())
                {
                    // Hmm.. Can't unlock pid, but who was the initial locker then? :)
                    Console.WriteLine("Warning: Can't unlock pid!");
                }

            }

            if (bRestart)
            {
                return (-1);
            }
            else
            {
                return (0);
            }
        }
示例#2
0
 public Monitor(ref App x, ref QueueWorker y)
 {
     cApp = x;
     cQueueWorkerClass = y;
 }