protected override Task <int> ExecuteCoreAsync()
        {
            // Make sure there's only one server with the same identity at a time.
            using (var mutex = new Mutex(initiallyOwned: true, name: MutexName.GetServerMutexName(Pipe.Value()), createdNew: out var holdsMutex))
            {
                if (!holdsMutex)
                {
                    // Another server is running, just exit.
                    Error.Write("Another server already running...");
                    return(Task.FromResult(1));
                }

                try
                {
                    TimeSpan?keepAlive = null;
                    if (KeepAlive.HasValue())
                    {
                        var value = KeepAlive.Value();
                        if (int.TryParse(value, out var result))
                        {
                            // Keep alive times are specified in seconds
                            keepAlive = TimeSpan.FromSeconds(result);
                        }
                    }

                    var host = ConnectionHost.Create(Pipe.Value());

                    var compilerHost = CompilerHost.Create();
                    ExecuteServerCore(host, compilerHost, Cancelled, eventBus: null, keepAlive: keepAlive);
                }
                finally
                {
                    mutex.ReleaseMutex();
                }
            }

            return(Task.FromResult(0));
        }
        protected virtual void ExecuteServerCore(ConnectionHost host, CompilerHost compilerHost, CancellationToken cancellationToken, EventBus eventBus, TimeSpan?keepAlive)
        {
            var dispatcher = RequestDispatcher.Create(host, compilerHost, cancellationToken, eventBus, keepAlive);

            dispatcher.Run();
        }
 public static RequestDispatcher Create(ConnectionHost connectionHost, CompilerHost compilerHost, CancellationToken cancellationToken, EventBus eventBus, TimeSpan?keepAlive = null)
 {
     return(new DefaultRequestDispatcher(connectionHost, compilerHost, cancellationToken, eventBus, keepAlive));
 }
Exemple #4
0
        protected override Task <int> ExecuteCoreAsync()
        {
            // Make sure there's only one server with the same identity at a time.
            var   serverMutexName = MutexName.GetServerMutexName(Pipe.Value());
            Mutex serverMutex     = null;
            var   holdsMutex      = false;

            try
            {
                serverMutex = new Mutex(initiallyOwned: true, name: serverMutexName, createdNew: out holdsMutex);
            }
            catch (Exception ex)
            {
                // The Mutex constructor can throw in certain cases. One specific example is docker containers
                // where the /tmp directory is restricted. In those cases there is no reliable way to execute
                // the server and we need to fall back to the command line.
                // Example: https://github.com/dotnet/roslyn/issues/24124

                Error.Write($"Server mutex creation failed. {ex.Message}");

                return(Task.FromResult(-1));
            }

            if (!holdsMutex)
            {
                // Another server is running, just exit.
                Error.Write("Another server already running...");
                return(Task.FromResult(1));
            }

            FileStream pidFileStream = null;

            try
            {
                try
                {
                    // Write the process and pipe information to a file in a well-known location.
                    pidFileStream = WritePidFile();
                }
                catch (Exception ex)
                {
                    // Something happened when trying to write to the pid file. Log and move on.
                    ServerLogger.LogException(ex, "Failed to create PID file.");
                }

                TimeSpan?keepAlive = null;
                if (KeepAlive.HasValue() && int.TryParse(KeepAlive.Value(), out var result))
                {
                    // Keep alive times are specified in seconds
                    keepAlive = TimeSpan.FromSeconds(result);
                }

                var host = ConnectionHost.Create(Pipe.Value());

                var compilerHost = CompilerHost.Create();
                ExecuteServerCore(host, compilerHost, Cancelled, eventBus: null, keepAlive: keepAlive);
            }
            finally
            {
                serverMutex.ReleaseMutex();
                serverMutex.Dispose();
                pidFileStream?.Close();
            }

            return(Task.FromResult(0));
        }
Exemple #5
0
 public static RequestDispatcher Create(ConnectionHost connectionHost, CompilerHost compilerHost, CancellationToken cancellationToken)
 {
     return(new DefaultRequestDispatcher(connectionHost, compilerHost, cancellationToken));
 }