Ejemplo n.º 1
0
        /// <summary>
        /// Called when the service is being started by the SCM.
        /// </summary>
        /// <param name="args">The arguments.</param>
        protected override void OnStart(string[] args)
        {
////#if DEBUG
////            System.Threading.Thread.Sleep(10000);
////#endif

            LogInfo("Starting service...");
            try
            {
                var options = TaskManagerOptions.Create("Start parameters: ", args);
                Initialize(options.EventLog);
                TaskSupervisor.Initialize(options.StatsStrategy);
                ModuleSupervisor.Initialize();

                LogInfo("Service successfully started...");

                ModuleSupervisor.Execute();
            }
            catch (Exception e)
            {
                LogError("Unable to start service.", e);

                File.WriteAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TaskManager.start.error.txt"), e.Message);
                throw;
            }
        }
Ejemplo n.º 2
0
 private static void WaitUserInteraction(TaskManagerOptions options, string message = null)
 {
     if (!options.NonStop)
     {
         Show(String.IsNullOrEmpty(message) ? "Press ENTER to continue" : message);
         Console.ReadLine();
     }
 }
Ejemplo n.º 3
0
		/// <summary>
		/// Create a TaskManageOptions from arguments.
		/// </summary>
		/// <param name="usagePrefix">Usage prefix.</param>
		/// <param name="args">The arguments.</param>
        public static TaskManagerOptions Create(string usagePrefix, string[] args)
        {
            var options = new TaskManagerOptions();
            var optionsSet = BuildOptions(usagePrefix, options);
            options.ParseArguments(optionsSet, args);

            if(!options.ShowHelp)
            {
                // Set the defaults.
                options.EventLog = options.EventLog ?? new WindowsEventLog();
                options.StatsStrategy = options.StatsStrategy ?? new PerformanceCounterStatsStrategy();
            }

            return options;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Create a TaskManageOptions from arguments.
        /// </summary>
        /// <param name="usagePrefix">Usage prefix.</param>
        /// <param name="args">The arguments.</param>
        public static TaskManagerOptions Create(string usagePrefix, string[] args)
        {
            var options    = new TaskManagerOptions();
            var optionsSet = BuildOptions(usagePrefix, options);

            options.ParseArguments(optionsSet, args);

            if (!options.ShowHelp)
            {
                // Set the defaults.
                options.EventLog      = options.EventLog ?? new WindowsEventLog();
                options.StatsStrategy = options.StatsStrategy ?? new PerformanceCounterStatsStrategy();
            }

            return(options);
        }
Ejemplo n.º 5
0
        private static OptionSet BuildOptions(string usagePrefix, TaskManagerOptions options)
        {
            return new OptionSet()
            {
                "Usage: ",
                String.Format(CultureInfo.InvariantCulture, "   {0} -e <event log> -s <stats>", usagePrefix),
                string.Empty,
                "Options:",
                {
                    "e|event-log=",
                    "the event log. Available values are: Console and Windows. Default is: Windows.",
                    e => options.EventLog = ArgumentsHelper.CreateEventLog(e)
                },
                {
                    "s|stats=",
                    "the stats strategy. Available values are: Memory and PerformanceCounter. Default is: PerformanceCounter.",
                     s => options.StatsStrategy = ArgumentsHelper.CreateStatsStrategy(s)
                },
                {
                    "h|help", "show this message and exit", h => options.ShowHelp = h != null
                },
                // The arguments below are used to functional tests purpose only.
                {
                    "non-stop", "if should wait for user interaction", n => options.NonStop = n != null, true
                },
                {
                    "non-stop-wait=", "the time in milliseconds to wait to tasks run when in non-stop mode", n => options.NonStopWait = Convert.ToInt32(n, CultureInfo.InvariantCulture), true
                },

                string.Empty,
                string.Empty,
                "Samples:",
                String.Format(CultureInfo.InvariantCulture, "{0} -e Windows -s PerformanceCounter", usagePrefix),                
                string.Empty,
                String.Format(CultureInfo.InvariantCulture, "{0} -e Console", usagePrefix),
                string.Empty,
                String.Format(CultureInfo.InvariantCulture, "{0} -s Memory", usagePrefix),
            };
        }
Ejemplo n.º 6
0
        private static OptionSet BuildOptions(string usagePrefix, TaskManagerOptions options)
        {
            return(new OptionSet()
            {
                "Usage: ",
                String.Format(CultureInfo.InvariantCulture, "   {0} -e <event log> -s <stats>", usagePrefix),
                string.Empty,
                "Options:",
                {
                    "e|event-log=",
                    "the event log. Available values are: Console and Windows. Default is: Windows.",
                    e => options.EventLog = ArgumentsHelper.CreateEventLog(e)
                },
                {
                    "s|stats=",
                    "the stats strategy. Available values are: Memory and PerformanceCounter. Default is: PerformanceCounter.",
                    s => options.StatsStrategy = ArgumentsHelper.CreateStatsStrategy(s)
                },
                {
                    "h|help", "show this message and exit", h => options.ShowHelp = h != null
                },
                // The arguments below are used to functional tests purpose only.
                {
                    "non-stop", "if should wait for user interaction", n => options.NonStop = n != null, true
                },
                {
                    "non-stop-wait=", "the time in milliseconds to wait to tasks run when in non-stop mode", n => options.NonStopWait = Convert.ToInt32(n, CultureInfo.InvariantCulture), true
                },

                string.Empty,
                string.Empty,
                "Samples:",
                String.Format(CultureInfo.InvariantCulture, "{0} -e Windows -s PerformanceCounter", usagePrefix),
                string.Empty,
                String.Format(CultureInfo.InvariantCulture, "{0} -e Console", usagePrefix),
                string.Empty,
                String.Format(CultureInfo.InvariantCulture, "{0} -s Memory", usagePrefix),
            });
        }
Ejemplo n.º 7
0
        private static void RunFromConsole(string[] args)
        {
            ShowHeader();
            TaskManagerOptions options;

            try
            {
                options = TaskManagerOptions.Create("TaskManager.exe", args);
            }
            catch (Exception ex)
            {
                Show("Argument parsing error: ");
                Show(ex.Message);
                Show("Try `TaskManager --help` for more information");
                Environment.Exit(3);
                return;
            }

            if (options.ShowHelp)
            {
                Show(options.HelpText);
                return;
            }

            WaitUserInteraction(options, "Press ENTER to locate task modules.");
            TaskManagerService.LogInfo("Initializing service...");

            try
            {
                Show("Event log: {0}", options.EventLog.GetType().Name);
                Show("Stats strategy: {0}", options.StatsStrategy.GetType().Name);

                TaskSupervisor.Initialize(options.StatsStrategy);
                TaskManagerService.Initialize(options.EventLog);
                ModuleSupervisor.Initialize();
            }
            catch (Exception e)
            {
                TaskManagerService.LogError("Unable to initialize service.", e);
                WaitUserInteraction(options);
                return;
            }

            WaitUserInteraction(options, "Press ENTER to start.");

            try
            {
                TaskManagerService.LogInfo("Service successfully started...");
                ModuleSupervisor.Execute();

                if (options.NonStop && options.NonStopWait > 0)
                {
                    Show("Waiting {0} milliseconds to tasks execution...", options.NonStopWait);
                    Thread.Sleep(options.NonStopWait);
                }
            }
            catch (Exception e)
            {
                TaskManagerService.LogError("Unable to start service.", e);
                WaitUserInteraction(options);

                return;
            }

            // If you are debugging, you can freeze the main thread here.
            WaitUserInteraction(options, "Press ENTER to stop.");

            TaskManagerService.LogInfo("Stopping service...");
            try
            {
                ModuleSupervisor.Shutdown();
                TaskSupervisor.Shutdown();
                TaskManagerService.LogInfo("Service successfully stopped...");
            }
            catch (Exception e)
            {
                TaskManagerService.LogError("Unable to stop service.", e);
                return;
            }

            WaitUserInteraction(options, "Press ENTER to finish.");
        }
Ejemplo n.º 8
0
 private static void WaitUserInteraction(TaskManagerOptions options, string message = null)
 {
     if (!options.NonStop)
     {
         Show(String.IsNullOrEmpty(message) ? "Press ENTER to continue" : message);
         Console.ReadLine();
     }
 }