Ejemplo n.º 1
0
        private static void Cleanup()
        {
            try
            {
                if (client != null)
                {
                    client.Dispose();
                    client = null;
                }
            }
            catch (Exception ex)
            {
                log.ErrorException("Unable to cleanup client", ex);
            }

            try
            {
                if (serviceManager != null)
                {
                    serviceManager.Dispose();
                    serviceManager = null;
                }
            }
            catch (Exception ex)
            {
                log.ErrorException("Unable to cleanup service", ex);
            }
        }
Ejemplo n.º 2
0
        static void Run()
        {
            try
            {
                // connect the client (note: for multiple services, we may need to config bindings in code on both ends)
                using (var client = new JobObjectClient("IJobObjectService"))
                {
                    client.Register();
                    client.ProcessExited += (sender, eventArgs) =>
                    {
                        Console.WriteLine("Exit Code: {0}", eventArgs.Value);
                        Environment.Exit(0); // makes sure we exit so service can clean up (assuming a single process)
                    };

                    ListCommands();
                    ListProcesses(client);

                    var prompt = GetPrompt();
                    while (!IsPrompt(prompt, "quit", "q", "exit"))
                    {
                        var arg = GetSecondArg(prompt);

                        if (HasPrompt(prompt, "kill", "k"))
                        {
                            int pid;
                            if (Int32.TryParse(arg, out pid))
                            {
                                client.StopProcess(pid);
                            }
                        }
                        else if (HasPrompt(prompt, "run", "r"))
                        {
                            var runOptions = GetOptions(arg);
                            client.StartProcess(runOptions.Command, runOptions.WorkingDirectory, runOptions.Arguments);
                        }
                        else if (HasPrompt(prompt, "list", "l"))
                        {
                            ListProcesses(client);
                        }
                        else if (HasPrompt(prompt, "help", "h"))
                        {
                            ListCommands();
                        }

                        prompt = GetPrompt();
                    }
                }
            }
            catch (Exception ex)
            {
                log.ErrorException("Error in run loop", ex);
            }
        }
Ejemplo n.º 3
0
        private static void Run(Options options)
        {
            var processDir = Path.Combine(Environment.CurrentDirectory, "ProcessHost"); // \process-exec\HostDriver\bin\debug\ProcessHost

            try
            {
                var processCred = new NetworkCredential(options.UserName, options.Password);
                serviceManager = new ProcessHostManager(processDir, options.ContainerDir, "processhost.exe", options.ServiceName, processCred);
                serviceManager.RunService();

                // connect the client (note: for multiple services, we may need to config bindings in code on both ends)
                client = new JobObjectClient("IJobObjectService");
                client.ServiceMessageReceived += (sender, eventArgs) => log.Info(eventArgs.Value);
                client.OutputReceived += (sender, eventArgs) => log.Info(eventArgs.Value);
                client.ErrorReceived += (sender, eventArgs) => log.Error(eventArgs.Value);
                client.ProcessExited += (sender, eventargs) => log.Info("Process Exit: {0}", eventargs.Value);

                client.Register();
                client.SetJobLimits(new JobObjectLimits
                {
                    MemoryMB = options.MemoryLimit,
                    CpuPercent = options.CpuRateLimit
                });

                client.StartProcess(options.Command, options.WorkingDirectory, options.Arguments);

                //client.StartProcess(@"c:\tmp\eatcpu.exe", @"C:\tmp", string.Empty);
                //client.StartProcess(@"c:\tmp\eatmemory.exe", @"C:\tmp", "512 10"); // 512 mb, 10 seconds wait for exit
                //client.StartProcess("cmd.exe", @"C:\tmp", "/c dir *.*");
                //client.StartProcess("cmd.exe", String.Empty, "/c ping 127.0.0.1 -n 200 -w 1000");
                //client.StartProcess("cmd.exe", String.Empty, "/c ping 127.0.0.1 -n 25 -w 500");

                exitLatch.WaitOne(); // wait for exit
                Console.WriteLine("Press [enter] to terminate and cleanup"); // reminder
            }
            catch (Exception ex)
            {
                log.ErrorException("Error in run loop", ex);
            }
            finally
            {
                Cleanup();
            }
        }