Ejemplo n.º 1
0
        public ActionResult StartOrStopServices(
            string[] serviceNames,
            string[] hostNames,
            bool shouldStartNotStop, bool shouldUpdate)
        {
            SharpCodeContract.Requires <ArgumentNullException>(serviceNames != null, "serviceNames = null");
            SharpCodeContract.Requires <ArgumentNullException>(hostNames != null, "hostNames = null");
            SharpCodeContract.Requires <ArgumentOutOfRangeException>(serviceNames.Length == hostNames.Length);

            var errorStrings = new List <string>();

            for (var i = 0; i < serviceNames.Length; i++)
            {
                var srv      = serviceNames[i];
                var hostName = hostNames[i];

                string errorString;
                var    isOk = shouldUpdate
                                ? unitStatusRepository.UpdateService(hostName, srv, out errorString)
                                : shouldStartNotStop
                                        ? unitStatusRepository.StartService(hostName, srv, out errorString)
                                        : unitStatusRepository.StopService(hostName, srv, out errorString);

                if (!isOk)
                {
                    errorStrings.Add(errorString);
                }
            }

            return(Json(new
            {
                isOk = errorStrings.Count == 0,
                errorString = string.Join("\n", errorStrings)
            }, JsonRequestBehavior.AllowGet));
        }
Ejemplo n.º 2
0
        private bool StartStopOrUpdateService(string hostName, string serviceName,
                                              bool shouldUpdate, bool shouldStart, bool shouldStop,
                                              out string errorString)
        {
            SharpCodeContract.Requires <ArgumentNullException>(!string.IsNullOrEmpty(hostName));
            SharpCodeContract.Requires <ArgumentNullException>(!string.IsNullOrEmpty(serviceName));
            SharpCodeContract.Requires <ArgumentException>(shouldUpdate || shouldStart || shouldStop);

            errorString = string.Empty;
            try
            {
                var client = CreateWcfClient(hostName);
                if (shouldStart)
                {
                    var status = client.TryStartService(serviceName);
                    if (status != StartProcessStatus.OK)
                    {
                        errorString = status.ToString();
                        return(false);
                    }
                    return(true);
                }
                if (shouldStop)
                {
                    var status = client.TryStopService(serviceName);
                    if (status != KillProcessStatus.OK)
                    {
                        errorString = status.ToString();
                        return(false);
                    }
                    return(true);
                }

                // shouldUpdate == true
                client.TryUpdateServices(new List <string> {
                    serviceName
                });
                return(true);
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("Ошибка StartStopOrUpdateService({0}: {1}) - {2}",
                                   hostName, serviceName, ex);
                errorString = ex.Message;
                return(false);
            }
        }