Пример #1
0
        private static void KillGlobalServerProcess()
        {
            var p = _globalServerProcess;

            _globalServerProcess = null;
            if (p != null && p.HasExited == false)
            {
                ReportInfo($"Kill global server PID { p.Id }.");

                try
                {
                    p.Kill();
                }
                catch (Exception e)
                {
                    ReportError(e);
                }
            }

            RavenServerRunner <TServerLocator> .CleanupTempFiles();
        }
Пример #2
0
        private static IDocumentStore RunServer()
        {
            var process = _globalServerProcess = RavenServerRunner <TServerLocator> .Run(new TServerLocator());

            ReportInfo($"Starting global server: { _globalServerProcess.Id }");

#if NETSTANDARD1_3
            AppDomain.CurrentDomain.ProcessExit += (s, args) =>
            {
                KillGlobalServerProcess();
            };
#endif

#if NETSTANDARD1_5
            AssemblyLoadContext.Default.Unloading += c =>
            {
                KillGlobalServerProcess();
            };
#endif

            string url    = null;
            var    output = process.StandardOutput;
            var    sb     = new StringBuilder();

            var startupDuration = Stopwatch.StartNew();

            Task <string> readLineTask = null;
            while (true)
            {
                if (readLineTask == null)
                {
                    readLineTask = output.ReadLineAsync();
                }

                var task = Task.WhenAny(readLineTask, Task.Delay(TimeSpan.FromSeconds(5))).Result;

                if (startupDuration.Elapsed > TimeSpan.FromMinutes(1))
                {
                    break;
                }

                if (task != readLineTask)
                {
                    continue;
                }

                var line = readLineTask.Result;

                readLineTask = null;

                sb.AppendLine(line);

                if (line == null)
                {
                    try
                    {
                        process.Kill();
                    }
                    catch (Exception e)
                    {
                        ReportError(e);
                    }

                    throw new InvalidOperationException("Unable to start server, log is: " + Environment.NewLine + sb);
                }
                const string prefix = "Server available on: ";
                if (line.StartsWith(prefix))
                {
                    url = line.Substring(prefix.Length);
                    break;
                }
            }

            if (url == null)
            {
                var log = sb.ToString();
                ReportInfo(log);
                try
                {
                    process.Kill();
                }
                catch (Exception e)
                {
                    ReportError(e);
                }

                throw new InvalidOperationException("Unable to start server, log is: " + Environment.NewLine + log);
            }

            output.ReadToEndAsync()
            .ContinueWith(x =>
            {
                ReportError(x.Exception);
                GC.KeepAlive(x.Exception);
            });     // just discard any other output

            var store = new DocumentStore
            {
                Urls     = new[] { url },
                Database = "test.manager"
            };

            return(store.Initialize());
        }