Ejemplo n.º 1
0
        // Define the event handlers.
        private void OnChanged(object source, FileSystemEventArgs e)
        {
            if (!active)
            {
                return;
            }

            bwq.enque(this, e, (o1, e1) => {
                const int NUM_RETRIES  = 5;
                const int WAIT_TIME_MS = 500;
                for (int i = 0; i < NUM_RETRIES; i++)
                {
                    try
                    {
                        if (i > 0)
                        {
                            //MyConsole.WriteLine("\tWaiting before retrying...");
                            Thread.Sleep(WAIT_TIME_MS);
                        }
                        processLogFile(e.FullPath);
                        break;
                    }
                    catch (IOException ex)
                    {
                        Trace.WriteLine(MODULE + "Logged exception " + ex);
                        MyConsole.WriteError(String.Format("\tCould not access [{0}]. Retrying...", Path.GetFileName(e.FullPath)));
                    }
                }
            });
            Trace.WriteLine(MODULE + String.Format("Received OnChanged ([{0}], {1})", e.FullPath, e.ChangeType));
        }
Ejemplo n.º 2
0
 private static void myCtrlCHandler(ConsoleCancelEventArgs ea, BlockingWorkQueue workQueue)
 {
     Trace.WriteLine(MODULE + "In CTRL-C handler.");
     ea.Cancel = true;
     workQueue.enque(null, null, (ox, ex) => {
         MyConsole.WriteWarning("CTRL-C received. Ok to quit (y/n)?");
         String s = "" + (char)Console.Read();
         if (s.ToUpperInvariant().IndexOf('Y') == 0)
         {
             workQueue.stopWaiting();
         }
     });
 }
Ejemplo n.º 3
0
        const String MODULE  = "MAIN: "; // for debug log.
        static void Main(string[] args)
        {
            string baseWorkingDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
                                    + "\\PuzzleOracle\\consolidated";

            MyConsole.Initialize();
            MyConsole.WriteLine("LOG PROCESSOR Version " + VERSION);
            MyConsole.WriteLine(String.Format("Working directory [{0}]", baseWorkingDir));
            MyConsole.WriteLine("Press CTRL-C to quit");

            try
            {
                if (!initializeLogging(baseWorkingDir))
                {
                    // Can't progress...
                    throw new ApplicationException("Cannot initialize logging.");
                }

                // Initialie the blocking work queue - it runs all significant processing operations in sequence on the main thread.
                BlockingWorkQueue workQueue = new BlockingWorkQueue();
                //workQueue.enque(null, null, (o, ea) => { Console.WriteLine("TEST WORK ITEM -A-"); });
                //workQueue.enque(null, null, (o, ea) => { Console.WriteLine("TEST WORK ITEM -B-"); });

                // Register the CTRL-C handler - this lets the user quit the program and potentially enter other commands.
                // TODO: Currently the handler simply causes the work queue to exit after the latter processes current work items. Perhaps add some
                // command processing and request confirmation of this behavior.
                Console.CancelKeyPress += new ConsoleCancelEventHandler((o, ea) => { myCtrlCHandler(ea, workQueue); });

                // Create the file copier - responsible for copying files from thumb drives to a staging directory, verifying the file copy and then moving
                // the source files into an arcive subdir on the thumb drive, and (finally) moving the newly copied files under the "new" directory.
                FileCopier fileCopier = new FileCopier(baseWorkingDir);

                // Create a new-drive notifier and hook it up to the file copier - so that the latter will get notified whenever there is a new removable drive
                // plugged in.
                //NewDriveNotifier ndn = new NewDriveNotifier((o, e) => { bwq.enque(o, e, (o1, ea1) => { Console.WriteLine("WORK ITEM -NEW DRIVE-" + ((NewDriveNotifierEventArgs)ea1).driveName); }); });
                using (NewDriveNotifier driveNotifier = new NewDriveNotifier((o, e) => { workQueue.enque(o, e, fileCopier.newDriveHandler); }))
                {
                    // Create a log consumer - this processes submission requests pulled from individual puzzle oracle log files.
                    LogConsumer logConsumer = new LogConsumer(baseWorkingDir);

                    // Create the log processor - that processes new log files as they show up in the "new" directory. Hook it up to the log consumer so that the latter
                    // processes the submissions requests.
                    String newLogsDir = fileCopier.newDir;
                    using (LogProcessor lp = new LogProcessor(newLogsDir, (o, e) => { workQueue.enque(o, e, logConsumer.logEventHandler); }, workQueue))
                    {
                        lp.startListening();            // start listening for new files in the "new directory"
                        driveNotifier.startListening(); // start listening for new thumb drives
                        workQueue.process();
                    }
                }
            }
            catch (ApplicationException e)
            {
                Trace.TraceError("Caught application exception: " + e);
                MyConsole.WriteError("THE LOG PROCESSOR MUST EXIT.\nPress ENTER to quit.");
                Trace.Flush();
                Console.ReadLine();
            }
        }