Beispiel #1
0
        public static int Main(string[] args)
        {
            Console.CancelKeyPress += new ConsoleCancelEventHandler(ConsoleCancelKeyPress);

            options = InputOptions.Create(args);
            logger = new ConsoleLogger(options);
            isRunning = true;

            if (options.IsValid)
            {
                if (!options.Help)
                {
                    if (options.ParentProcessId > 0)
                    {
                        Process parentProcess = Process.GetProcessById(options.ParentProcessId);

                        if (parentProcess != null)
                        {
                            logger.Debug("Parent process was found with ID {0}. Subscribing to 'Exited' event.", options.ParentProcessId);
                            parentProcess.Exited += new EventHandler(ParentProcessExited);
                            parentProcess.EnableRaisingEvents = true;
                        }
                        else
                        {
                            logger.Debug("No parent process was found with ID {0}.", options.ParentProcessId);
                        }
                    }

                    Console.WriteLine();
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.WriteLine("Type 'exit' to shutdown gracefully, or Ctl+C to exit immediately.");
                    Console.ResetColor();
                    Console.WriteLine();

                    inputThread = new Thread(WaitForInput);
                    inputThread.Start();

                    PullupBootstraps();
                }
                else
                {
                    logger.Help();
                }
            }
            else
            {
                logger.InputError();
                return 1;
            }

            return 0;
        }
        /// <summary>
        /// Creates a new <see cref="InputOptions"/> instance by parsing the given collection of arguments.
        /// </summary>
        /// <param name="args">The collection of arguments to parse.</param>
        /// <returns>The created <see cref="InputOptions"/>.</returns>
        public static InputOptions Create(IEnumerable<string> args)
        {
            string app = null, config = null, thresh = null, pid = null;
            bool verbose = false, help = false;

            OptionSet options = new OptionSet()
            {
                { "app=", "(required) the path to the directory of the application to run jobs for.", v => app = v },
                { "config=", "the path to the configuration file to use, if not the default for the application.", v => config = v },
                { "v|verbose", "write logging information to standard out.", v => verbose = v != null },
                { "thresh=", "the threshold, in milliseconds, to compress file system events into.", v => thresh = v },
                { "h|?|help", "display usage help.", v => help = v != null }
            };

            InputOptions result = new InputOptions();
            result.OptionSet = options;

            try
            {
                options.Parse(args);
                result.ApplicationPath = PathQuotesExp.Replace(app ?? string.Empty, "$1");
                result.ConfigPath = PathQuotesExp.Replace(config ?? string.Empty, "$1");
                result.IsValid = true;
            }
            catch (OptionException ex)
            {
                result.IsValid = false;
                result.ParseException = ex;
            }

            if (result.IsValid)
            {
                result.Help = help;
                result.Verbose = verbose;

                if (!result.Help)
                {
                    if (string.IsNullOrEmpty(result.ApplicationPath))
                    {
                        result.ParseErrorMessage = "Application path is required.";
                        result.IsValid = false;
                    }

                    pid = Environment.GetEnvironmentVariable("COLLARSERVICEPID");

                    if (!string.IsNullOrEmpty(pid))
                    {
                        try
                        {
                            result.ParentProcessId = Convert.ToInt32(pid, CultureInfo.InvariantCulture);
                        }
                        catch (FormatException)
                        {
                        }
                        catch (OverflowException)
                        {
                        }
                    }

                    if (!string.IsNullOrEmpty(thresh))
                    {
                        const string ThresholdErrorMessage = "Threshold must be an integer value greater than or equal to 500.";

                        try
                        {
                            result.Threshold = Convert.ToInt32(thresh, CultureInfo.InvariantCulture);

                            if (result.Threshold < 500)
                            {
                                result.IsValid = false;
                                result.ParseErrorMessage = ThresholdErrorMessage;
                            }
                        }
                        catch (FormatException)
                        {
                            result.IsValid = false;
                            result.ParseErrorMessage = ThresholdErrorMessage;
                        }
                        catch (OverflowException)
                        {
                            result.IsValid = false;
                            result.ParseErrorMessage = ThresholdErrorMessage;
                        }
                    }
                }
            }

            return result;
        }
Beispiel #3
0
        /// <summary>
        /// Creates a new <see cref="InputOptions"/> instance by parsing the given collection of arguments.
        /// </summary>
        /// <param name="args">The collection of arguments to parse.</param>
        /// <returns>The created <see cref="InputOptions"/>.</returns>
        public static InputOptions Create(IEnumerable <string> args)
        {
            string app = null, config = null, thresh = null, pid = null;
            bool   verbose = false, help = false;

            OptionSet options = new OptionSet()
            {
                { "app=", "(required) the path to the directory of the application to run jobs for.", v => app = v },
                { "config=", "the path to the configuration file to use, if not the default for the application.", v => config = v },
                { "v|verbose", "write logging information to standard out.", v => verbose = v != null },
                { "thresh=", "the threshold, in milliseconds, to compress file system events into.", v => thresh = v },
                { "h|?|help", "display usage help.", v => help = v != null }
            };

            InputOptions result = new InputOptions();

            result.OptionSet = options;

            try
            {
                options.Parse(args);
                result.ApplicationPath = PathQuotesExp.Replace(app ?? string.Empty, "$1");
                result.ConfigPath      = PathQuotesExp.Replace(config ?? string.Empty, "$1");
                result.IsValid         = true;
            }
            catch (OptionException ex)
            {
                result.IsValid        = false;
                result.ParseException = ex;
            }

            if (result.IsValid)
            {
                result.Help    = help;
                result.Verbose = verbose;

                if (!result.Help)
                {
                    if (string.IsNullOrEmpty(result.ApplicationPath))
                    {
                        result.ParseErrorMessage = "Application path is required.";
                        result.IsValid           = false;
                    }

                    pid = Environment.GetEnvironmentVariable("COLLARSERVICEPID");

                    if (!string.IsNullOrEmpty(pid))
                    {
                        try
                        {
                            result.ParentProcessId = Convert.ToInt32(pid, CultureInfo.InvariantCulture);
                        }
                        catch (FormatException)
                        {
                        }
                        catch (OverflowException)
                        {
                        }
                    }

                    if (!string.IsNullOrEmpty(thresh))
                    {
                        const string ThresholdErrorMessage = "Threshold must be an integer value greater than or equal to 500.";

                        try
                        {
                            result.Threshold = Convert.ToInt32(thresh, CultureInfo.InvariantCulture);

                            if (result.Threshold < 500)
                            {
                                result.IsValid           = false;
                                result.ParseErrorMessage = ThresholdErrorMessage;
                            }
                        }
                        catch (FormatException)
                        {
                            result.IsValid           = false;
                            result.ParseErrorMessage = ThresholdErrorMessage;
                        }
                        catch (OverflowException)
                        {
                            result.IsValid           = false;
                            result.ParseErrorMessage = ThresholdErrorMessage;
                        }
                    }
                }
            }

            return(result);
        }
 /// <summary>
 /// Initializes a new instance of the ConsoleLogger class.
 /// </summary>
 /// <param name="options">The parsed <see cref="InputOptions"/> for the current execution context.</param>
 public ConsoleLogger(InputOptions options)
 {
     this.options = options;
 }
Beispiel #5
0
 /// <summary>
 /// Initializes a new instance of the ConsoleLogger class.
 /// </summary>
 /// <param name="options">The parsed <see cref="InputOptions"/> for the current execution context.</param>
 public ConsoleLogger(InputOptions options)
 {
     this.options = options;
 }