dotnet-installer/src/dotnet/BuildServer/RazorServerManager.cs
Peter Huene 3488a84c35
Implement buildserver shutdown command.
This commit implements the `buildserver shutdown` command that can be used to
shutdown MSBuild, VB/C# compiler, and Razor build servers.

By default, all three build servers are shut down.  Options can be passed to
shut down a subset of the build servers.

Fixes #8185.
2018-03-30 15:13:52 -07:00

65 lines
2.2 KiB
C#

// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Build.Exceptions;
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools;
using Microsoft.Extensions.EnvironmentAbstractions;
namespace Microsoft.DotNet.BuildServer
{
internal class RazorServerManager : IBuildServerManager
{
private readonly IRazorAssemblyResolver _resolver;
private readonly ICommandFactory _commandFactory;
public RazorServerManager(IRazorAssemblyResolver resolver = null, ICommandFactory commandFactory = null)
{
_resolver = resolver ?? new RazorAssemblyResolver();
_commandFactory = commandFactory ?? new DotNetCommandFactory(alwaysRunOutOfProc: true);
}
public string ServerName => LocalizableStrings.RazorServer;
public Task<Result> ShutdownServerAsync()
{
return Task.Run(() => {
try
{
bool haveRazorAssembly = false;
foreach (var toolAssembly in _resolver.EnumerateRazorToolAssemblies())
{
haveRazorAssembly = true;
var command = _commandFactory
.Create("exec", new string[] { toolAssembly.Value, "shutdown" })
.CaptureStdOut()
.CaptureStdErr();
var result = command.Execute();
if (result.ExitCode != 0)
{
return new Result(ResultKind.Failure, result.StdErr);
}
}
if (!haveRazorAssembly)
{
return new Result(ResultKind.Skipped, LocalizableStrings.NoRazorProjectFound);
}
return new Result(ResultKind.Success);
}
catch (InvalidProjectFileException ex)
{
return new Result(ex);
}
});
}
}
}