Esempio n. 1
0
        public void RestartServices(string[] services, TimeSpan timeoutPerService)
        {
            // Stop them one by one (Must one by one because they may have dependencies)
            CmdHelper cmd = new CmdHelper();

            for (int i = 0; i < services.Length; i++)
            {
                Log.Info("Restart services with {0}\\{1}...", this.Domain, this.userName);
                CommandExecutionResult result = cmd.ExecuteCommand2(
                    @"cmd /c sc \\{0} stop {1}".FormatWith(this.machine, services[i])
                    //, this.userName
                    //, this.password
                    //, this.Domain
                    );
                if (result.ExitCode == 0)
                {
                    DateTime start  = DateTime.Now;
                    string   status = this.GetServiceStatus(services[i]);
                    while (!status.Equals("Stopped", StringComparison.OrdinalIgnoreCase))
                    {
                        if (DateTime.Now.Subtract(start) < timeoutPerService)
                        {
                            Log.Info("Waiting...");
                            System.Threading.Thread.Sleep(2000);
                        }
                        else
                        {
                            throw new Exception("Timeout after {0} when stopping service {1} on machine {2}. Latest status: {3}.".FormatWith(timeoutPerService.ToString(), services[i], this.machine, status));
                        }

                        status = this.GetServiceStatus(services[i]);
                    }
                }
                else
                {
                    Log.Info(result.StandardOutput.ToString());
                    Log.Error(result.StandardError.ToString());

                    DateTime start  = DateTime.Now;
                    string   status = this.GetServiceStatus(services[i]);
                    while (!status.Equals("Stopped", StringComparison.OrdinalIgnoreCase))
                    {
                        if (DateTime.Now.Subtract(start) < timeoutPerService)
                        {
                            Log.Info("Waiting...");
                            System.Threading.Thread.Sleep(2000);
                        }
                        else
                        {
                            throw new Exception("Timeout after {0} when stopping service {1} on machine {2}. Latest status is {3}.".FormatWith(timeoutPerService.ToString(), services[i], this.machine, status));
                        }

                        status = this.GetServiceStatus(services[i]);
                    }

                    //throw new Exception("Failed to stop service {0} on machine {1}.\r\n{2}".FormatWith(services[i], this.machine, result.ToSerializedXmlString()));
                }
            }

            // Start them one by one
            for (int i = services.Length - 1; i >= 0; i--)
            {
                CommandExecutionResult result = cmd.ExecuteCommand2(
                    @"cmd /c sc \\{0} start {1}".FormatWith(this.machine, services[i])
                    //, this.userName
                    //, this.password
                    //, this.Domain
                    );
                if (result.ExitCode == 0)
                {
                    DateTime start  = DateTime.Now;
                    string   status = this.GetServiceStatus(services[i]);
                    while (!status.Equals("running", StringComparison.OrdinalIgnoreCase))
                    {
                        if (DateTime.Now.Subtract(start) < timeoutPerService)
                        {
                            Log.Info("Waiting...");
                            System.Threading.Thread.Sleep(2000);
                        }
                        else
                        {
                            throw new Exception("Timeout after {0} when starting service {1} on machine {2}. Latest status: {3}.".FormatWith(timeoutPerService.ToString(), services[i], this.machine, status));
                        }

                        status = this.GetServiceStatus(services[i]);
                    }
                }
                else
                {
                    Log.Info(result.StandardOutput.ToString());
                    Log.Error(result.StandardError.ToString());

                    DateTime start  = DateTime.Now;
                    string   status = this.GetServiceStatus(services[i]);
                    while (!status.Equals("running", StringComparison.OrdinalIgnoreCase))
                    {
                        if (DateTime.Now.Subtract(start) < timeoutPerService)
                        {
                            Log.Info("Waiting...");
                            System.Threading.Thread.Sleep(2000);
                        }
                        else
                        {
                            throw new Exception("Timeout after {0} when starting service {1} on machine {2}. Latest status: {3}.".FormatWith(timeoutPerService.ToString(), services[i], this.machine, status));
                        }

                        status = this.GetServiceStatus(services[i]);
                    }

                    //throw new Exception("Failed to start service {0} on machine {1}.\r\n{2}".FormatWith(services[i], this.machine, result.ToSerializedXmlString()));
                }
            }
        }