Exemplo n.º 1
0
        /// <summary>
        /// Restart server if freeze/slowness detected
        /// </summary>
        private void RestartServer(ServerTimer timer)
        {
            if (IsStopping)
            {
                return;
            }

            IsStopping = true;
            if (timer == null || timer.Server == null)
            {
                return;
            }

            Log.WriteInfo("Stopping frozen server {0}...", timer.Server.Name);

            var id = timer.With(x => x.Server).With(x => x.Process).Return(x => x.Id, 0);

            if (id == 0)
            {
                Log.WriteInfo("Unable to restart server {0}. Unknown process id.", timer.Server.Name);
                return;
            }

            Util.Try(() =>
            {
                var process = Process.GetProcessById(id);

                if (process == null)
                {
                    Log.WriteInfo("Unable to restart server {0}. Process not found.", timer.Server.Name);
                    return;
                }

                if (String.IsNullOrWhiteSpace(Server.Process.CommandLine))
                {
                    Log.WriteInfo("Unable to restart a server {0}. Empty command line!", timer.Server.Name);
                    return;
                }

                process.Kill();
            }, false);

            Util.Try(() =>
            {
                Thread.Sleep(1000);
                Log.WriteInfo("Starting server {0}...", timer.Server.Name);
                var exeFile   = Re.GetSubString(Server.Process.CommandLine, @"^(.*exe).*$");
                var arguments = Re.GetSubString(Server.Process.CommandLine, @"^.*?\s(.*)$");
                var directory = Server.Process.Path;
                StartProcess(exeFile, arguments, directory);
            });
        }