Exemple #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);
            }
        }
Exemple #2
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();
            }
        }